Skip to content

Commit

Permalink
🐛 Avoid build failures on schedule events: use a fixed "defaultbranch…
Browse files Browse the repository at this point in the history
…" instead of computing the branch name

schedule event misses repository information in the payload.
  • Loading branch information
vlsi committed Aug 14, 2020
1 parent 01d4c8f commit 6957760
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +33,7 @@ suspend fun dependenciesCache(
cacheLocation: List<String>,
pathDependencies: List<String>,
): Cache {
val defaultBranch = trigger.event.repository.default_branch
val defaultBranch = GradleCacheAction.DEFAULT_BRANCH_VAR
val pkPrefix = trigger.cacheKey
val cacheName = "dependencies-$name"

Expand All @@ -49,6 +50,8 @@ suspend fun dependenciesCache(
restoreKeys = listOf(
"$prefix-$pkPrefix",
"$prefix-$defaultBranch",
"$prefix-master",
"$prefix-main",
),
paths = cacheLocation,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@
*/
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) {
is ActionsTrigger.PullRequest -> arrayOf(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Commit>
Expand All @@ -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
Expand Down

0 comments on commit 6957760

Please sign in to comment.