Publish Package #114
Workflow file for this run
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
# Bump version and release specified package to NPM | |
name: 'Publish Package' | |
on: | |
workflow_dispatch: | |
inputs: | |
package: | |
description: Package | |
required: true | |
type: choice | |
options: | |
- components | |
- tokens | |
- linting | |
- test | |
version_type: | |
description: Version type | |
required: true | |
type: choice | |
options: | |
- alpha | |
- beta | |
- patch | |
- minor | |
- major | |
- new package | |
jobs: | |
publish-package: | |
runs-on: ubuntu-latest | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_KEY }} | |
GIT_AUTHOR_NAME: VA Automation Bot" | |
GIT_AUTHOR_EMAIL: [email protected] | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.VA_MOBILE_ROBOT_GITHUB_PAT }} | |
- uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: '3.2' | |
bundler-cache: true | |
- name: Use Node.js 18.x | |
uses: actions/setup-node@v3 | |
with: | |
registry-url: https://registry.npmjs.org/ | |
node-version-file: .nvmrc | |
cache: yarn | |
cache-dependency-path: yarn.lock | |
- run: yarn install | |
- name: Get NPM package name | |
id: package-name | |
working-directory: packages/${{ inputs.package }} | |
run: | | |
NPM_PACKAGE=$(jq -r .name package.json) | |
echo "NPM Package name: $NPM_PACKAGE" | |
echo "NPM_PACKAGE_NAME=$NPM_PACKAGE" >> "$GITHUB_OUTPUT" | |
- name: Bump version | |
if: ${{ inputs.version_type != 'new package' }} | |
working-directory: packages/${{ inputs.package }} | |
run: | | |
BUMP=${{ inputs.version_type }} | |
NPM_PACKAGE=${{ steps.package-name.outputs.NPM_PACKAGE_NAME }} | |
echo "Checking package.json version..." | |
CURRENT_VERSION=$(jq -r .version package.json) | |
echo "Checking latest version on NPM..." | |
LATEST_VERSION=$(npm view $NPM_PACKAGE versions --json | \ | |
jq -r 'if type=="string" then . elif type=="array" then .[-1] else "error" end') | |
if [[ "$LATEST_VERSION" == "error" ]]; then | |
echo "Unexpected result getting version. Exiting..." | |
exit 1 | |
fi | |
echo "Latest NPM version: $LATEST_VERSION" | |
if [[ "$CURRENT_VERSION" != "$LATEST_VERSION" ]]; then | |
echo "Setting package.json version to $LATEST_VERSION" | |
npm version $LATEST_VERSION | |
fi | |
echo "Bumping $BUMP version..." | |
if [[ "$BUMP" == "alpha" ]] || [[ "$BUMP" == "beta" ]]; then | |
npm version prerelease --preid $BUMP | |
else | |
npm version $BUMP | |
fi | |
- name: Publish to NPM | |
id: publish | |
working-directory: packages/${{ inputs.package }} | |
run: | | |
BUMP="${{ inputs.version_type }}" | |
echo "Publishing to NPM..." | |
if [[ "$BUMP" == "alpha" ]] || [[ "$BUMP" == "beta" ]]; then | |
npm publish --access public --tolerate-republish --tag $BUMP | |
else | |
npm publish --access public --tolerate-republish | |
fi | |
NEW_VERSION=$(jq -r .version package.json) | |
echo "Updated version: $NEW_VERSION" | |
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_OUTPUT" | |
echo "GIT_TAG=${{ inputs.package }}-v$NEW_VERSION" >> "$GITHUB_OUTPUT" | |
- name: Commit changes and tag | |
if: ${{ inputs.version_type != 'new package' }} | |
working-directory: packages/${{ inputs.package }} | |
run: | | |
git pull | |
git add package.json | |
git commit -m 'Version bump: ${{ steps.publish.outputs.GIT_TAG }}' | |
git push | |
TAG=${{ steps.publish.outputs.GIT_TAG }} | |
echo $TAG | |
git tag -a $TAG -m $TAG | |
git push origin $TAG | |
- name: Generate changelog | |
if: ${{ inputs.version_type != 'alpha' && inputs.version_type != 'beta' }} | |
run: | | |
chmod +x .github/scripts/generate-changelog.sh | |
./.github/scripts/generate-changelog.sh ${{ inputs.package }} ${{ secrets.VA_MOBILE_ROBOT_GITHUB_PAT }} | |
git add CHANGELOG.md | |
git commit -m 'Changelog for ${{ steps.publish.outputs.GIT_TAG }}' | |
git push | |
- name: Post to a Slack channel | |
id: slack | |
uses: slackapi/[email protected] | |
with: | |
channel-id: C062TM03HN2 # DSVA #va-mobile-library-alerts channel | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "Published *${{ steps.package-name.outputs.NPM_PACKAGE_NAME }}* to NPM" | |
} | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{ | |
"type": "mrkdwn", | |
"text": "*Version:* ${{ steps.publish.outputs.NEW_VERSION }}" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "<https://www.npmjs.com/package/${{ steps.publish.outputs.NPM_PACKAGE_NAME }}|NPM>" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "<https://github.com/department-of-veterans-affairs/va-mobile-library/releases/tag/${{ steps.publish.outputs.GIT_TAG }}|GitHub>" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Workflow Run>" | |
} | |
] | |
} | |
], | |
"unfurl_links": false, | |
"unfurl_media": false | |
} | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_OAUTH_TOKEN }} |