Skip to content

Commit 10aa3f8

Browse files
committed
ci: improve release workflow
Add `verify_release` step which will check if a tag exists or not to perform a release only when there is a commit title containing `chore(release)`
1 parent 3a36b3d commit 10aa3f8

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

.github/workflows/release.yml

+47-31
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,51 @@ name: Release
33
on:
44
push:
55
branches:
6+
- main
67
- next
78

89
jobs:
10+
verify_release:
11+
name: Verify release 🔍
12+
if: startsWith(github.event.head_commit.message, 'chore(release)')
13+
runs-on: ubuntu-latest
14+
outputs:
15+
version: ${{ steps.check_tag.outputs.version }}
16+
is_prerelease: ${{ steps.check_tag.outputs.is_prerelease }}
17+
tag_exists: ${{ steps.check_tag.outputs.tag_exists }}
18+
19+
steps:
20+
- name: Checkout 🛎️
21+
uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
25+
- name: Check tag details 🔍
26+
id: check_tag
27+
run: |
28+
VERSION=$(node -p "require('./package.json').version")
29+
TAG="v$VERSION"
30+
31+
if [[ "$VERSION" == *rc* ]] || [[ "$VERSION" == *next* ]] || [[ "$VERSION" == *alpha* ]] || [[ "$VERSION" == *beta* ]]; then
32+
echo "is_prerelease=true" >> $GITHUB_OUTPUT
33+
echo "This is a prerelease version"
34+
else
35+
echo "is_prerelease=false" >> $GITHUB_OUTPUT
36+
echo "This is a stable release version"
37+
fi
38+
39+
if [ $(git tag -l "$TAG") ]; then
40+
echo "Tag $TAG already exists"
41+
echo "tag_exists=true" >> $GITHUB_OUTPUT
42+
else
43+
echo "Tag $TAG does not exist"
44+
echo "tag_exists=false" >> $GITHUB_OUTPUT
45+
fi
46+
947
release_to_npm:
48+
name: Release to NPM 🚀
49+
needs: verify_release
50+
if: needs.verify_release.outputs.tag_exists != 'true'
1051
permissions:
1152
contents: write
1253
runs-on: ubuntu-latest
@@ -35,65 +76,40 @@ jobs:
3576
restore-keys: |
3677
${{ inputs.os }}-${{ inputs.node }}-release
3778
38-
- id: version_check
39-
name: Identify new version 🔍
40-
run: |
41-
PKG_VERSION=$(node -p "require('./package.json').version")
42-
PKG_NAME=$(node -p "require('./package.json').name")
43-
PUBLISHED_VERSION=$(npm view $PKG_NAME@next version 2>/dev/null || npm view $PKG_NAME version 2>/dev/null || echo "0.0.0")
44-
echo "version=$PKG_VERSION" >> $GITHUB_OUTPUT
45-
if [ "$PKG_VERSION" = "$PUBLISHED_VERSION" ]; then
46-
echo "is_new_version=false" >> $GITHUB_OUTPUT
47-
else
48-
echo "is_new_version=true" >> $GITHUB_OUTPUT
49-
fi
50-
51-
- id: release_type
52-
name: Identify release type 🔍
53-
if: ${{ steps.version_check.outputs.is_new_version == 'true' }}
54-
run: echo "is_latest=$LATEST" >> $GITHUB_OUTPUT
55-
env:
56-
# Check on current `version` from the step above to decide whether to publish to `latest`/`next` npm tag
57-
LATEST: ${{ contains(steps.version_check.outputs.version, '-next') != true && contains(steps.version_check.outputs.version, '-rc') != true }}
58-
59-
- id: create_tag
60-
name: Create and push tag 🔖
61-
if: ${{ steps.version_check.outputs.is_new_version == 'true' }}
79+
- name: Create and push tag 🔖
80+
id: create_tag
6281
run: |
63-
TAG_NAME="v${{ steps.version_check.outputs.version }}"
82+
TAG_NAME="v${{ needs.verify_release.outputs.version }}"
6483
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
6584
git tag $TAG_NAME
6685
git push origin $TAG_NAME
6786
env:
6887
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6988

7089
- name: Create release 🚢
71-
if: ${{ steps.version_check.outputs.is_new_version == 'true' }}
7290
uses: softprops/action-gh-release@v2
7391
env:
7492
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7593
with:
7694
tag_name: ${{ steps.create_tag.outputs.tag_name }}
7795
body: Please refer to [CHANGELOG.md](https://github.com/thymikee/jest-preset-angular/blob/${{ steps.create_tag.outputs.tag_name }}/CHANGELOG.md) for details.
7896
draft: false
79-
prerelease: ${{ steps.release_type.outputs.is_latest != 'true' }}
97+
prerelease: ${{ needs.verify_release.outputs.is_prerelease == 'true' }}
8098

8199
- name: Install 🔧
82-
if: ${{ steps.version_check.outputs.is_new_version == 'true' }}
83100
run: yarn --immutable
84101

85102
- name: Build 🔨
86-
if: ${{ steps.version_check.outputs.is_new_version == 'true' }}
87103
run: yarn build
88104

89105
- name: Publish with latest tag 🚀
90-
if: ${{ steps.version_check.outputs.is_new_version == 'true' && steps.release_type.outputs.is_latest == 'true' }}
106+
if: needs.verify-release.outputs.is_prerelease != 'true'
91107
run: npm publish --access public
92108
env:
93109
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
94110

95111
- name: Publish with next tag 🚀
96-
if: ${{ steps.version_check.outputs.is_new_version == 'true' && steps.release_type.outputs.is_latest != 'true' }}
112+
if: needs.verify_release.outputs.is_prerelease == 'true'
97113
run: npm publish --access public --tag next
98114
env:
99115
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)