Skip to content

Instantly share code, notes, and snippets.

@carthegian
Created December 6, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carthegian/49c2ae2e5c39991c1612a7e3c4fc7ccb to your computer and use it in GitHub Desktop.
Save carthegian/49c2ae2e5c39991c1612a7e3c4fc7ccb to your computer and use it in GitHub Desktop.
Github action workflow to manage versioning upon pull request to a branch
# This is a workflow to update version of of an application
name: Version-release
on:
# Run this workflow only if PR to master branch is closed
pull_request:
types: [closed]
branches:
- master
# Also enable manual trigger workflow run
workflow_dispatch:
jobs:
# Job for updating version
update-version:
# Run this job if the PR has been merged & closed not only closed
if: github.event.pull_request.merged == true
# The type of runner that the job will run on
runs-on: ubuntu-latest
steps:
# Associate each release label with semver
- name: Set major release
if: contains(github.event.pull_request.labels.*.name, 'release-major')
run: echo "RELEASE=major" >> $GITHUB_ENV
- name: Set minor release
if: contains(github.event.pull_request.labels.*.name, 'release-minor')
run: echo "RELEASE=minor" >> $GITHUB_ENV
- name: Set patch release
if: contains(github.event.pull_request.labels.*.name, 'release-patch')
run: echo "RELEASE=patch" >> $GITHUB_ENV
# Check here if release label is not set on pull request
- name: Check release env
# Use pipe symbol | to do multiline commands
run: |
if [[ -z "${{ env.RELEASE }}" ]];
then
echo "Release label is not set on this PR. You need to set release label on PRs in order to continue."
exit 1
else
exit 0
fi
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
with:
ref: master
# Install semver tool
- name: Install semver-tool
run: |
export DIR=$(mktemp -d)
cd $DIR
curl https://github.com/fsaintjacques/semver-tool/archive/3.2.0.tar.gz -L -o semver.tar.gz
tar -xvf semver.tar.gz
sudo cp semver-tool-3.2.0/src/semver /usr/local/bin
# Install jq
- name: Install jq
run: |
sudo apt-get update
sudo apt-get install jq
# Get CURRENT from __version file then increment new version
- name: Bump version
run: |
export CURRENT=$(head -1 __version | xargs )
export NEW_VERSION=$(semver bump ${{ env.RELEASE }} $CURRENT)
echo "VERSION=$NEW_VERSION" >> $GITHUB_ENV
# Update __version
- name: Prepare __version
run: |
echo "${{ env.VERSION }}" > "__version"
# Use jq to update manifest.json
- name: Prepare manifest
run: |
jq '.version="${{ env.VERSION }}"' public/manifest.json > manifest.json.tmp
mv manifest.json.tmp public/manifest.json
# Update CHANGELOG with pull request contents
- name: Prepare CHANGELOG
run: |
echo "${{ github.event.pull_request.body }}" | csplit -s - "/##/"
echo "# Changelog
## ${{ env.VERSION }}
" >> CHANGELOG.tmp
grep "-" xx01 >> CHANGELOG.tmp
grep -v "^# " CHANGELOG.md >> CHANGELOG.tmp
cp CHANGELOG.tmp CHANGELOG.md
# Upload artifacts to be used in 'build'
- run: echo "🥇 Update version is finished with artifacts."
- uses: actions/upload-artifact@v2
with:
name: version_file
path: __version
- uses: actions/upload-artifact@v2
with:
name: changelog_file
path: CHANGELOG.md
- uses: actions/upload-artifact@v2
with:
name: manifest_file
path: public/manifest.json
# Job to build application and push to default branch
build:
# This job will only run if job 'update-version' is completed
needs: update-version
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
with:
ref: master
- name: Using Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Run install
run: npm install
- name: Run build
run: npm run build
- run: echo "🍏 Build has finished"
# Download artifacts created in 'update-version'
- uses: actions/download-artifact@v2
with:
name: version_file
- uses: actions/download-artifact@v2
with:
name: changelog_file
- uses: actions/download-artifact@v2
with:
name: manifest_file
path: public
# Git commit and push
- name: Git commit build
run: |
export VERSION=$(head -1 __version | xargs)
git config --local user.email <YOUR_EMAIL_HERE>
git config --local user.name "github-actions[bot]"
git add .
git commit -m "[Build] Creating build files for $VERSION"
git tag $VERSION
# Push to protected branch
- name: Push Changes
uses: CasperWA/push-protected@v2
with:
# For protected branch needs to use Personal Access Token
token: ${{ secrets.TOKEN }}
tags: True
branch: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment