Skip to content

Commit de8e7ff

Browse files
committed
Move release logic into sidecar script
1 parent a1adc7a commit de8e7ff

File tree

2 files changed

+75
-42
lines changed

2 files changed

+75
-42
lines changed

.github/workflows/create-new-tag.yaml

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,30 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- name: Dump GitHub context
10+
# Get the version of _this_ repository that is in use so that we can use
11+
# sidecar scripts
12+
- id: workflow-parsing
13+
name: Get SHA of reusuable workflow
1114
env:
12-
GITHUB_CONTEXT: ${{ toJson(github) }}
13-
run: echo "$GITHUB_CONTEXT"
15+
REPO: ${{ github.repository }}
16+
RUN_ID: ${{ github.run_id }}
17+
GH_TOKEN: ${{ github.token }}
18+
run: |
19+
ACTION_DATA=$(gh api "repos/$REPO/actions/runs/$RUN_ID")
20+
echo "::debug::$ACTION_DATA"
21+
SHA=$(echo "$ACTION_DATA" | jq -r '.referenced_workflows | .[] | select(.path | startswith("uclahs-cds/tool-create-release")).sha')
22+
echo "SHA=$SHA" >> "$GITHUB_OUTPUT"
23+
24+
- name: Checkout reusable repository
25+
uses: actions/checkout@v4
26+
with:
27+
repository: uclahs-cds/tool-create-release
28+
ref: ${{ steps.workflow-parsing.outputs.SHA }}
29+
token: ${{ secrets.UCLAHS_CDS_REPO_READ_TOKEN }}
1430

1531
- id: parse-version
1632
uses: actions/github-script@v7
1733
with:
1834
script: |
19-
// Sanity-check that this was called from an appropriate merge event and
20-
// extract the version number embedded in the branch name.
21-
22-
if (context.eventName !== 'pull_request') {
23-
core.setFailed('Workflow requires pull_request events')
24-
process.exit()
25-
}
26-
27-
if (!context.payload.pull_request.merged || context.payload.pull_request.state !== 'closed') {
28-
core.setFailed('Workflow should only be called on merged and closed PRs')
29-
process.exit()
30-
}
31-
32-
if (!['Bot', 'Organization'].includes(context.payload.pull_request.user.type)) {
33-
core.setFailed('Workflow should only be called for Bot- or Organization-generated release PRs')
34-
process.exit()
35-
}
36-
37-
// This regex needs to kept in-sync with the pattern in create-release-pr.yaml
38-
const regex = /^automation-create-release-(.*)$/i
39-
const parsed_version = context.payload.pull_request.head.ref.match(regex)
40-
41-
if (!parsed_version || !parsed_version[1].length) {
42-
core.setFailed('Workflow not called from an appropriate branch name')
43-
process.exit()
44-
}
45-
46-
const new_version = parsed_version[1]
47-
48-
github.rest.repos.createRelease({
49-
owner: context.repo.owner,
50-
repo: context.repo.repo,
51-
tag_name: `v${new_version}`,
52-
target_commitish: context.payload.pull_request.merge_commit_sha,
53-
name: `Release ${new_version}`,
54-
draft: true,
55-
generate_release_notes: true,
56-
body: `Automatically generated after merging #${context.payload.number}.`
57-
})
35+
const script = require('./scripts/create-tag.js')
36+
await script({github, context, core})

scripts/create-tag.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module.exports = async ({ github, context, core }) => {
2+
// Sanity-check that this was called from an appropriate merge event and
3+
// extract the version number embedded in the branch name.
4+
if (context.eventName !== 'pull_request') {
5+
core.setFailed('Workflow requires pull_request events')
6+
process.exit()
7+
}
8+
9+
if (
10+
!context.payload.pull_request.merged ||
11+
context.payload.pull_request.state !== 'closed'
12+
) {
13+
core.setFailed('Workflow should only be called on merged and closed PRs')
14+
process.exit()
15+
}
16+
17+
if (
18+
!['Bot', 'Organization'].includes(context.payload.pull_request.user.type)
19+
) {
20+
core.setFailed(
21+
'Workflow should only be called for Bot- or Organization-generated release PRs'
22+
)
23+
process.exit()
24+
}
25+
26+
// This regex needs to kept in-sync with the pattern in create-release-pr.yaml
27+
const regex = /^automation-create-release-(.*)$/i
28+
const parsedVersion = context.payload.pull_request.head.ref.match(regex)
29+
30+
if (!parsedVersion || !parsedVersion[1].length) {
31+
core.setFailed('Workflow not called from an appropriate branch name')
32+
process.exit()
33+
}
34+
35+
const newVersion = parsedVersion[1]
36+
37+
await github.rest.repos.createRelease({
38+
owner: context.repo.owner,
39+
repo: context.repo.repo,
40+
tag_name: `v${newVersion}`,
41+
target_commitish: context.payload.pull_request.merge_commit_sha,
42+
name: `Release ${newVersion}`,
43+
draft: true,
44+
generate_release_notes: true,
45+
body: `Automatically generated after merging #${context.payload.number}.`
46+
})
47+
48+
// Attempt to delete the release branch
49+
await github.rest.git.deleteRef({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
ref: `heads/${context.payload.pull_request.head.ref}`
53+
})
54+
}

0 commit comments

Comments
 (0)