From 6957760055ae1da7f0811ffb7a9c06c838f47337 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Thu, 13 Aug 2020 23:11:26 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Avoid=20build=20failures=20on=20?= =?UTF-8?q?schedule=20events:=20use=20a=20fixed=20"defaultbranch"=20instea?= =?UTF-8?q?d=20of=20computing=20the=20branch=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit schedule event misses repository information in the payload. --- .../com/github/burrunan/gradle/GradleCacheAction.kt | 4 ++++ .../burrunan/gradle/cache/dependenciesCache.kt | 5 ++++- .../github/burrunan/gradle/cache/localBuildCache.kt | 13 +++++++++++-- .../burrunan/gradle/github/event/ActionsTrigger.kt | 7 ++++++- .../github/burrunan/gradle/github/event/Event.kt | 3 ++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/GradleCacheAction.kt b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/GradleCacheAction.kt index dc47d08..a152747 100644 --- a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/GradleCacheAction.kt +++ b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/GradleCacheAction.kt @@ -26,6 +26,10 @@ import actions.exec.exec import kotlinx.coroutines.await class GradleCacheAction(val trigger: ActionsTrigger, val params: Parameters) { + companion object { + const val DEFAULT_BRANCH_VAR = "defaultbranch" + } + private val treeId = suspendingStateVariable("tree_id") { exec("git", "log", "-1", "--quiet", "--format=%T").stdout } diff --git a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/dependenciesCache.kt b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/dependenciesCache.kt index b9531ac..578a8ea 100644 --- a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/dependenciesCache.kt +++ b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/dependenciesCache.kt @@ -17,6 +17,7 @@ package com.github.burrunan.gradle.cache import actions.core.debug +import com.github.burrunan.gradle.GradleCacheAction import com.github.burrunan.gradle.github.env.ActionsEnvironment import com.github.burrunan.gradle.github.event.ActionsTrigger import com.github.burrunan.gradle.github.event.cacheKey @@ -32,7 +33,7 @@ suspend fun dependenciesCache( cacheLocation: List, pathDependencies: List, ): Cache { - val defaultBranch = trigger.event.repository.default_branch + val defaultBranch = GradleCacheAction.DEFAULT_BRANCH_VAR val pkPrefix = trigger.cacheKey val cacheName = "dependencies-$name" @@ -49,6 +50,8 @@ suspend fun dependenciesCache( restoreKeys = listOf( "$prefix-$pkPrefix", "$prefix-$defaultBranch", + "$prefix-master", + "$prefix-main", ), paths = cacheLocation, ) diff --git a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/localBuildCache.kt b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/localBuildCache.kt index 25cab3e..011f2ea 100644 --- a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/localBuildCache.kt +++ b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/cache/localBuildCache.kt @@ -15,12 +15,13 @@ */ package com.github.burrunan.gradle.cache +import com.github.burrunan.gradle.GradleCacheAction import com.github.burrunan.gradle.github.event.ActionsTrigger import com.github.burrunan.gradle.github.event.cacheKey fun localBuildCache(jobId: String, trigger: ActionsTrigger, gradleVersion: String, treeId: String): Cache { val buildCacheLocation = "~/.gradle/caches/build-cache-1" - val defaultBranch = trigger.event.repository.default_branch + val defaultBranch = GradleCacheAction.DEFAULT_BRANCH_VAR val pkPrefix = trigger.cacheKey val restoreKeys = when (trigger) { @@ -28,12 +29,20 @@ fun localBuildCache(jobId: String, trigger: ActionsTrigger, gradleVersion: Strin pkPrefix, trigger.event.pull_request.base.ref.removePrefix("refs/heads/"), defaultBranch, + "master", + "main", ) is ActionsTrigger.BranchPush -> arrayOf( pkPrefix, defaultBranch, + "master", + "main", + ) + is ActionsTrigger.Other -> arrayOf( + defaultBranch, + "master", + "main", ) - is ActionsTrigger.Other -> throw IllegalStateException("Unknown event ${trigger.name}") } val prefix = "gradle-build-cache-$jobId-$gradleVersion" return LayeredCache( diff --git a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/ActionsTrigger.kt b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/ActionsTrigger.kt index dabdc4f..b743673 100644 --- a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/ActionsTrigger.kt +++ b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/ActionsTrigger.kt @@ -15,6 +15,7 @@ */ package com.github.burrunan.gradle.github.event +import com.github.burrunan.gradle.GradleCacheAction import com.github.burrunan.gradle.github.env.ActionsEnvironment import fs2.promises.readFile import kotlinx.coroutines.await @@ -28,7 +29,11 @@ sealed class ActionsTrigger(val name: String, open val event: Event) { val ActionsTrigger.cacheKey: String get() = when (this) { is ActionsTrigger.PullRequest -> "PR${event.pull_request.number}" - is ActionsTrigger.BranchPush -> event.ref.removePrefix("refs/heads/") + is ActionsTrigger.BranchPush -> when (val ref = event.ref.removePrefix("refs/heads/")) { + event.repository.default_branch.removePrefix("refs/heads/") -> + GradleCacheAction.DEFAULT_BRANCH_VAR + else -> ref + } is ActionsTrigger.Other -> "$name-${ActionsEnvironment.GITHUB_WORKFLOW}-${ActionsEnvironment.GITHUB_SHA}" } diff --git a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/Event.kt b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/Event.kt index 70c817e..1ce703d 100644 --- a/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/Event.kt +++ b/layered-cache/src/main/kotlin/com/github/burrunan/gradle/github/event/Event.kt @@ -16,13 +16,13 @@ package com.github.burrunan.gradle.github.event external interface Event { - var repository: Repository } /** * See https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#push */ external interface BranchPushEvent : Event { + var repository: Repository var action: String var ref: String var commits: List @@ -38,6 +38,7 @@ external interface BranchPushEvent : Event { * See https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#pull_request */ external interface PullRequestEvent : Event { + var repository: Repository var url: String var id: Number var number: Int