Skip to content

Commit 456cb67

Browse files
TheLartiansCopilot
andauthored
Use manually github workflow to create releases (#710)
* use manually github workflow to create releases * ensure that release tags always start with 'v' and set pipefail * better errror handling * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * push tag in the end only * address review feedback * pass variables through env * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * get version directly from tag * validate the branch explicitly * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4cc97fa commit 456cb67

1 file changed

Lines changed: 86 additions & 19 deletions

File tree

.github/workflows/publish.yaml

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,96 @@
11
name: Publish
22

33
on:
4-
push:
5-
tags:
6-
- '*'
4+
workflow_dispatch:
5+
inputs:
6+
tag:
7+
description: Release tag (e.g. v42.0.0)
8+
required: true
9+
10+
permissions:
11+
contents: write
712

813
jobs:
9-
build:
10-
name: Publish CPM.cmake
14+
publish:
1115
runs-on: ubuntu-latest
16+
1217
steps:
13-
- uses: actions/checkout@v6
18+
- name: Checkout repository
19+
uses: actions/checkout@v6
20+
21+
- name: Validate publish request
22+
env:
23+
TAG: ${{ inputs.tag }}
24+
run: |
25+
set -euo pipefail
26+
27+
# validate dispatch branch
28+
if [[ "${GITHUB_REF}" != "refs/heads/master" ]]; then
29+
echo "Error: publish workflow must be dispatched from 'master'. Current ref: ${GITHUB_REF}" >&2
30+
exit 1
31+
fi
32+
33+
# validate tag input
34+
if [[ -z "$TAG" ]]; then
35+
echo "Error: tag input is empty" >&2
36+
exit 1
37+
fi
38+
39+
# validate tag format (SemVer with leading "v")
40+
if ! [[ "$TAG" =~ ^v[0-9]+(\.[0-9]+){2}(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
41+
echo "Error: tag must be a SemVer starting with 'v' (e.g., v42.0.0, v42.0.0-rc.1, or v42.0.0-rc.1+build.1)" >&2
42+
exit 1
43+
fi
44+
45+
git fetch --tags
46+
47+
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
48+
if [[ "$(git rev-parse "$TAG^{commit}")" != "$(git rev-parse HEAD)" ]]; then
49+
echo "Error: tag '$TAG' already exists and points to a different commit." >&2
50+
exit 1
51+
fi
52+
fi
1453
15-
- name: Set CPM version by tag
54+
- name: Create tag
55+
env:
56+
TAG: ${{ inputs.tag }}
1657
run: |
58+
set -euo pipefail
59+
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
60+
echo "Tag '$TAG' already exists; reusing it." >&2
61+
else
62+
git tag "$TAG"
63+
fi
64+
65+
- name: Build CPM.cmake
66+
env:
67+
TAG: ${{ inputs.tag }}
68+
run: |
69+
set -euo pipefail
1770
mkdir dist
18-
sed -e "s/1.0.0-development-version/${GITHUB_REF/refs\/tags\/v}/g" cmake/CPM.cmake > dist/CPM.cmake
19-
sed -e "s/1.0.0-development-version/${GITHUB_REF/refs\/tags\/v}/g" \
20-
-e "s/CPM_HASH_SUM_PLACEHOLDER/$(sha256sum dist/CPM.cmake | cut -d' ' -f1)/" cmake/get_cpm.cmake > dist/get_cpm.cmake
21-
22-
- name: Upload CPM.cmake to release
23-
uses: svenstaro/upload-release-action@29e53e917877a24fad85510ded594ab3c9ca12de # 2.11.5
24-
with:
25-
repo_token: ${{ secrets.GITHUB_TOKEN }}
26-
tag: ${{ github.ref }}
27-
file: dist/*.cmake
28-
file_glob: true
29-
overwrite: true
71+
VERSION="${TAG#v}"
72+
73+
sed \
74+
-e "s/1.0.0-development-version/${VERSION}/g" \
75+
cmake/CPM.cmake > dist/CPM.cmake
76+
77+
sed \
78+
-e "s/1.0.0-development-version/${VERSION}/g" \
79+
-e "s/CPM_HASH_SUM_PLACEHOLDER/$(sha256sum dist/CPM.cmake | cut -d' ' -f1)/" \
80+
cmake/get_cpm.cmake > dist/get_cpm.cmake
81+
82+
- name: Create GitHub release
83+
env:
84+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
GH_TAG: ${{ inputs.tag }}
86+
run: |
87+
set -euo pipefail
88+
if gh release view "$GH_TAG" >/dev/null 2>&1; then
89+
echo "Error: GitHub release '$GH_TAG' already exists. Because releases/assets are immutable, choose a new tag or delete the existing release before re-running." >&2
90+
exit 1
91+
fi
92+
git push origin "refs/tags/$GH_TAG"
93+
gh release create "$GH_TAG" \
94+
dist/*.cmake \
95+
--title "$GH_TAG" \
96+
--generate-notes

0 commit comments

Comments
 (0)