Skip to content

Commit 929cebf

Browse files
authored
Update release workflow to check for existing releases
1 parent d91eaab commit 929cebf

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

.github/workflows/release.yml

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ on:
1414
jobs:
1515
build-and-release:
1616
runs-on: ubuntu-latest
17-
17+
# expose the release upload_url so downstream jobs can use it
18+
outputs:
19+
upload_url: ${{ steps.get_release.outputs.upload_url }}
20+
1821
steps:
1922
- name: Checkout
2023
uses: actions/checkout@v4
@@ -39,7 +42,24 @@ jobs:
3942
- name: Package
4043
run: npm run package
4144

45+
- name: Check if release exists
46+
id: check_release
47+
env:
48+
TAG: ${{ github.ref_name || github.event.inputs.version }}
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
run: |
51+
echo "Checking for release $TAG"
52+
resp=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" \
53+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/$TAG")
54+
if echo "$resp" | grep -q '"message": "Not Found"'; then
55+
echo "exists=false" >> $GITHUB_OUTPUT
56+
else
57+
echo "exists=true" >> $GITHUB_OUTPUT
58+
fi
59+
4260
- name: Create Release
61+
id: create_release
62+
if: steps.check_release.outputs.exists == 'false'
4363
uses: actions/create-release@v1
4464
env:
4565
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -67,12 +87,32 @@ jobs:
6787

6888
- name: Update major version tag
6989
run: |
90+
# Accept either "v1.2.3" or "1.2.3" and always push a "v<MAJOR>" tag (e.g. v1)
7091
VERSION=${{ github.ref_name || github.event.inputs.version }}
71-
MAJOR_VERSION=$(echo $VERSION | cut -d. -f1)
92+
STRIPPED=${VERSION#v}
93+
MAJOR_NUMBER=$(echo "$STRIPPED" | cut -d. -f1)
94+
MAJOR_TAG="v${MAJOR_NUMBER}"
7295
git config user.name github-actions
7396
git config user.email github-actions@github.com
74-
git tag -fa $MAJOR_VERSION -m "Update $MAJOR_VERSION tag"
75-
git push origin $MAJOR_VERSION --force
97+
git tag -fa "$MAJOR_TAG" -m "Update $MAJOR_TAG tag"
98+
git push origin "$MAJOR_TAG" --force
99+
100+
- name: Get release info (upload_url)
101+
id: get_release
102+
env:
103+
TAG: ${{ github.ref_name || github.event.inputs.version }}
104+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
run: |
106+
resp=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" \
107+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/$TAG")
108+
# extract upload_url using Python to avoid depending on jq
109+
upload_url=$(python - <<'PY'
110+
import sys, json
111+
data = json.load(sys.stdin)
112+
print(data.get("upload_url", ""))
113+
PY
114+
<<<"$resp")
115+
echo "upload_url=$upload_url" >> $GITHUB_OUTPUT
76116

77117
marketplace:
78118
needs: build-and-release
@@ -88,7 +128,7 @@ jobs:
88128
env:
89129
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90130
with:
91-
upload_url: ${{ steps.create_release.outputs.upload_url }}
131+
upload_url: ${{ needs.build-and-release.outputs.upload_url }}
92132
asset_path: ./action.yml
93133
asset_name: action.yml
94134
asset_content_type: text/yaml

0 commit comments

Comments
 (0)