Publish Package #51
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 | |
version_bump: | |
description: Version bump | |
required: true | |
type: choice | |
options: | |
- alpha | |
- beta | |
- patch | |
- minor | |
- major | |
jobs: | |
publish-package: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: packages/${{ inputs.package }} | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_KEY }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
persist-credentials: false | |
- name: Use Node.js 18.x | |
uses: actions/setup-node@v3 | |
with: | |
registry-url: https://registry.npmjs.org/ | |
node-version-file: '.nvmrc' | |
- run: yarn install | |
- name: Bump version and publish to NPM | |
id: bump-version | |
run: | | |
NPM_PACKAGE=$(jq -r .name package.json) | |
echo "NPM_PACKAGE_NAME=$NPM_PACKAGE" >> "$GITHUB_OUTPUT" | |
echo "NPM Package name: $NPM_PACKAGE" | |
CURRENT_VERSION=$(jq -r .version package.json) | |
LATEST_VERSION=$(npm view $NPM_PACKAGE versions --json | jq -r '.[-1]') | |
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 | |
BUMP=${{ inputs.version_bump }} | |
echo "Bumping $BUMP version and publishing to NPM..." | |
if [[ "$BUMP" == "alpha" ]] || [[ "$BUMP" == "beta" ]]; then | |
npm version prerelease --preid $BUMP | |
npm publish --access public --tolerate-republish --tag $BUMP | |
else | |
npm version $BUMP | |
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 package.json changes and tag | |
env: | |
GITHUB_TOKEN: ${{ secrets.VA_MOBILE_ROBOT_GITHUB_PAT }} | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" >> token.txt | |
echo "${{ secrets.VA_MOBILE_ROBOT_GITHUB_PAT }}" >> token.txt | |
git config --global user.name 'VA Automation Bot' | |
git config --global user.email '[email protected]' | |
git pull | |
git add package.json | |
git commit -m 'Version bump: ${{ steps.bump-version.outputs.GIT_TAG }}' | |
git push | |
TAG=${{ steps.bump-version.outputs.GIT_TAG }} | |
echo $TAG | |
git tag -a $TAG -m $TAG | |
git push origin $TAG | |
- 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.bump-version.outputs.NPM_PACKAGE_NAME }}* to NPM" | |
} | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{ | |
"type": "mrkdwn", | |
"text": "*Version:* ${{ steps.bump-version.outputs.NEW_VERSION }}" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "<https://www.npmjs.com/package/${{ steps.bump-version.outputs.NPM_PACKAGE_NAME }}|NPM>" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "<https://github.com/department-of-veterans-affairs/va-mobile-library/releases/tag/${{ steps.bump-version.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 }} | |
- name: Setup tmate session | |
if: ${{ failure() }} | |
uses: mxschmitt/action-tmate@v3 | |
timeout-minutes: 15 |