Skip to content

Commit

Permalink
Fixed yarn install on CI
Browse files Browse the repository at this point in the history
Update repos to use private mirrors on CI
  • Loading branch information
Mr3zee committed Sep 23, 2024
1 parent 9a7cea8 commit ee524bf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 57 deletions.
41 changes: 25 additions & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/

import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnLockMismatchReport
import util.getSpacePassword
import util.getSpaceUsername
import util.kotlinVersionParsed
import util.libs

Expand Down Expand Up @@ -67,38 +67,44 @@ if (kotlinVersion != kotlinGPVersion) {
}

val executeNpmLogin by tasks.registering {
val isCI = System.getenv("TEAMCITY_VERSION") != null
val localYarnUpdate = project.providers.gradleProperty("kotlinx.rpc.localYarnUpdate")

if (!isCI && localYarnUpdate.orNull?.toBooleanStrictOrNull() != true) {
return@registering
}

val registryUrl = "https://packages.jetbrains.team/npm/p/krpc/build-deps/"

// To prevent leaking of credentials in VCS on dev machine use the build directory config file
val buildYarnConfigFile = File(project.rootDir, "build/js/.yarnrc")
val buildYarnYmlConfigFile = File(project.rootDir, "build/js/.yarnrc.yml")
val buildNpmConfigFile = File(project.rootDir, "build/js/.npmrc")

val spaceUsername: String? = getSpaceUsername()
val spacePassword: String? = getSpacePassword()
val spacePassword: String = project.getSpacePassword()

doLast {
if (spaceUsername == null || spacePassword == null) {
return@doLast
}

if (spacePassword.split(".").size != 3) {
error("Unexpected Space Token format")
}

val outputYarnYmlText = """
npmRegistryServer: "$registryUrl"
npmAlwaysAuth: true
npmAuthToken: "$spacePassword"
val outputYarnText = """
registry: "$registryUrl"
""".trimIndent()

val outputNpmText = """
registry: "$registryUrl"
always-auth: true
${registryUrl.removePrefix("https:")}:_authToken=$spacePassword
""".trimIndent()

buildYarnConfigFile.createNewFile()
buildYarnConfigFile.writeText("registry: $registryUrl")
buildYarnYmlConfigFile.createNewFile()
buildYarnYmlConfigFile.writeText(outputYarnYmlText)
buildYarnConfigFile.writeText(outputYarnText)
buildNpmConfigFile.createNewFile()
buildNpmConfigFile.writeText(outputNpmText)
}

outputs.file(buildYarnConfigFile).withPropertyName("buildOutputYarnFile")
outputs.file(buildYarnYmlConfigFile).withPropertyName("buildOutputYarnYmlFile")
outputs.file(buildNpmConfigFile).withPropertyName("buildOutputNpmFile")
}

val isCI = System.getenv("TEAMCITY_VERSION") != null
Expand All @@ -125,6 +131,9 @@ rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlu

if (isCI) {
downloadBaseUrl = "https://packages.jetbrains.team/files/p/krpc/build-deps/"
yarnLockMismatchReport = YarnLockMismatchReport.FAIL
} else {
yarnLockMismatchReport = YarnLockMismatchReport.WARNING
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

@file:Suppress("DuplicatedCode", "MISSING_DEPENDENCY_CLASS")

import java.util.*

pluginManagement {
fun logAbsentProperty(name: String): Nothing? {
logger.info("Property '$name' is not present for repository credentials.")
val isCI = System.getenv("TEAMCITY_VERSION") != null

return null
fun requiredPropertyError(name: String, env: String): Nothing {
throw GradleException("Property '$name' (or env var '$env') is not present for repository credentials.")
}

@Suppress("RemoveRedundantQualifierName")
fun getLocalProperties(): java.util.Properties {
return java.util.Properties().apply {
val propertiesDir = File(
Expand All @@ -31,20 +28,22 @@ pluginManagement {
}
}

fun getSpaceUsername(): String? {
fun getSpaceUsername(): String {
val username = "kotlinx.rpc.team.space.username"
val usernameEnv = "kotlinx_rpc_team_space_username"
return getLocalProperties()[username] as String?
?: settings.providers.gradleProperty(username).orNull
?: System.getenv(username)?.ifEmpty { null }
?: logAbsentProperty(username)
?: System.getenv(usernameEnv)?.ifEmpty { null }
?: requiredPropertyError(username, usernameEnv)
}

fun getSpacePassword(): String? {
fun getSpacePassword(): String {
val password = "kotlinx.rpc.team.space.password"
val passwordEnv = "kotlinx_rpc_team_space_password"
return getLocalProperties()[password] as String?
?: settings.providers.gradleProperty(password).orNull
?: System.getenv(password)?.ifEmpty { null }
?: logAbsentProperty(password)
?: System.getenv(passwordEnv)?.ifEmpty { null }
?: requiredPropertyError(password, passwordEnv)
}

/**
Expand All @@ -56,6 +55,7 @@ pluginManagement {
maven {
name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } }
url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName")

credentials {
username = getSpaceUsername()
password = getSpacePassword()
Expand All @@ -67,20 +67,25 @@ pluginManagement {
fun RepositoryHandler.buildDepsEap() = jbTeamPackages(repoName = "build-deps-eap")

repositories {
buildDeps()
buildDepsEap()
if (isCI) {
buildDeps()
buildDepsEap()
} else {
mavenCentral()
gradlePluginPortal()
}
}
}

gradle.rootProject {
fun logAbsentProperty(name: String): Nothing? {
logger.info("Property '$name' is not present for repository credentials.")
val isCI = System.getenv("TEAMCITY_VERSION") != null

return null
fun requiredPropertyError(name: String, env: String): Nothing {
throw GradleException("Property '$name' (or env var '$env') is not present for repository credentials.")
}

fun getLocalProperties(): Properties {
return Properties().apply {
fun getLocalProperties(): java.util.Properties {
return java.util.Properties().apply {
val propertiesDir = File(
rootDir.path
.removeSuffix("/gradle-conventions")
Expand All @@ -96,20 +101,22 @@ gradle.rootProject {
}
}

fun getSpaceUsername(): String? {
fun getSpaceUsername(): String {
val username = "kotlinx.rpc.team.space.username"
val usernameEnv = "kotlinx_rpc_team_space_username"
return getLocalProperties()[username] as String?
?: settings.providers.gradleProperty(username).orNull
?: System.getenv(username)?.ifEmpty { null }
?: logAbsentProperty(username)
?: System.getenv(usernameEnv)?.ifEmpty { null }
?: requiredPropertyError(username, usernameEnv)
}

fun getSpacePassword(): String? {
fun getSpacePassword(): String {
val password = "kotlinx.rpc.team.space.password"
val passwordEnv = "kotlinx_rpc_team_space_password"
return getLocalProperties()[password] as String?
?: settings.providers.gradleProperty(password).orNull
?: System.getenv(password)?.ifEmpty { null }
?: logAbsentProperty(password)
?: System.getenv(passwordEnv)?.ifEmpty { null }
?: requiredPropertyError(password, passwordEnv)
}

/**
Expand All @@ -122,6 +129,7 @@ gradle.rootProject {
name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } }

url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName")

credentials {
username = getSpaceUsername()
password = getSpacePassword()
Expand All @@ -135,13 +143,28 @@ gradle.rootProject {
allprojects {
buildscript {
repositories {
buildDeps()
buildDepsEap()
if (isCI) {
buildDeps()
buildDepsEap()
} else {
mavenCentral()
gradlePluginPortal()
}
}
}

repositories {
buildDeps()
buildDepsEap()
if (isCI) {
buildDeps()
buildDepsEap()
} else {
mavenCentral()
gradlePluginPortal()
maven("https://www.jetbrains.com/intellij-repository/releases")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
maven("https://maven.pkg.jetbrains.space/public/p/ktor/eap")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
}
}
}
}
26 changes: 14 additions & 12 deletions gradle-conventions-settings/src/main/kotlin/util/properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

package util

import org.gradle.api.GradleException
import org.gradle.api.Project
import java.io.File
import java.util.*

fun Project.logAbsentProperty(name: String): Nothing? {
logger.info("Property '$name' is not present.")

return null
fun requiredPropertyError(name: String, env: String): Nothing {
throw GradleException("Property '$name' (or env var '$env') is not present for repository credentials.")
}

fun Project.getLocalProperties(): java.util.Properties {
return java.util.Properties().apply {
fun Project.getLocalProperties(): Properties {
return Properties().apply {
val propertiesDir = File(
rootDir.path
.removeSuffix("/gradle-conventions")
Expand All @@ -32,18 +32,20 @@ fun Project.getLocalProperties(): java.util.Properties {
}
}

fun Project.getSpaceUsername(): String? {
fun Project.getSpaceUsername(): String {
val username = "kotlinx.rpc.team.space.username"
val usernameEnv = "kotlinx_rpc_team_space_username"
return getLocalProperties()[username] as String?
?: providers.gradleProperty(username).orNull
?: System.getenv(username)?.ifEmpty { null }
?: logAbsentProperty(username)
?: System.getenv(usernameEnv)?.ifEmpty { null }
?: requiredPropertyError(username, usernameEnv)
}

fun Project.getSpacePassword(): String? {
fun Project.getSpacePassword(): String {
val password = "kotlinx.rpc.team.space.password"
val passwordEnv = "kotlinx_rpc_team_space_password"
return getLocalProperties()[password] as String?
?: providers.gradleProperty(password).orNull
?: System.getenv(password)?.ifEmpty { null }
?: logAbsentProperty(password)
?: System.getenv(passwordEnv)?.ifEmpty { null }
?: requiredPropertyError(password, passwordEnv)
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pluginManagement {

includeBuild("gradle-plugin")

apply(from = "gradle-conventions-settings/src/main/kotlin/conventions-repositories.settings.gradle.kts")

resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlinx-atomicfu") {
Expand Down

0 comments on commit ee524bf

Please sign in to comment.