Skip to content

Commit

Permalink
Merge pull request #14 from laxmanpokhrel/dev
Browse files Browse the repository at this point in the history
major release with depencencies cleanup
  • Loading branch information
laxmanpokhrel authored Feb 27, 2024
2 parents ec1c8c8 + 5deee04 commit 1dc05b3
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 49 deletions.
147 changes: 147 additions & 0 deletions .github/scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Change directory to the root of the project
cd "$(git rev-parse --show-toplevel)"

echo "=========================================================="
echo "= ="
echo "=========================================================="

# Declerations
RELEASE_CONFIG_FILE=null
VERSION_UPGRADE_TYPE=null
RELEASE_TYPE=''
TAG=null

PACKAGEJSON=$(cat package.json)
PACKAGE_NAME="$(echo $PACKAGEJSON | jq -r '.name')"
PACKAGE_VERSION="$(echo $PACKAGEJSON | jq -r '.version')"
PACKAGE_NPM_VERSION=null
FINAL_RELEASE_VERSION=null

# Check if release config file is available
echo "- reading config.json file"
if [ -f ".release/config.json" ]; then
echo "- '.release/config.json' file found"
RELEASE_CONFIG_FILE=$(cat .release/config.json)
else
echo "- '.release/config.json' file not found"
echo "- abort"
exit 1
fi

# Check if release config file has `versionUpgradeType`
echo "- reading 'versionUpgradeType'"
if [ "$(echo "$RELEASE_CONFIG_FILE" | jq -r ".versionUpgradeType")" != "null" ]; then
VERSION_UPGRADE_TYPE=$(echo "$RELEASE_CONFIG_FILE" | jq -r ".versionUpgradeType")
echo "- 'versionUpgradeType': $VERSION_UPGRADE_TYPE"
else
VERSION_UPGRADE_TYPE=null
echo "- no 'versionUpgradeType' provided"
echo "- abort"
exit 1
fi

# Check if release config file has `releaseType`
echo "- reading 'releaseType'"
if [ "$(echo "$RELEASE_CONFIG_FILE" | jq -r ".releaseType")" != "null" ]; then
RELEASE_TYPE=$(echo "$RELEASE_CONFIG_FILE" | jq -r ".releaseType")
echo "- 'releaseType': $RELEASE_TYPE"
else
RELEASE_TYPE=''
echo "- no 'releaseType' provided"
# echo "- abort"
# exit 1
fi

# Check if npm view command succeeded or failed
echo "- reading package version from npm"
PACKAGE_NPM_VERSION=$(npm view $PACKAGE_NAME version 2>/dev/null)
if [ $? -eq 0 ]; then
echo "- package npm version: $PACKAGE_NPM_VERSION"
# if [ "$PACKAGE_VERSION" == "$PACKAGE_NPM_VERSION" ]; then
# echo "NPM version and package version matched."
# fi
else
echo "- no npm version of the package found"
# FIRST_RELEASE=true
PACKAGE_NPM_VERSION=null
FINAL_RELEASE_VERSION=$PACKAGE_VERSION
fi

# Function to update version based on VERSION_UPGRADE_TYPE
update_version() {
local current_version=$1
local release_type=$2
local major
local minor
local patch

IFS='.' read -r major minor patch <<<"$current_version"

case $release_type in
"major")
echo "$(($major + 1)).0.0"
;;
"minor")
echo "$major.$(($minor + 1)).0"
;;
"patch")
echo "$major.$minor.$(($patch + 1))"
;;
*)
echo "- invalid release type: $release_type"
;;
esac
}

# Logic for FINAL_RELEASE_VERSION
if [ "$PACKAGE_NPM_VERSION" == "null" ]; then
FINAL_RELEASE_VERSION=$PACKAGE_VERSION
else
case $VERSION_UPGRADE_TYPE in
"major" | "minor" | "patch")
echo "- analysing version"
FINAL_RELEASE_VERSION=$(update_version "$PACKAGE_NPM_VERSION" "$VERSION_UPGRADE_TYPE") TAG=$FINAL_RELEASE_VERSION
echo "- analysed deployable version $FINAL_RELEASE_VERSION"
;;
*)
echo "- invalid release type: $VERSION_UPGRADE_TYPE"
;;
esac
fi

# Logic for TAG
echo "- reading custom tag"
if [ "$(echo "$RELEASE_CONFIG_FILE" | jq -r ".tag")" != "null" ]; then
TAG=$(echo "$RELEASE_CONFIG_FILE" | jq -r ".tag")
echo "- custom tag detected $TAG"
else
echo "- no custom tag found"
TAG="v-$FINAL_RELEASE_VERSION"
fi

# Check if release notes are available
echo "- reading release notes"
RELEASE_NOTES=$(cat .release/release-notes.txt)

if [ $? != 0 ]; then
echo "- release notes not provided"
RELEASE_NOTES=null
# exit 1
fi

echo "---"
echo "- RELEASE_NOTES: $RELEASE_NOTES"
echo "- PACKAGE_NAME: $PACKAGE_NAME"
echo "- PACKAGE_VERSION: $PACKAGE_VERSION"
echo "- FINAL_RELEASE_VERSION: $FINAL_RELEASE_VERSION"
echo "- VERSION_UPGRADE_TYPE: $VERSION_UPGRADE_TYPE"
echo "- TAG: $TAG"
echo "- RELEASE_TYPE: $RELEASE_TYPE"

