Skip to content

Commit e98fb82

Browse files
authored
Merge pull request #230 from raboof/githubWorkflowUpdate
feat: update workflows but keep any action hashes intact
2 parents a6a53f9 + 49f0807 commit e98fb82

6 files changed

Lines changed: 138 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ ThisBuild / githubWorkflowPublish := Seq(
9797

9898
- `githubWorkflowGenerate` – Generates (and overwrites if extant) **ci.yml** and **clean.yml** workflows according to configuration within sbt. The **clean.yml** workflow is something that GitHub Actions should just do by default: it removes old build artifacts to prevent them from running up your storage usage (it has no effect on currently running builds). This workflow is unconfigurable and is simply drawn from the static contents of the **clean.yml** resource file within this repository.
9999
- `githubWorkflowCheck` – Checks to see if the **ci.yml** and **clean.yml** files are equivalent to what would be generated and errors if otherwise. This task is run from within the generated **ci.yml** to ensure that the build and the workflow are kept in sync. As a general rule, any time you change the workflow configuration within sbt, you should regenerate the **ci.yml** and commit the results, but inevitably people forget. This check fails the build if that happens. Note that if you *need* to manually fiddle with the **ci.yml** contents, for whatever reason, you will need to remove the call to this check from within the workflow, otherwise your build will simply fail.
100+
- `githubWorkflowUpdate` – Regenerates **ci.yml** and **clean.yml** while preserving the action references. This lets us use Dependabot to manage the action dependencies.
100101

101102
## Settings
102103

src/main/scala/sbtghactions/GenerativeKeys.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ trait GenerativeKeys {
2525
@transient
2626
lazy val githubWorkflowGenerate = taskKey[Unit]("Generates (and overwrites if extant) a ci.yml and clean.yml actions description according to configuration")
2727
@transient
28+
lazy val githubWorkflowUpdate = taskKey[Unit]("Updates ci.yml and clean.yml keeping existing action hashes intact")
29+
@transient
2830
lazy val githubWorkflowCheck = taskKey[Unit]("Checks to see if the ci.yml and clean.yml files are equivalent to what would be generated and errors if otherwise")
2931

3032
lazy val githubWorkflowDir = settingKey[File]("Where to place the workflow directory which contains the generated ci.yml and clean.yml files. (default: baseDirectory.value / \".github\")")

src/main/scala/sbtghactions/GenerativePlugin.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
921921
},
922922

923923
githubWorkflowGenerate / aggregate := false,
924+
githubWorkflowUpdate / aggregate := false,
924925
githubWorkflowCheck / aggregate := false,
925926

926927
githubWorkflowGenerate := {
@@ -937,6 +938,32 @@ ${indent(jobs.map(compileJob(_, sbt)).mkString("\n\n"), 1)}
937938
IO.write(cleanYml, cleanContents)
938939
},
939940

941+
githubWorkflowUpdate := {
942+
val ciContents = generateCiContents.value
943+
val includeClean = githubWorkflowIncludeClean.value
944+
val cleanContents = generateCleanContents(githubWorkflowOSes.value.head)
945+
946+
val ciYml = ciYmlFile.value
947+
val cleanYml = cleanYmlFile.value
948+
949+
def updateAndWrite(yml: File, contents: String) = {
950+
val existingContents = IO.read(yml)
951+
val hashRefPattern = "uses: ([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)@([a-z0-9]{40}.*)".r
952+
val existingHashes = hashRefPattern.findAllMatchIn(existingContents)
953+
val updatedContents = existingHashes.foldLeft(contents)((acc, action) =>
954+
acc.replaceAll(
955+
s"uses: ${action.group(1)}@.*",
956+
s"uses: ${action.group(1)}@${action.group(2)}"
957+
)
958+
)
959+
IO.write(yml, updatedContents)
960+
}
961+
962+
updateAndWrite(ciYml, ciContents)
963+
if(includeClean)
964+
updateAndWrite(cleanYml, cleanContents)
965+
},
966+
940967
githubWorkflowCheck := {
941968
val expectedCiContents = generateCiContents.value
942969
val includeClean = githubWorkflowIncludeClean.value
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# This file was automatically generated by sbt-github-actions using the
2+
# githubWorkflowGenerate task. You should add and commit this file to
3+
# your git repository. It goes without saying that you shouldn't edit
4+
# this file by hand! Instead, if you wish to make changes, you should
5+
# change your sbt build configuration to revise the workflow description
6+
# to meet your needs, then regenerate this file.
7+
8+
# This file was changed, to test that githubWorkflowUpdate will overwrite
9+
# all such changes *except* references to actions by hash.
10+
11+
name: Continuous Integration
12+
13+
on:
14+
pull_request:
15+
branches: ['**']
16+
push:
17+
branches: ['**']
18+
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
22+
jobs:
23+
build:
24+
name: Build and Test
25+
strategy:
26+
matrix:
27+
os: [ubuntu-latest]
28+
scala: [2.13.10]
29+
java: [zulu@8]
30+
runs-on: ${{ matrix.os }}
31+
steps:
32+
- name: Checkout current branch (full)
33+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
34+
with:
35+
fetch-depth: 0
36+
37+
- name: Setup Java (zulu@8)
38+
if: matrix.java == 'zulu@8'
39+
uses: actions/setup-java@v5
40+
with:
41+
distribution: zulu
42+
java-version: 8
43+
cache: sbt
44+
45+
- name: Setup sbt
46+
uses: sbt/setup-sbt@v1
47+
48+
- name: Check that workflows are up to date
49+
run: sbt '++ ${{ matrix.scala }}; githubWorkflowCheck'
50+
51+
- name: Build project
52+
run: sbt '++ ${{ matrix.scala }}; test'
53+
54+
- name: Compress target directories
55+
run: tar cf targets.tar target project/target
56+
57+
- name: Upload target directories
58+
uses: actions/upload-artifact@v7
59+
with:
60+
name: target-${{ matrix.os }}-${{ matrix.scala }}-${{ matrix.java }}
61+
path: targets.tar
62+
63+
publish:
64+
name: Publish Artifacts
65+
needs: [build]
66+
if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main')
67+
strategy:
68+
matrix:
69+
os: [ubuntu-latest]
70+
scala: [2.13.10]
71+
java: [zulu@8]
72+
runs-on: ${{ matrix.os }}
73+
steps:
74+
- name: Checkout current branch (full)
75+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
76+
with:
77+
fetch-depth: 0
78+
79+
- name: Setup Java (zulu@8)
80+
if: matrix.java == 'zulu@8'
81+
uses: actions/setup-java@v5
82+
with:
83+
distribution: zulu
84+
java-version: 8
85+
cache: sbt
86+
87+
- name: Setup sbt
88+
uses: sbt/setup-sbt@v1
89+
90+
- name: Download target directories (2.13.10)
91+
uses: actions/download-artifact@v8
92+
with:
93+
name: target-${{ matrix.os }}-2.13.10-${{ matrix.java }}
94+
95+
- name: Inflate target directories (2.13.10)
96+
run: |
97+
tar xf targets.tar
98+
rm targets.tar
99+
100+
- name: Publish project
101+
run: sbt +publish

src/sbt-test/sbtghactions/allow-hashes/.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
runs-on: ${{ matrix.os }}
7070
steps:
7171
- name: Checkout current branch (full)
72-
uses: actions/checkout@v7
72+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
7373
with:
7474
fetch-depth: 0
7575

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
> githubWorkflowCheck
2+
$ copy-file .github/workflows/ci.yml .github/workflows/ci-ok.yml
3+
$ copy-file .github/workflows/ci-modified.yml .github/workflows/ci.yml
4+
> githubWorkflowUpdate
5+
# The update should overwrite the modifications but keep the
6+
# by-hash references to actions intact
7+
$ must-mirror .github/workflows/ci.yml .github/workflows/ci-ok.yml

0 commit comments

Comments
 (0)