From 8373ebfde4accf1e6ba026e30ff2988dec167dfc Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 10:56:27 +0530 Subject: [PATCH 1/8] Add bash script to check if release exists --- .github/scripts/check-release.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 .github/scripts/check-release.sh diff --git a/.github/scripts/check-release.sh b/.github/scripts/check-release.sh new file mode 100755 index 0000000..320e810 --- /dev/null +++ b/.github/scripts/check-release.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +repo=$1 +release_version="v$2" +token=$3 + +echo "Checking release version ${release_version} exists or not." + +exists=$( + curl -s \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${token}" \ + https://api.github.com/repos/${repo}/releases/tags/${release_version} | + jq 'has("id")' +) + +if [[ "$exists" == true ]]; then + echo "::warning title=Skipping Release::Release ${release_version} already exists. If you want to create new release please update the version in package.json" + echo '::set-output name=release_exists::true' +else + echo '::set-output name=release_exists::false' +fi From 18826bc9df1f835bf4cc839b8ce4b77c0caa1d09 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 10:59:46 +0530 Subject: [PATCH 2/8] Add Release workflow --- .github/workflows/build-release.yaml | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .github/workflows/build-release.yaml diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml new file mode 100644 index 0000000..9213971 --- /dev/null +++ b/.github/workflows/build-release.yaml @@ -0,0 +1,82 @@ +name: Release + +on: + workflow_dispatch: + push: + branches: + - "main" + paths-ignore: + - "docs/**" + - README.md + - CHANGELOG.md + - .gitignore + +jobs: + build: + runs-on: ubuntu-latest + name: Build + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup node + uses: actions/setup-node@v3 + with: + node-version: 12 + + - name: Install dependencies + run: npm install + + - name: Build package + run: npm run build + + - name: Verify build + run: .github/scripts/verify-build.sh + + release-check: + runs-on: ubuntu-latest + needs: build + name: Release Precheck + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Get version + id: get-version + run: | + version=$(cat package.json | jq '.version' --raw-output) + echo "Version: ${version}" + echo "::set-output name=version::$version" + + - name: Check if release exists + id: release-check + run: | + .github/scripts/check-release.sh \ + ${{ github.repository }} \ + ${{ steps.get-version.outputs.version }} \ + ${{ secrets.GITHUB_TOKEN }} + + outputs: + version: ${{ steps.get-version.outputs.version }} + release_exists: ${{ steps.release-check.outputs.release_exists }} + + release: + runs-on: ubuntu-latest + needs: release-check + name: Release + if: needs.release-check.outputs.release_exists == 'false' + steps: + - name: Release + uses: softprops/action-gh-release@v1 + with: + name: v${{ needs.release-check.outputs.version }} + tag_name: v${{ needs.release-check.outputs.version }} + generate_release_notes: true + + major_version_update: + name: Major Release + needs: [release, release-check] + uses: ./.github/workflows/major-release.yaml + with: + TAG_NAME: v${{ needs.release-check.outputs.version }} From 9819c67e1be64565aa19714ffdb0fe5f102ebb8d Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 11:09:39 +0530 Subject: [PATCH 3/8] Refactor to add v prefix in version --- .github/scripts/check-release.sh | 2 +- .github/workflows/build-release.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/scripts/check-release.sh b/.github/scripts/check-release.sh index 320e810..45898f0 100755 --- a/.github/scripts/check-release.sh +++ b/.github/scripts/check-release.sh @@ -1,7 +1,7 @@ #!/bin/bash repo=$1 -release_version="v$2" +release_version=$2 token=$3 echo "Checking release version ${release_version} exists or not." diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 9213971..ab9b34f 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -46,8 +46,8 @@ jobs: id: get-version run: | version=$(cat package.json | jq '.version' --raw-output) - echo "Version: ${version}" - echo "::set-output name=version::$version" + echo "Version: v${version}" + echo "::set-output name=version::v${version}" - name: Check if release exists id: release-check @@ -70,8 +70,8 @@ jobs: - name: Release uses: softprops/action-gh-release@v1 with: - name: v${{ needs.release-check.outputs.version }} - tag_name: v${{ needs.release-check.outputs.version }} + name: ${{ needs.release-check.outputs.version }} + tag_name: ${{ needs.release-check.outputs.version } generate_release_notes: true major_version_update: @@ -79,4 +79,4 @@ jobs: needs: [release, release-check] uses: ./.github/workflows/major-release.yaml with: - TAG_NAME: v${{ needs.release-check.outputs.version }} + TAG_NAME: ${{ needs.release-check.outputs.version }} From a41b1d5a40b46f59eb910c5831e77864a0a6ba41 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 11:55:12 +0530 Subject: [PATCH 4/8] Add major release workflow --- .github/scripts/check-release.sh | 2 +- .github/scripts/source-tag-sha.sh | 14 ++++++ .github/scripts/update-major-tag.sh | 13 +++++ .github/workflows/build-release.yaml | 4 ++ .github/workflows/major-release.yaml | 74 ++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100755 .github/scripts/source-tag-sha.sh create mode 100755 .github/scripts/update-major-tag.sh create mode 100644 .github/workflows/major-release.yaml diff --git a/.github/scripts/check-release.sh b/.github/scripts/check-release.sh index 45898f0..1f43668 100755 --- a/.github/scripts/check-release.sh +++ b/.github/scripts/check-release.sh @@ -15,7 +15,7 @@ exists=$( ) if [[ "$exists" == true ]]; then - echo "::warning title=Skipping Release::Release ${release_version} already exists. If you want to create new release please update the version in package.json" + echo "Release ${release_version} already exists." echo '::set-output name=release_exists::true' else echo '::set-output name=release_exists::false' diff --git a/.github/scripts/source-tag-sha.sh b/.github/scripts/source-tag-sha.sh new file mode 100755 index 0000000..903a4b6 --- /dev/null +++ b/.github/scripts/source-tag-sha.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +repo=$1 +tag_name=$2 +token=$3 + +sha=$( + curl \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${token}" \ + https://api.github.com/repos/${repo}/git/ref/tags/${tag_name} | + jq '.object.sha' --raw-output +) +echo "::set-output name=sha::$sha" diff --git a/.github/scripts/update-major-tag.sh b/.github/scripts/update-major-tag.sh new file mode 100755 index 0000000..c5ec7c0 --- /dev/null +++ b/.github/scripts/update-major-tag.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +repo=$1 +source_tag_sha=$2 +major_version=$3 +token=$4 + +curl \ + -X PATCH -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${token}" \ + https://api.github.com/repos/${repo}/git/refs/tags/${major_version} \ + -d '{"sha": "'$source_tag_sha'", "force":true}' diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index ab9b34f..37bd950 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -57,6 +57,10 @@ jobs: ${{ steps.get-version.outputs.version }} \ ${{ secrets.GITHUB_TOKEN }} + - name: Display warning + if: steps.release-check.outputs.release_exists == 'true' + run: echo "::warning title=Skipping Release::Release ${release_version} already exists. If you want to create new release please update the version in package.json" + outputs: version: ${{ steps.get-version.outputs.version }} release_exists: ${{ steps.release-check.outputs.release_exists }} diff --git a/.github/workflows/major-release.yaml b/.github/workflows/major-release.yaml new file mode 100644 index 0000000..8437545 --- /dev/null +++ b/.github/workflows/major-release.yaml @@ -0,0 +1,74 @@ +name: Update Major Release + +on: + release: + types: + - published + + workflow_dispatch: + inputs: + TAG_NAME: + description: "Tag name that the major tag will point to" + required: true + type: string + + workflow_call: + inputs: + TAG_NAME: + description: "Tag name that the major tag will point to" + required: true + type: string + +env: + TAG_NAME: ${{ github.event.release.tag_name || inputs.TAG_NAME }} + +jobs: + update_major_tag: + runs-on: ubuntu-latest + name: Update Major Tag + environment: major-release-update + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Get major release version + id: get-major-version + run: | + echo "Tag name: ${TAG_NAME}" + major_version=${TAG_NAME%.*.*} + echo "Major Version: ${major_version}" + echo "::set-output name=major_version::$major_version" + + - name: Check if major version exists + id: check-release + run: | + .github/scripts/check-release.sh \ + ${{ github.repository }} \ + ${{ steps.get-major-version.outputs.major_version }} \ + ${{ secrets.GITHUB_TOKEN }} + + - name: Get Source Tag SHA + id: source-tag-sha + run: | + .github/scripts/check-release.sh \ + ${{ github.repository }} \ + ${TAG_NAME} \ + ${{ secrets.GITHUB_TOKEN }} + + - name: Create Major Release + uses: softprops/action-gh-release@v1 + if: steps.check-release.outputs.release_exists == 'false' + id: create_release + with: + name: ${{ steps.get-major-version.outputs.major_version }} + tag_name: ${{ steps.get-major-version.outputs.major_version }} + target_commitish: ${{ steps.source-tag-sha.outputs.sha }} + + - name: Update Major tag + if: steps.check-release.outputs.release_exists == 'true' + run: | + .github/scripts/check-release.sh \ + ${{ github.repository }} \ + ${{ steps.source-tag-sha.outputs.sha }} + ${{ steps.get-major-version.outputs.major_version }} + ${{ secrets.GITHUB_TOKEN }} From 710372f24b063e264371ad3fbdcca97cee151967 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 11:56:00 +0530 Subject: [PATCH 5/8] Temp run workflow on feature branch --- .github/workflows/build-release.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 37bd950..1b01807 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -5,6 +5,7 @@ on: push: branches: - "main" + - "feature/add-ci-cd" paths-ignore: - "docs/**" - README.md From ff062890785157a38a292cf8b04343d4e9703d84 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 11:56:57 +0530 Subject: [PATCH 6/8] Add missing } --- .github/workflows/build-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 1b01807..34678d3 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -76,7 +76,7 @@ jobs: uses: softprops/action-gh-release@v1 with: name: ${{ needs.release-check.outputs.version }} - tag_name: ${{ needs.release-check.outputs.version } + tag_name: ${{ needs.release-check.outputs.version }} generate_release_notes: true major_version_update: From 265cd12c350d42e1495b8a92912b8c9a8a4f59cb Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 12:33:19 +0530 Subject: [PATCH 7/8] Minor fixes --- .github/scripts/source-tag-sha.sh | 2 +- .github/scripts/update-major-tag.sh | 2 +- .github/workflows/build-release.yaml | 2 +- .github/workflows/major-release.yaml | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/scripts/source-tag-sha.sh b/.github/scripts/source-tag-sha.sh index 903a4b6..4deeb25 100755 --- a/.github/scripts/source-tag-sha.sh +++ b/.github/scripts/source-tag-sha.sh @@ -5,7 +5,7 @@ tag_name=$2 token=$3 sha=$( - curl \ + curl -s \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${token}" \ https://api.github.com/repos/${repo}/git/ref/tags/${tag_name} | diff --git a/.github/scripts/update-major-tag.sh b/.github/scripts/update-major-tag.sh index c5ec7c0..888d7e7 100755 --- a/.github/scripts/update-major-tag.sh +++ b/.github/scripts/update-major-tag.sh @@ -5,7 +5,7 @@ source_tag_sha=$2 major_version=$3 token=$4 -curl \ +curl -s \ -X PATCH -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${token}" \ diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 34678d3..7e55f8c 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -58,7 +58,7 @@ jobs: ${{ steps.get-version.outputs.version }} \ ${{ secrets.GITHUB_TOKEN }} - - name: Display warning + - name: Display warning if release exists if: steps.release-check.outputs.release_exists == 'true' run: echo "::warning title=Skipping Release::Release ${release_version} already exists. If you want to create new release please update the version in package.json" diff --git a/.github/workflows/major-release.yaml b/.github/workflows/major-release.yaml index 8437545..9466604 100644 --- a/.github/workflows/major-release.yaml +++ b/.github/workflows/major-release.yaml @@ -50,9 +50,9 @@ jobs: - name: Get Source Tag SHA id: source-tag-sha run: | - .github/scripts/check-release.sh \ + .github/scripts/source-tag-sha.sh \ ${{ github.repository }} \ - ${TAG_NAME} \ + ${{ steps.get-major-version.outputs.major_version }} \ ${{ secrets.GITHUB_TOKEN }} - name: Create Major Release @@ -67,8 +67,8 @@ jobs: - name: Update Major tag if: steps.check-release.outputs.release_exists == 'true' run: | - .github/scripts/check-release.sh \ + .github/scripts/update-major-tag.sh \ ${{ github.repository }} \ - ${{ steps.source-tag-sha.outputs.sha }} - ${{ steps.get-major-version.outputs.major_version }} + ${{ steps.source-tag-sha.outputs.sha }} \ + ${{ steps.get-major-version.outputs.major_version }} \ ${{ secrets.GITHUB_TOKEN }} From 440b77850dcf2686db9b0b6d83e542724e7c48d5 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Tue, 23 Aug 2022 12:33:57 +0530 Subject: [PATCH 8/8] Remove release run on feature branch --- .github/workflows/build-release.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build-release.yaml b/.github/workflows/build-release.yaml index 7e55f8c..636cfdc 100644 --- a/.github/workflows/build-release.yaml +++ b/.github/workflows/build-release.yaml @@ -5,7 +5,6 @@ on: push: branches: - "main" - - "feature/add-ci-cd" paths-ignore: - "docs/**" - README.md