Skip to content

Publish Package

Publish Package #10

name: Publish Package
on:
workflow_dispatch:
inputs:
package:
description: 'Name of package to publish'
required: true
default: 'components'
type: choice
options:
- components
- contexts
- hooks
- resource-utils
- utils
version:
description: 'SemVer format release tag, i.e. 0.2.4'
required: true
concurrency:
group: ${{ github.workflow }}-${{ github.event.inputs.package }}-${{ github.ref }}
cancel-in-progress: true
jobs:
format-version:
runs-on: ubuntu-22.04
outputs:
version: ${{ steps.format_version.outputs.version }}
steps:
- name: Format Input
id: format_version
run: |
version="${{ github.event.inputs.version }}"
if [[ $version == v* ]];
then
version="${version:1}"
fi
echo "version=$version" >> $GITHUB_OUTPUT
build-and-release:
needs: format-version
name: Create release
runs-on: ubuntu-22.04
outputs:
release_id: ${{ steps.create_release.outputs.id }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update version command
run: |
sed -i -e 's/"version"\s*:\s*".*"/"version": "'"$NEW_VERSION"'"/' "$FILE"
env:
FILE: ${{ github.workspace }}/packages/${{ github.event.inputs.package }}/package.json
NEW_VERSION: ${{ github.event.inputs.version }}
- name: Determine and Set Tags
run: |
# Find the last two tags that match the package pattern, sort them to get the latest
TAGS=$(git tag | grep "^${{ github.event.inputs.package }}-v" | sort -rV | head -n 1)
LAST_TAG=$(echo "$TAGS" | sed -n '1p')
echo "LAST_TAG=${LAST_TAG}" >> $GITHUB_ENV
- name: Generate Changelog
id: changelog
uses: mikepenz/release-changelog-builder-action@v4
with:
configurationJson: |
{
"categories": [
{
"title": "## ${{ github.event.inputs.package }}",
"labels": [
"${{ github.event.inputs.package }}"
],
"exhaustive": true,
"exhaustive_rules": "false",
"empty_content": "- no matching PRs",
"rules": [
{
"pattern": "${{ github.event.inputs.package }}",
"on_property": "labels"
},
{
"pattern": "merged",
"on_property": "status"
}
]
}
],
"sort": {
"order": "ASC",
"on_property": "mergedAt"
},
"template": "#{{CHANGELOG}}\n",
"pr_template": "* [#{{NUMBER}}](#{{URL}}): #{{TITLE}}\n #{{BODY}}",
"empty_template": "- no changes",
"max_tags_to_fetch": 200,
"max_back_track_time_days": 365,
"base_branches": [
"main"
]
}
fromTag: "${{ env.LAST_TAG }}"
toTag: "main"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add and Commit Changes
uses: EndBug/[email protected]
with:
message: "Update ${{ github.event.inputs.package }} package version to ${{ needs.format-version.outputs.version }}"
add: "${{ github.workspace }}/packages/${{ github.event.inputs.package }}/package.json"
cwd: "."
new_branch: ${{ github.event.inputs.package }}-package-release
- name: Create Pull Request
run: gh pr create --base main --head "${{ github.event.inputs.package }}-package-release" --title "Release ${{ github.event.inputs.package }} ${{ needs.format-version.outputs.version }}" --body "Created by the Release workflow to update ${{ github.event.inputs.package }} version"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Enable Pull Request Auto-Merge
run: gh pr merge --merge --auto "${{ github.event.inputs.package }}-package-release"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Enable Corepack before setting up Node
run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: '18'
registry-url: 'https://registry.npmjs.org'
- name: Install and run build
working-directory: ./
run: |
yarn install --immutable
yarn dbuild
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.event.inputs.package }}-v${{ needs.format-version.outputs.version }}
release_name: ${{ github.event.inputs.package }}-v${{ needs.format-version.outputs.version }}
- name: Publish to npm
run: npm publish
working-directory: packages/${{ github.event.inputs.package }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}