11name : Versioned Tag
22
33on :
4+ # Trigger on push if it includes a tag like vX.Y.Z
45 push :
56 tags :
67 - ' v*.*.*'
1516 pull-requests : write
1617
1718 steps :
19+ # 1) Check if a non-draft release with this tag already exists
1820 - name : Check if release already exists
1921 id : check_release
2022 uses : actions/github-script@v7
@@ -25,25 +27,54 @@ jobs:
2527 owner: context.repo.owner,
2628 repo: context.repo.repo
2729 });
28-
2930 const existing = releases.data.find(r => r.tag_name === tag && !r.draft);
3031 if (existing) {
3132 core.setOutput('skip', 'true');
3233 } else {
3334 core.setOutput('skip', 'false');
3435 }
3536
37+ # If a published release already exists, skip the rest of the workflow
3638 - name : Skip if release already exists
3739 if : steps.check_release.outputs.skip == 'true'
3840 run : echo "Release already exists, skipping workflow."
3941
42+ # 2) Determine the base branch from which the tag was pushed
43+ - name : Get base branch
44+ if : steps.check_release.outputs.skip == 'false'
45+ id : get_base
46+ uses : actions/github-script@v7
47+ with :
48+ script : |
49+ /*
50+ For a push event with a tag, GitHub sets context.payload.base_ref
51+ if the tag was pushed from a branch.
52+ If it's empty, we can't determine the correct base branch and must fail.
53+ */
54+ const baseRef = context.payload.base_ref;
55+ if (!baseRef) {
56+ core.setFailed(`❌ base_ref is empty. Make sure you push the tag from a branch (e.g. 'git push origin HEAD:refs/tags/vX.Y.Z').`);
57+ return;
58+ }
59+
60+ const shortBranch = baseRef.replace("refs/heads/", "");
61+ const releasePattern = /^release-\d+\.\d+$/;
62+ if (shortBranch !== "main" && !releasePattern.test(shortBranch)) {
63+ core.setFailed(`❌ Tagged commit must belong to branch 'main' or 'release-X.Y'. Got '${shortBranch}'`);
64+ return;
65+ }
66+
67+ core.setOutput('branch', shortBranch);
68+
69+ # 3) Checkout full git history and tags
4070 - name : Checkout code
4171 if : steps.check_release.outputs.skip == 'false'
4272 uses : actions/checkout@v4
4373 with :
4474 fetch-depth : 0
4575 fetch-tags : true
4676
77+ # 4) Login to GitHub Container Registry
4778 - name : Login to GitHub Container Registry
4879 if : steps.check_release.outputs.skip == 'false'
4980 uses : docker/login-action@v3
@@ -52,10 +83,12 @@ jobs:
5283 password : ${{ secrets.GITHUB_TOKEN }}
5384 registry : ghcr.io
5485
86+ # 5) Build project artifacts
5587 - name : Build
5688 if : steps.check_release.outputs.skip == 'false'
5789 run : make build
5890
91+ # 6) Optionally commit built artifacts to the repository
5992 - name : Commit release artifacts
6093 if : steps.check_release.outputs.skip == 'false'
6194 env :
@@ -67,55 +100,56 @@ jobs:
67100 git add .
68101 git commit -m "Prepare release ${GITHUB_REF#refs/tags/}" -s || echo "No changes to commit"
69102
103+ # 7) Create a release branch like release-X.Y.Z
70104 - name : Create release branch
71105 if : steps.check_release.outputs.skip == 'false'
72106 run : |
73107 BRANCH_NAME="release-${GITHUB_REF#refs/tags/v}"
74108 git branch -f "$BRANCH_NAME"
75109 git push origin "$BRANCH_NAME" --force
76110
111+ # 8) Create a pull request from release-X.Y.Z to the original base branch
77112 - name : Create pull request if not exists
78113 if : steps.check_release.outputs.skip == 'false'
79114 uses : actions/github-script@v7
80115 with :
81116 script : |
82117 const version = context.ref.replace('refs/tags/v', '');
83- const branch = `release-${version}` ;
84- const base = 'main' ;
85-
118+ const base = '${{ steps.get_base.outputs.branch }}' ;
119+ const head = `release-${version}` ;
120+
86121 const prs = await github.rest.pulls.list({
87122 owner: context.repo.owner,
88123 repo: context.repo.repo,
89- head : ` ${context.repo.owner}:${branch }` ,
124+ head: `${context.repo.owner}:${head }`,
90125 base
91126 });
92-
127+
93128 if (prs.data.length === 0) {
94129 const newPr = await github.rest.pulls.create({
95130 owner: context.repo.owner,
96131 repo: context.repo.repo,
97- head : branch ,
98- base : base ,
132+ head,
133+ base,
99134 title: `Release v${version}`,
100135 body:
101136 `This PR prepares the release \`v${version}\`.\n` +
102137 `(Please merge it before releasing draft)`,
103138 draft: false
104139 });
105-
106- console.log(`Created pull request # ${newPr.data.number} from ${branch} to ${base}`);
107-
140+
141+ console.log(`Created pull request #${newPr.data.number} from ${head} to ${base}`);
108142 await github.rest.issues.addLabels({
109143 owner: context.repo.owner,
110144 repo: context.repo.repo,
111145 issue_number: newPr.data.number,
112146 labels: ['release']
113147 });
114-
115148 } else {
116- console.log(`Pull request already exists from ${branch } to ${base}`);
149+ console.log(`Pull request already exists from ${head } to ${base}`);
117150 }
118151
152+ # 9) Create or reuse an existing draft GitHub release for this tag
119153 - name : Create or reuse draft release
120154 if : steps.check_release.outputs.skip == 'false'
121155 id : create_release
@@ -141,22 +175,20 @@ jobs:
141175 }
142176 core.setOutput('upload_url', release.upload_url);
143177
178+ # 10) Build additional assets for the release (if needed)
144179 - name : Build assets
145180 if : steps.check_release.outputs.skip == 'false'
146181 run : make assets
147182 env :
148183 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
149184
185+ # 11) Upload assets to the draft release
150186 - name : Upload assets
151187 if : steps.check_release.outputs.skip == 'false'
152188 run : make upload_assets VERSION=${GITHUB_REF#refs/tags/}
153189 env :
154190 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
155191
156- - name : Delete pushed tag
157- if : steps.check_release.outputs.skip == 'false'
158- run : |
159- git push --delete origin ${GITHUB_REF#refs/tags/}
160-
192+ # 12) Run tests
161193 - name : Run tests
162194 run : make test
0 commit comments