diff --git a/.github/scripts/check-skip.sh b/.github/scripts/check-skip.sh new file mode 100644 index 0000000..88eb488 --- /dev/null +++ b/.github/scripts/check-skip.sh @@ -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 diff --git a/.github/scripts/check-tag-exists.sh b/.github/scripts/check-tag-exists.sh new file mode 100644 index 0000000..d978dd4 --- /dev/null +++ b/.github/scripts/check-tag-exists.sh @@ -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 diff --git a/.github/scripts/create-package.sh b/.github/scripts/create-package.sh new file mode 100644 index 0000000..a64aaea --- /dev/null +++ b/.github/scripts/create-package.sh @@ -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 diff --git a/.github/scripts/create-tag.sh b/.github/scripts/create-tag.sh new file mode 100644 index 0000000..93a724a --- /dev/null +++ b/.github/scripts/create-tag.sh @@ -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 diff --git a/.github/scripts/delete-release.sh b/.github/scripts/delete-release.sh new file mode 100644 index 0000000..f00af96 --- /dev/null +++ b/.github/scripts/delete-release.sh @@ -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" diff --git a/.github/scripts/get-latest-tag.sh b/.github/scripts/get-latest-tag.sh new file mode 100644 index 0000000..d25ced5 --- /dev/null +++ b/.github/scripts/get-latest-tag.sh @@ -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 diff --git a/.github/scripts/setup-npmrc.sh b/.github/scripts/setup-npmrc.sh new file mode 100644 index 0000000..dbabceb --- /dev/null +++ b/.github/scripts/setup-npmrc.sh @@ -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 diff --git a/.github/scripts/verify-pr-title.sh b/.github/scripts/verify-pr-title.sh new file mode 100644 index 0000000..45db66e --- /dev/null +++ b/.github/scripts/verify-pr-title.sh @@ -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 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d9a1642..b246a1b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ - +name: Build on: workflow_call: @@ -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 @@ -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 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0d4f461..72c7cbf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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' @@ -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' @@ -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 }}