Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/scripts/check-skip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

pr_title="$1"

if [[ "$pr_title" == "[Skip]"* ]]; then
echo "skip=true" >> $GITHUB_ENV
else
echo "skip=false" >> $GITHUB_ENV
fi
9 changes: 9 additions & 0 deletions .github/scripts/check-tag-exists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

new_tag="$1"

if git rev-parse "refs/tags/$new_tag" >/dev/null 2>&1; then
echo "Tag $new_tag already exists."
exit 0
fi
10 changes: 10 additions & 0 deletions .github/scripts/create-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

mkdir temp_package
cp -r dist/. temp_package/
cp package.json temp_package/
cp README.md temp_package/
cp LICENSE temp_package/
cd temp_package
npm pack
9 changes: 9 additions & 0 deletions .github/scripts/create-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

new_tag="$1"

git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag $new_tag
git push origin $new_tag
22 changes: 22 additions & 0 deletions .github/scripts/delete-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -e

tag="$1"
github_token="$2"
repo="$3"

git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

# Delete Git Tag
git tag -d $tag
git push origin :refs/tags/$tag

# Delete GitHub Release
release_id=$(curl -s -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$repo/releases/tags/$tag" \
| jq -r '.id')
curl -X DELETE -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$repo/releases/$release_id"
8 changes: 8 additions & 0 deletions .github/scripts/get-latest-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null)
if [ -z "$latest_tag" ]; then
latest_tag="v0.0.0"
fi
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
7 changes: 7 additions & 0 deletions .github/scripts/setup-npmrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

npm_token="$1"

echo "//registry.npmjs.org/:_authToken=$npm_token" > ~/.npmrc
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc
29 changes: 29 additions & 0 deletions .github/scripts/verify-pr-title.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
set -e

pr_title="$1"
latest_tag="$2"

# Extract major, minor, and patch components from the latest tag
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
minor=$(echo $latest_tag | cut -d. -f2)
patch=$(echo $latest_tag | cut -d. -f3)

# Determine the increment type based on PR title
if [[ "$pr_title" == "[Major]"* ]]; then
major=$((major + 1))
minor=0
patch=0
elif [[ "$pr_title" == "[Minor]"* ]]; then
minor=$((minor + 1))
patch=0
elif [[ "$pr_title" == "[Patch]"* ]]; then
patch=$((patch + 1))
else
echo "PR title must start with [Major], [Minor], or [Patch]."
exit 1
fi

# Construct the new tag
new_tag="v${major}.${minor}.${patch}"
echo "new_tag=$new_tag" >> $GITHUB_ENV
52 changes: 10 additions & 42 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

name: Build

on:
workflow_call:
Expand All @@ -10,52 +10,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check for [skip] in PR title
id: check_skip
run: |
pr_title="${{ github.event.pull_request.title }}"

if [[ "$pr_title" == "[Skip]"* ]]; then
echo "skip=true" >> $GITHUB_ENV
else
echo "skip=false" >> $GITHUB_ENV
fi

run: bash .github/scripts/check-skip.sh "${{ github.event.pull_request.title }}"

- name: Verify PR title
if: env.skip == 'false'
id: determine_increment
run: |
pr_title="${{ github.event.pull_request.title }}"
latest_tag=${{ env.latest_tag }}

# Extract major, minor, and patch components from the latest tag
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
minor=$(echo $latest_tag | cut -d. -f2)
patch=$(echo $latest_tag | cut -d. -f3)

# Determine the increment type based on PR title
if [[ "$pr_title" == "[Major]"* ]]; then
major=$((major + 1))
minor=0
patch=0
elif [[ "$pr_title" == "[Minor]"* ]]; then
minor=$((minor + 1))
patch=0
elif [[ "$pr_title" == "[Patch]"* ]]; then
patch=$((patch + 1))
else
echo "PR title must start with [Major], [Minor], or [Patch]."
exit 1
fi

# Construct the new tag
new_tag="v${major}.${minor}.${patch}"
echo "new_tag=$new_tag" >> $GITHUB_ENV

- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
run: bash .github/scripts/verify-pr-title.sh "${{ github.event.pull_request.title }}" "${{ env.latest_tag }}"

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

- name: Upload build artifacts
if: github.event.pull_request.merged == true
uses: actions/upload-artifact@v4
with:
name: dist
Expand Down
96 changes: 9 additions & 87 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,25 @@ jobs:

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

- name: Check for [skip] in PR title
id: check_skip
run: |
pr_title="${{ github.event.pull_request.title }}"

if [[ "$pr_title" == "[Skip]"* ]]; then
echo "skip=true" >> $GITHUB_ENV
else
echo "skip=false" >> $GITHUB_ENV
fi
run: bash .github/scripts/check-skip.sh "${{ github.event.pull_request.title }}"

- name: Determine version increment
if: env.skip == 'false'
id: determine_increment
run: |
pr_title="${{ github.event.pull_request.title }}"
latest_tag=${{ env.latest_tag }}

# Extract major, minor, and patch components from the latest tag
major=$(echo $latest_tag | cut -d. -f1 | sed 's/v//')
minor=$(echo $latest_tag | cut -d. -f2)
patch=$(echo $latest_tag | cut -d. -f3)

# Determine the increment type based on PR title
if [[ "$pr_title" == "[Major]"* ]]; then
major=$((major + 1))
minor=0
patch=0
elif [[ "$pr_title" == "[Minor]"* ]]; then
minor=$((minor + 1))
patch=0
elif [[ "$pr_title" == "[Patch]"* ]]; then
patch=$((patch + 1))
else
echo "PR title must start with [Major], [Minor], or [Patch]."
exit 1
fi

# Construct the new tag
new_tag="v${major}.${minor}.${patch}"
echo "new_tag=$new_tag" >> $GITHUB_ENV

- name: Configure Git User
if: env.skip == 'false'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
run: bash .github/scripts/verify-pr-title.sh "${{ github.event.pull_request.title }}" "${{ env.latest_tag }}"

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

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

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

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

- name: Create temp_package
if: env.skip == 'false'
run: |
mkdir temp_package
cp -r dist/. temp_package/ # Copy all contents of dist, including hidden files
cp package.json temp_package/
cp README.md temp_package/
cp LICENSE temp_package/
cd temp_package
npm pack # Create a tarball from the contents
run: bash .github/scripts/create-package.sh

- name: Publish to NPM
if: env.skip == 'false'
Expand Down Expand Up @@ -172,29 +113,10 @@ jobs:
with:
name: failed-tag

- name: Configure Git User
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

- name: Delete Git Tag
run: |
tag=$(cat failed-tag)
git tag -d $tag
git push origin :refs/tags/$tag
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Delete GitHub Release
- name: Delete release and tag
run: |
tag=$(cat failed-tag)
release_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/$tag" \
| jq -r '.id')
curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/releases/$release_id"
bash .github/scripts/delete-release.sh "$tag" "${{ secrets.GITHUB_TOKEN }}" "${{ github.repository }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
Loading