Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit 8eba9e4

Browse files
authored
Merge pull request #20 from jmkcoder/github-action-upgrade
[Skip] - Refactor GitHub Actions workflows and scripts for improved maintainab…
2 parents d91f814 + a06d758 commit 8eba9e4

10 files changed

Lines changed: 123 additions & 129 deletions

File tree

.github/scripts/check-skip.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
4+
pr_title="$1"
5+
6+
if [[ "$pr_title" == "[Skip]"* ]]; then
7+
echo "skip=true" >> $GITHUB_ENV
8+
else
9+
echo "skip=false" >> $GITHUB_ENV
10+
fi
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -e
3+
4+
new_tag="$1"
5+
6+
if git rev-parse "refs/tags/$new_tag" >/dev/null 2>&1; then
7+
echo "Tag $new_tag already exists."
8+
exit 0
9+
fi

.github/scripts/create-package.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
4+
mkdir temp_package
5+
cp -r dist/. temp_package/
6+
cp package.json temp_package/
7+
cp README.md temp_package/
8+
cp LICENSE temp_package/
9+
cd temp_package
10+
npm pack

.github/scripts/create-tag.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -e
3+
4+
new_tag="$1"
5+
6+
git config user.name "github-actions[bot]"
7+
git config user.email "github-actions[bot]@users.noreply.github.com"
8+
git tag $new_tag
9+
git push origin $new_tag

.github/scripts/delete-release.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -e
3+
4+
tag="$1"
5+
github_token="$2"
6+
repo="$3"
7+
8+
git config user.name "github-actions[bot]"
9+
git config user.email "github-actions[bot]@users.noreply.github.com"
10+
11+
# Delete Git Tag
12+
git tag -d $tag
13+
git push origin :refs/tags/$tag
14+
15+
# Delete GitHub Release
16+
release_id=$(curl -s -H "Authorization: token $github_token" \
17+
-H "Accept: application/vnd.github.v3+json" \
18+
"https://api.github.com/repos/$repo/releases/tags/$tag" \
19+
| jq -r '.id')
20+
curl -X DELETE -H "Authorization: token $github_token" \
21+
-H "Accept: application/vnd.github.v3+json" \
22+
"https://api.github.com/repos/$repo/releases/$release_id"

.github/scripts/get-latest-tag.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -e
3+
4+
latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null)
5+
if [ -z "$latest_tag" ]; then
6+
latest_tag="v0.0.0"
7+
fi
8+
echo "latest_tag=$latest_tag" >> $GITHUB_ENV

.github/scripts/setup-npmrc.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
npm_token="$1"
5+
6+
echo "//registry.npmjs.org/:_authToken=$npm_token" > ~/.npmrc
7+
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc

.github/scripts/verify-pr-title.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
set -e
3+
4+
pr_title="$1"
5+
latest_tag="$2"
6+
7+
# Extract major, minor, and patch components from the latest tag
8+
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
9+
minor=$(echo $latest_tag | cut -d. -f2)
10+
patch=$(echo $latest_tag | cut -d. -f3)
11+
12+
# Determine the increment type based on PR title
13+
if [[ "$pr_title" == "[Major]"* ]]; then
14+
major=$((major + 1))
15+
minor=0
16+
patch=0
17+
elif [[ "$pr_title" == "[Minor]"* ]]; then
18+
minor=$((minor + 1))
19+
patch=0
20+
elif [[ "$pr_title" == "[Patch]"* ]]; then
21+
patch=$((patch + 1))
22+
else
23+
echo "PR title must start with [Major], [Minor], or [Patch]."
24+
exit 1
25+
fi
26+
27+
# Construct the new tag
28+
new_tag="v${major}.${minor}.${patch}"
29+
echo "new_tag=$new_tag" >> $GITHUB_ENV

.github/workflows/build.yml

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
name: Build
22

33
on:
44
workflow_call:
@@ -10,52 +10,19 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
1318
- name: Check for [skip] in PR title
1419
id: check_skip
15-
run: |
16-
pr_title="${{ github.event.pull_request.title }}"
17-
18-
if [[ "$pr_title" == "[Skip]"* ]]; then
19-
echo "skip=true" >> $GITHUB_ENV
20-
else
21-
echo "skip=false" >> $GITHUB_ENV
22-
fi
23-
20+
run: bash .github/scripts/check-skip.sh "${{ github.event.pull_request.title }}"
21+
2422
- name: Verify PR title
2523
if: env.skip == 'false'
2624
id: determine_increment
27-
run: |
28-
pr_title="${{ github.event.pull_request.title }}"
29-
latest_tag=${{ env.latest_tag }}
30-
31-
# Extract major, minor, and patch components from the latest tag
32-
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
33-
minor=$(echo $latest_tag | cut -d. -f2)
34-
patch=$(echo $latest_tag | cut -d. -f3)
35-
36-
# Determine the increment type based on PR title
37-
if [[ "$pr_title" == "[Major]"* ]]; then
38-
major=$((major + 1))
39-
minor=0
40-
patch=0
41-
elif [[ "$pr_title" == "[Minor]"* ]]; then
42-
minor=$((minor + 1))
43-
patch=0
44-
elif [[ "$pr_title" == "[Patch]"* ]]; then
45-
patch=$((patch + 1))
46-
else
47-
echo "PR title must start with [Major], [Minor], or [Patch]."
48-
exit 1
49-
fi
50-
51-
# Construct the new tag
52-
new_tag="v${major}.${minor}.${patch}"
53-
echo "new_tag=$new_tag" >> $GITHUB_ENV
54-
55-
- name: Checkout code
56-
uses: actions/checkout@v4
57-
with:
58-
fetch-depth: 0
25+
run: bash .github/scripts/verify-pr-title.sh "${{ github.event.pull_request.title }}" "${{ env.latest_tag }}"
5926

