Skip to content

Commit 08a5169

Browse files
committed
chore: Custom release workflow
1 parent 917c432 commit 08a5169

File tree

8 files changed

+349
-2386
lines changed

8 files changed

+349
-2386
lines changed

.github/scripts/release.sh

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Change directory to the root of the project
2+
cd "$(git rev-parse --show-toplevel)"
3+
4+
echo "=========================================================="
5+
echo "= ="
6+
echo "=========================================================="
7+
8+
# Declerations
9+
RELEASE_CONFIG_FILE=null
10+
RELEASE_TYPE=null
11+
TAG=null
12+
13+
PACKAGEJSON=$(cat package.json)
14+
PACKAGE_NAME="$(echo $PACKAGEJSON | jq -r '.name')"
15+
PACKAGE_VERSION="$(echo $PACKAGEJSON | jq -r '.version')"
16+
PACKAGE_NPM_VERSION=null
17+
FINAL_RELEASE_VERSION=null
18+
19+
# Check if release config file is available
20+
echo "- reading config.json file"
21+
if [ -f ".release/config.json" ]; then
22+
echo "- '.release/config.json' file found"
23+
RELEASE_CONFIG_FILE=$(cat .release/config.json)
24+
else
25+
echo "- '.release/config.json' file not found"
26+
echo "- abort"
27+
exit 1
28+
fi
29+
30+
# Check if release config file has `versionUpgradeType`
31+
echo "- reading 'versionUpgradeType'"
32+
if [ "$(echo "$RELEASE_CONFIG_FILE" | jq -r ".versionUpgradeType")" != "null" ]; then
33+
RELEASE_TYPE=$(echo "$RELEASE_CONFIG_FILE" | jq -r ".versionUpgradeType")
34+
echo "- 'versionUpgradeType': $RELEASE_TYPE"
35+
else
36+
RELEASE_TYPE=null
37+
echo "- no 'versionUpgradeType' provided"
38+
echo "- abort"
39+
exit 1
40+
fi
41+
42+
# Check if npm view command succeeded or failed
43+
echo "- reading package version from npm"
44+
PACKAGE_NPM_VERSION=$(npm view $PACKAGE_NAME version 2>/dev/null)
45+
if [ $? -eq 0 ]; then
46+
echo "- package npm version: $PACKAGE_NPM_VERSION"
47+
# if [ "$PACKAGE_VERSION" == "$PACKAGE_NPM_VERSION" ]; then
48+
# echo "NPM version and package version matched."
49+
# fi
50+
else
51+
echo "- no npm version of the package found"
52+
# FIRST_RELEASE=true
53+
PACKAGE_NPM_VERSION=null
54+
FINAL_RELEASE_VERSION=$PACKAGE_VERSION
55+
fi
56+
57+
# Function to update version based on RELEASE_TYPE
58+
update_version() {
59+
local current_version=$1
60+
local release_type=$2
61+
local major
62+
local minor
63+
local patch
64+
65+
IFS='.' read -r major minor patch <<<"$current_version"
66+
67+
case $release_type in
68+
"major")
69+
echo "$(($major + 1)).0.0"
70+
;;
71+
"minor")
72+
echo "$major.$(($minor + 1)).0"
73+
;;
74+
"patch")
75+
echo "$major.$minor.$(($patch + 1))"
76+
;;
77+
*)
78+
echo "- invalid release type: $release_type"
79+
;;
80+
esac
81+
}
82+
83+
# Logic for FINAL_RELEASE_VERSION
84+
if [ "$PACKAGE_NPM_VERSION" == "null" ]; then
85+
FINAL_RELEASE_VERSION=$PACKAGE_VERSION
86+
else
87+
case $RELEASE_TYPE in
88+
"major" | "minor" | "patch")
89+
echo "- analysing version"
90+
FINAL_RELEASE_VERSION=$(update_version "$PACKAGE_NPM_VERSION" "$RELEASE_TYPE") TAG=$FINAL_RELEASE_VERSION
91+
echo "- analysed deployable version $FINAL_RELEASE_VERSION"
92+
;;
93+
*)
94+
echo "- invalid release type: $RELEASE_TYPE"
95+
;;
96+
esac
97+
fi
98+
99+
# Logic for TAG
100+
echo "- reading custom tag"
101+
if [ "$(echo "$RELEASE_CONFIG_FILE" | jq -r ".tag")" != "null" ]; then
102+
TAG=$(echo "$RELEASE_CONFIG_FILE" | jq -r ".tag")
103+
echo "- custom tag detected $TAG"
104+
else
105+
echo "- no custom tag found"
106+
TAG="v-$FINAL_RELEASE_VERSION"
107+
fi
108+
109+
# Check if release notes are available
110+
echo "- reading release notes"
111+
RELEASE_NOTES=$(cat .release/release-notes.md)
112+
113+
if [ $? != 0 ]; then
114+
echo "- release notes not provided"
115+
RELEASE_NOTES=null
116+
# exit 1
117+
fi
118+
119+
echo "---"
120+
echo "- RELEASE_NOTES: $RELEASE_NOTES"
121+
echo "- PACKAGE_NAME: $PACKAGE_NAME"
122+
echo "- PACKAGE_VERSION: $PACKAGE_VERSION"
123+
echo "- FINAL_RELEASE_VERSION: $FINAL_RELEASE_VERSION"
124+
echo "- RELEASE_TYPE:$RELEASE_TYPE"
125+
echo "- TAG:$TAG"
126+
127+
# Set output
128+
echo "tag=$(echo $TAG)" >>$GITHUB_OUTPUT
129+
echo "release-notes=$(echo $RELEASE_NOTES)" >>$GITHUB_OUTPUT
130+
echo "final-release-version=$(echo $FINAL_RELEASE_VERSION)" >>$GITHUB_OUTPUT
131+
echo "release-type=$(echo $RELEASE_TYPE)" >>$GITHUB_OUTPUT
132+
echo "package-version=$(echo $PACKAGE_VERSION)" >>$GITHUB_OUTPUT

.github/workflows/release.yaml

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Deploy and Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- .release/*
8+
permissions:
9+
contents: write
10+
jobs:
11+
release:
12+
permissions: write-all
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install jq
20+
run: |
21+
sudo apt-get update
22+
sudo apt-get install jq
23+
24+
- name: Setup node version
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: '*'
28+
registry-url: 'https://registry.npmjs.org'
29+
30+
- name: Read release props
31+
id: read-release-props
32+
run: bash .github/scripts/release.sh 2>&1 | tee -a get-release-props-logs.log
33+
34+
- name: Configure git
35+
run: |
36+
USER_NAME='laxmanpokhel_actions'
37+
USER_EMAIL='laxmanpokhrel@users.noreply.github.com'
38+
39+
# Set the git configs
40+
git config --global user.name $USER_NAME
41+
git config --global user.email $USER_EMAIL
42+
43+
- name: Commit logs
44+
run: |
45+
git add .
46+
git commit -m "ci: Release props logs generated."
47+
48+
- name: Upload log file
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: get-release-props-logs
52+
path: get-release-props-logs.log
53+
54+
- name: Debug variables
55+
run: |
56+
echo "tag=${{steps.read-release-props.outputs.tag}}"
57+
echo "release-notes=${{steps.read-release-props.outputs.release-notes}}"
58+
echo "package-version: ${{steps.read-release-props.outputs.package-version}}"
59+
echo "release-type: ${{steps.read-release-props.outputs.release-type}}"
60+
echo "final-release-version: ${{steps.read-release-props.outputs.final-release-version}}"
61+
echo "final-release-version: ${{steps.read-release-props.outputs.final-release-version}}"
62+
63+
- name: Checkout to new branch and update package json with new version
64+
# if: steps.read-release-props.outcome.package-version != steps.read-release-props.outputs.final-release-version
65+
# continue-on-error: true
66+
id: branch-checkout-details
67+
run: |
68+
# Read current branch
69+
current_branch=$(git rev-parse --abbrev-ref HEAD)
70+
71+
# Create a checkout branch name
72+
checkout_branch_name=release-${{steps.read-release-props.outputs.final-release-version}}
73+
74+
# Checkout to new branch
75+
git checkout -b $checkout_branch_name
76+
77+
# Update the version in package.json, and commit & tag the change:
78+
npm version ${{steps.read-release-props.outputs.final-release-version}} -m "chore(Github Actions-$(date -u +"%Y-%m-%d %H:%M:%S")): Deploy package to NPM"
79+
80+
# Set output
81+
echo "current_branch=$(echo $current_branch)" >>$GITHUB_OUTPUT
82+
echo "checkout_branch_name=$(echo $checkout_branch_name)" >>$GITHUB_OUTPUT
83+
84+
- name: Check branch
85+
run: git branch
86+
87+
- name: Install package
88+
run: yarn install
89+
90+
- name: Build package
91+
run: yarn build
92+
93+
- name: Publish package
94+
env:
95+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
96+
run: |
97+
npm pkg fix
98+
npm publish
99+
100+
- name: Create release
101+
env:
102+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103+
run: |
104+
tag="${{ steps.read-release-props.outputs.tag }}"
105+
title="${tag}"
106+
gh release create "$tag" \
107+
--repo="$GITHUB_REPOSITORY" \
108+
--title="$title" \
109+
--notes="${{ steps.read-release-props.outputs.release-notes }}"
110+
111+
- name: Push and raise a PR
112+
env:
113+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
114+
run: |
115+
# Push the updated version new checkout branch
116+
git push --set-upstream origin ${{steps.branch-checkout-details.outputs.checkout_branch_name}}
117+
118+
# Set the upstream branch for the current branch
119+
git branch --set-upstream-to=origin/${{ steps.branch-checkout-details.outputs.checkout_branch_name }} ${GITHUB_REF#refs/heads/}
120+
121+
# Create PR for the release
122+
gh pr create \
123+
--base ${{steps.branch-checkout-details.outputs.current_branch}} \
124+
--title "version ${{steps.read-release-props.outputs.final-release-version}}" \
125+
--body "* version: ${{steps.read-release-props.outputs.final-release-version}} | $(date -u +"%Y-%m-%d %H:%M:%S")" \
126+
--no-maintainer-edit

.github/workflows/release.yml

-42
This file was deleted.

.npmignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ignore source files
2+
src/
3+
*.js
4+
*.ts
5+
6+
# Ignore development tools and configuration files
7+
node_modules/
8+
.git/
9+
.editorconfig
10+
.eslintrc
11+
tsconfig.json
12+
.github
13+
.husky
14+
.release
15+
scripts
16+
src
17+
18+
.eslintignore
19+
.eslintrc.cjs
20+
.lintstagedrc
21+
.prettierrc
22+
.releaserc
23+
yarn.lock
24+
package-lock.json

.release/config.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"versionUpgradeType": "minor"
3+
}

.release/release-notes.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Improvements
2+
- Uses custom workflow to automate release and deploy workflow
3+
- Solves node version 18.18.0 restriction

0 commit comments

Comments
 (0)