Skip to content

Commit

Permalink
Deploy færre apper om gangen til prod (#798)
Browse files Browse the repository at this point in the history
* Forslag til prod workflow

* Sammenlign forrige og nåværende tag for å finne apper å prodsette

* Hent flere commits for å inkludere forrige tag

* Fjern utdatert kode

* Innskrenk hvilke filer som trigger full deploy

* Formater

---------

Co-authored-by: Gustav Berggren <[email protected]>
  • Loading branch information
bjerga and b162214 authored Dec 9, 2024
1 parent 4f2f664 commit 87f6bbc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
fetch-depth: 25

- name: Cache gradle wrapper
uses: actions/cache@v4
Expand All @@ -45,14 +45,15 @@ jobs:
- name: Determine changes files
run: |
PREVIOUS_TAG=$(git tag --sort version:refname | tail -n 2 | head -n 1)
# Create a comma-separated list of changed files for use in build.gradle.kts
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m $GITHUB_SHA | tr '\r\n' ',' | sed -e 's/,$//' | tr -d '"')
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m $PREVIOUS_TAG $GITHUB_REF | tr '\r\n' ',' | sed -e 's/,$//' | tr -d '"')
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
- name: Determine projects to deploy
id: set_matrix
run: |
MATRIX=$(./gradlew -q buildAllMatrix --console=plain)
MATRIX=$(./gradlew -q buildMatrix --console=plain)
echo $MATRIX
echo $(./gradlew -q deployMatrixProd --console=plain)
DEPLOY_MATRIX=$(./gradlew -q deployMatrixProd --console=plain)
Expand Down
44 changes: 9 additions & 35 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,20 @@ tasks {
}
}

create("buildAllMatrix") {
doLast {
taskOutputJson(
"project" to getBuildableProjects(buildAll = true).toJsonList(),
)
}
}

create("deployMatrix") {
deployMatrix()
}

create("deployMatrixDev") {
deployMatrix(includeCluster = "dev-gcp")
}

create("deployMatrixProd") {
deployMatrix(includeCluster = "prod-gcp", deployAll = true)
deployMatrix(includeCluster = "prod-gcp")
}

check {
dependsOn(named<JacocoReport>("testCodeCoverageReport"))
}
}

fun getBuildableProjects(buildAll: Boolean = false): List<String> {
if (buildAll) return subprojects.map { it.name }
fun getBuildableProjects(): List<String> {
val changedFiles =
System
.getenv("CHANGED_FILES")
Expand All @@ -177,12 +164,10 @@ fun getBuildableProjects(buildAll: Boolean = false): List<String> {

val hasCommonChanges =
changedFiles.any {
it.startsWith("felles/") ||
it.startsWith("felles/src/main/") ||
it in
listOf(
"Dockerfile",
".github/workflows/build.yml",
"config/nais.yml",
"build.gradle.kts",
"gradle.properties",
)
Expand All @@ -205,25 +190,17 @@ fun getBuildableProjects(buildAll: Boolean = false): List<String> {
}
}

fun getDeployMatrixVariables(
includeCluster: String? = null,
deployAll: Boolean = false,
): Triple<Set<String>, Set<String>, List<Pair<String, String>>> {
fun getDeployMatrixVariables(includeCluster: String): Triple<Set<String>, Set<String>, List<Pair<String, String>>> {
val clustersByProject =
getBuildableProjects(deployAll)
getBuildableProjects()
.associateWith { project ->
File("config", project)
.listFiles()
?.filter { it.isFile && it.name.endsWith(".yml") }
?.map { it.name.removeSuffix(".yml") }
?.let { clusters ->
if (includeCluster != null) {
listOf(includeCluster).intersect(clusters.toSet())
} else {
clusters
}
}?.toSet()
?.ifEmpty { null }
setOf(includeCluster).intersect(clusters.toSet())
}?.ifEmpty { null }
}.mapNotNull { (key, value) ->
value?.let { key to it }
}.toMap()
Expand Down Expand Up @@ -273,16 +250,13 @@ fun Project.erFellesDatabaseModul(): Boolean = name == "felles-db-exposed"

fun Project.erIntegrasjonstestModul(): Boolean = name == "integrasjonstest"

fun Task.deployMatrix(
includeCluster: String? = null,
deployAll: Boolean = false,
) {
fun Task.deployMatrix(includeCluster: String) {
doLast {
val (
deployableProjects,
clusters,
exclusions,
) = getDeployMatrixVariables(includeCluster, deployAll)
) = getDeployMatrixVariables(includeCluster)

taskOutputJson(
"project" to deployableProjects.toJsonList(),
Expand Down

0 comments on commit 87f6bbc

Please sign in to comment.