# Set output
echo "tag=$(echo $TAG)" >>$GITHUB_OUTPUT
echo "release-notes=$(echo $RELEASE_NOTES)" >>$GITHUB_OUTPUT
echo "final-release-version=$(echo $FINAL_RELEASE_VERSION)" >>$GITHUB_OUTPUT
echo "release-type=$(echo $VERSION_UPGRADE_TYPE)" >>$GITHUB_OUTPUT
echo "package-version=$(echo $PACKAGE_VERSION)" >>$GITHUB_OUTPUT
echo "release-type=$(echo $RELEASE_TYPE)" >>$GITHUB_OUTPUT
129 changes: 129 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
name: Deploy and Release
on:
workflow_dispatch:
push:
branches:
- main
paths:
- .release/*
permissions:
contents: write
jobs:
release:
permissions: write-all
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install jq
run: |
sudo apt-get update
sudo apt-get install jq
- name: Setup node version
uses: actions/setup-node@v4
with:
node-version: '*'
registry-url: 'https://registry.npmjs.org'

- name: Read release props
id: read-release-props
run: bash .github/scripts/release.sh 2>&1 | tee -a get-release-props-logs.log

- name: Configure git
run: |
USER_NAME='laxmanpokhel_actions'
USER_EMAIL='laxmanpokhrel@users.noreply.github.com'
# Set the git configs
git config --global user.name $USER_NAME
git config --global user.email $USER_EMAIL
- name: Commit logs
continue-on-error: true
run: |
git add .
git commit -m "ci: Release props logs generated."
- name: Upload log file
uses: actions/upload-artifact@v4
with:
name: get-release-props-logs
path: get-release-props-logs.log

- name: Debug variables
run: |
echo "tag=${{steps.read-release-props.outputs.tag}}"
echo "release-notes=${{steps.read-release-props.outputs.release-notes}}"
echo "package-version: ${{steps.read-release-props.outputs.package-version}}"
echo "release-type: ${{steps.read-release-props.outputs.release-type}}"
echo "final-release-version: ${{steps.read-release-props.outputs.final-release-version}}"
echo "release-type: ${{steps.read-release-props.outputs.release-type}}"
echo "isPrerelease: ${{steps.read-release-props.outputs.release-type}} == '--prerelease'"
- name: Checkout to new branch and update package json with new version
id: branch-checkout-details
run: |
# Read current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Create a checkout branch name
checkout_branch_name=release-${{steps.read-release-props.outputs.final-release-version}}
# Checkout to new branch
git checkout -b $checkout_branch_name
# Update the version in package.json, and commit & tag the change:
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"
# Set output
echo "current_branch=$(echo $current_branch)" >>$GITHUB_OUTPUT
echo "checkout_branch_name=$(echo $checkout_branch_name)" >>$GITHUB_OUTPUT
- name: Check branch
run: git branch

- name: Install package
run: yarn install

- name: Build package
run: yarn build

- name: Publish package
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm pkg fix
npm publish
- name: Create release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${{ steps.read-release-props.outputs.tag }}"
title="${tag}"
gh release create "$tag" \
--repo="$GITHUB_REPOSITORY" \
--title="$title" \
--notes="${{ steps.read-release-props.outputs.release-notes }}" \
${{steps.read-release-props.outputs.release-type}}
- name: Push and raise a PR
if: ${{steps.read-release-props.outputs.release-type != '--prerelease'}}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Push the updated version new checkout branch
git push --set-upstream origin ${{steps.branch-checkout-details.outputs.checkout_branch_name}}
# Set the upstream branch for the current branch
git branch --set-upstream-to=origin/${{ steps.branch-checkout-details.outputs.checkout_branch_name }} ${GITHUB_REF#refs/heads/}
# Create PR for the release
gh pr create \
--base ${{steps.branch-checkout-details.outputs.current_branch}} \
--title "version ${{steps.read-release-props.outputs.final-release-version}}" \
--body "* version: ${{steps.read-release-props.outputs.final-release-version}} | $(date -u +"%Y-%m-%d %H:%M:%S")" \
--no-maintainer-edit
40 changes: 0 additions & 40 deletions .github/workflows/release.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Ignore source files
src/
*.js
*.ts

# Ignore development tools and configuration files
node_modules/
.git/
.editorconfig
.eslintrc
tsconfig.json
.github
.husky
.release
scripts
src

.eslintignore
.eslintrc.cjs
.lintstagedrc
.prettierrc
.releaserc
yarn.lock
package-lock.json
registry/
4 changes: 4 additions & 0 deletions .release/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"versionUpgradeType": "major",
"releaseType": "--latest"
}
5 changes: 5 additions & 0 deletions .release/release-notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes
- Semantic release removes
- Cleanup dev dependencies
- npm ignore
- workflow changes
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"url": "https://github.com/laxmanpokhrel/xmanscript-has-error-boundary.git"
},
"keywords": [],
"exports": {
"./setValueOfKeyForMatchingValuesOfAKey": "./src/utils/setValueOfKeyForMatchingValuesOfAKey"
},
"bugs": {
"url": "https://github.com/laxmanpokhrel/xmanscript-has-error-boundary/issues"
},
Expand All @@ -27,8 +30,6 @@
"@types/react": "^18.2.28",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.49.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-base": "^15.0.0",
Expand All @@ -42,8 +43,6 @@
"husky": "^8.0.0",
"lint-staged": "^14.0.1",
"prettier": "^3.0.3",
"semantic-release": "^22.0.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6"
Expand All @@ -55,11 +54,6 @@
"prettier --cache --write"
]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"publishConfig": {
"access": "public"
},
Expand Down

0 comments on commit 1dc05b3

Please sign in to comment.