merge(#42): feat: automatic gh releases and npm publishes #8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR workflow | |
on: | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
push: | |
branches: | |
- main | |
jobs: | |
semver-check: | |
if: github.event.action == 'opened' | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.get-version.outputs.version }} | |
steps: | |
- name: Reattach HEAD | |
uses: actions/checkout@v2 | |
with: | |
ref: ${{ github.event.pull_request.head.ref }} | |
fetch-depth: 0 | |
- name: Merge main branch to PR branch | |
uses: pr-mpt/actions-merge-branch@v2 | |
with: | |
from: "origin/main" | |
- name: Get current prefix | |
id: branch-prefix | |
run: echo "::set-output name=prefix::$(echo ${{ github.head_ref }} | cut -d '/' -f 1)" | |
- name: Check if branch prefix is valid (major, minor, patch) | |
run: | | |
echo "Checking branch prefix..." | |
echo "branch prefix: ${{ steps.branch-prefix.outputs.prefix }}" | |
if [[ ${{ steps.branch-prefix.outputs.prefix }} != "major" && ${{ steps.branch-prefix.outputs.prefix }} != "minor" && ${{ steps.branch-prefix.outputs.prefix }} != "patch" && ${{ steps.branch-prefix.outputs.prefix }} != "dependabot" ]]; then | |
echo "Branch prefix is not valid, exiting... allowed prefixes are major, minor, patch, dependabot" | |
exit 1 | |
fi | |
echo "Branch prefix is valid" | |
- name: Set next version number based on prefix | |
run: | | |
echo "Getting next version number..." | |
if [ "${{ steps.branch-prefix.outputs.prefix }}" == "major" ]; then | |
echo "Bumping major version..." | |
npm --no-git-tag-version version major | |
elif [ "${{ steps.branch-prefix.outputs.prefix }}" == "minor" ]; then | |
echo "Bumping minor version..." | |
npm --no-git-tag-version version minor | |
elif [ "${{ steps.branch-prefix.outputs.prefix }}" == "patch" ] || [ "${{ steps.branch-prefix.outputs.prefix }}" == "dependabot" ]; then | |
echo "Bumping patch version..." | |
npm --no-git-tag-version version patch | |
else | |
echo "Error: working branch prefix not found" | |
exit 1 | |
fi | |
- name: Get version number | |
id: get-version | |
run: echo "::set-output name=version::$(git describe --tags --abbrev=0)" | |
- name: Setup git | |
run: | | |
echo "Setting up git..." | |
git config --global user.email "${{ secrets.MY_EMAIL }}" | |
git config --global user.name "${{ secrets.MY_USERNAME }}" | |
- name: Commit and push changes to working branch | |
run: | | |
echo "Committing changes..." | |
git add . | |
git commit -am "chore(release): Bump ${{ steps.branch-prefix.outputs.prefix }} version" | |
git push origin HEAD | |
check-naming: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Check PR name | |
run: | | |
pr_name=$(jq -r .pull_request.title $GITHUB_EVENT_PATH) | |
if [[ ! $pr_name =~ ^(feat|fix|chore|docs|misc)(\([a-zA-Z]+\))?!?:\ .*$ ]]; then | |
echo "error: Pull request title must start with feat/fix/chore/docs/misc, followed by optional ! for breaking changes, followed by an optional scope in parentheses, followed by a colon, followed by a space, followed by a description." | |
exit 1 | |
fi | |
gh-release: | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
needs: | |
- semver-check | |
steps: | |
- name: Create release | |
uses: softprops/action-gh-release@v1 | |
with: | |
name: "New release ${{ needs.semver-check.outputs.version }}" | |
tag_name: ${{ needs.semver-check.outputs.version }} | |
generate_release_notes: true | |
draft: false | |
prerelease: false | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
publish-npm: | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
needs: | |
- semver-check | |
- gh-release | |
steps: | |
- name: Set up Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: '18' | |
- name: Install dependencies | |
run: npm install | |
- name: Publish to NPM | |
run: npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |