Skip to content

Commit

Permalink
Merge pull request #2 from debugger24/feature/add-ci-cd
Browse files Browse the repository at this point in the history
Add CI/CD for release
  • Loading branch information
debugger24 authored Aug 23, 2022
2 parents ce0cf37 + 440b778 commit 522c8a4
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/scripts/check-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

repo=$1
release_version=$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 "Release ${release_version} already exists."
echo '::set-output name=release_exists::true'
else
echo '::set-output name=release_exists::false'
fi
14 changes: 14 additions & 0 deletions .github/scripts/source-tag-sha.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

repo=$1
tag_name=$2
token=$3

sha=$(
curl -s \
-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"
13 changes: 13 additions & 0 deletions .github/scripts/update-major-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

repo=$1
source_tag_sha=$2
major_version=$3
token=$4

curl -s \
-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}'
86 changes: 86 additions & 0 deletions .github/workflows/build-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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: v${version}"
echo "::set-output name=version::v${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 }}
- 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"

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: ${{ needs.release-check.outputs.version }}
tag_name: ${{ 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: ${{ needs.release-check.outputs.version }}
74 changes: 74 additions & 0 deletions .github/workflows/major-release.yaml
Original file line number Diff line number Diff line change
@@ -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/source-tag-sha.sh \
${{ github.repository }} \
${{ steps.get-major-version.outputs.major_version }} \
${{ 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/update-major-tag.sh \
${{ github.repository }} \
${{ steps.source-tag-sha.outputs.sha }} \
${{ steps.get-major-version.outputs.major_version }} \
${{ secrets.GITHUB_TOKEN }}

0 comments on commit 522c8a4

Please sign in to comment.