6027
- name: Set up Node.js
6128
uses: actions/setup-node@v4
@@ -69,6 +36,7 @@ jobs:
6936
run: npm run build
7037

7138
- name: Upload build artifacts
39+
if: github.event.pull_request.merged == true
7240
uses: actions/upload-artifact@v4
7341
with:
7442
name: dist

.github/workflows/main.yml

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -36,75 +36,25 @@ jobs:
3636

3737
- name: Get the latest tag
3838
id: get_tag
39-
run: |
40-
latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null)
41-
if [ -z "$latest_tag" ]; then
42-
latest_tag="v0.0.0"
43-
fi
44-
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
39+
run: bash .github/scripts/get-latest-tag.sh
4540

4641
- name: Check for [skip] in PR title
4742
id: check_skip
48-
run: |
49-
pr_title="${{ github.event.pull_request.title }}"
50-
51-
if [[ "$pr_title" == "[Skip]"* ]]; then
52-
echo "skip=true" >> $GITHUB_ENV
53-
else
54-
echo "skip=false" >> $GITHUB_ENV
55-
fi
43+
run: bash .github/scripts/check-skip.sh "${{ github.event.pull_request.title }}"
5644

5745
- name: Determine version increment
5846
if: env.skip == 'false'
5947
id: determine_increment
60-
run: |
61-
pr_title="${{ github.event.pull_request.title }}"
62-
latest_tag=${{ env.latest_tag }}
63-
64-
# Extract major, minor, and patch components from the latest tag
65-
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
66-
minor=$(echo $latest_tag | cut -d. -f2)
67-
patch=$(echo $latest_tag | cut -d. -f3)
68-
69-
# Determine the increment type based on PR title
70-
if [[ "$pr_title" == "[Major]"* ]]; then
71-
major=$((major + 1))
72-
minor=0
73-
patch=0
74-
elif [[ "$pr_title" == "[Minor]"* ]]; then
75-
minor=$((minor + 1))
76-
patch=0
77-
elif [[ "$pr_title" == "[Patch]"* ]]; then
78-
patch=$((patch + 1))
79-
else
80-
echo "PR title must start with [Major], [Minor], or [Patch]."
81-
exit 1
82-
fi
83-
84-
# Construct the new tag
85-
new_tag="v${major}.${minor}.${patch}"
86-
echo "new_tag=$new_tag" >> $GITHUB_ENV
87-
88-
- name: Configure Git User
89-
if: env.skip == 'false'
90-
run: |
91-
git config user.name "github-actions[bot]"
92-
git config user.email "github-actions[bot]@users.noreply.github.com"
48+
run: bash .github/scripts/verify-pr-title.sh "${{ github.event.pull_request.title }}" "${{ env.latest_tag }}"
9349

9450
- name: Check if tag exists on remote
9551
id: check_tag
96-
run: |
97-
if git rev-parse "refs/tags/${{ env.new_tag }}" >/dev/null 2>&1; then
98-
echo "Tag ${{ env.new_tag }} already exists."
99-
exit 0
100-
fi
52+
run: bash .github/scripts/check-tag-exists.sh "${{ env.new_tag }}"
10153

10254
- name: Create new tag
10355
if: env.skip == 'false' && steps.check_tag.outcome == 'success'
10456
id: create_tag
105-
run: |
106-
git tag ${{ env.new_tag }}
107-
git push origin ${{ env.new_tag }}
57+
run: bash .github/scripts/create-tag.sh "${{ env.new_tag }}"
10858

10959
- name: Create GitHub Release
11060
if: env.skip == 'false' && steps.create_tag.outcome == 'success'
@@ -131,20 +81,11 @@ jobs:
13181

13282
- name: Set up .npmrc
13383
if: env.skip == 'false'
134-
run: |
135-
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_KEY }}" > ~/.npmrc
136-
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc
84+
run: bash .github/scripts/setup-npmrc.sh "${{ secrets.NPM_PUBLISH_KEY }}"
13785

13886
- name: Create temp_package
13987
if: env.skip == 'false'
140-
run: |
141-
mkdir temp_package
142-
cp -r dist/. temp_package/ # Copy all contents of dist, including hidden files
143-
cp package.json temp_package/
144-
cp README.md temp_package/
145-
cp LICENSE temp_package/
146-
cd temp_package
147-
npm pack # Create a tarball from the contents
88+
run: bash .github/scripts/create-package.sh
14889

14990
- name: Publish to NPM
15091
if: env.skip == 'false'
@@ -172,29 +113,10 @@ jobs:
172113
with:
173114
name: failed-tag
174115

175-
- name: Configure Git User
176-
run: |
177-
git config user.name "github-actions[bot]"
178-
git config user.email "github-actions[bot]@users.noreply.github.com"
179-
180-
- name: Delete Git Tag
181-
run: |
182-
tag=$(cat failed-tag)
183-
git tag -d $tag
184-
git push origin :refs/tags/$tag
185-
env:
186-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
187-
188-
- name: Delete GitHub Release
116+
- name: Delete release and tag
189117
run: |
190118
tag=$(cat failed-tag)
191-
release_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
192-
-H "Accept: application/vnd.github.v3+json" \
193-
"https://api.github.com/repos/${{ github.repository }}/releases/tags/$tag" \
194-
| jq -r '.id')
195-
curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
196-
-H "Accept: application/vnd.github.v3+json" \
197-
"https://api.github.com/repos/${{ github.repository }}/releases/$release_id"
119+
bash .github/scripts/delete-release.sh "$tag" "${{ secrets.GITHUB_TOKEN }}" "${{ github.repository }}"
198120
env:
199121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
200122

0 commit comments

Comments
 (0)