Publish Package #378
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 | |
- assets | |
- linting | |
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 }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.VA_MOBILE_ROBOT_GITHUB_PAT }} | |
- name: Setup Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: '3.2' | |
bundler-cache: true | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
registry-url: https://registry.npmjs.org/ | |
node-version-file: .nvmrc | |
cache: yarn | |
cache-dependency-path: yarn.lock | |
- name: Set git config | |
run: | | |
git config --global user.name 'VA Automation Bot' | |
git config --global user.email 'va-mobileapp@adhocteam.us' | |
- name: Install dependencies | |
run: yarn install | |
- name: Get 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: Increment 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 to git | |
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 | |
- name: Create git tag | |
working-directory: packages/${{ inputs.package }} | |
run: | | |
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' && github.ref_name == 'main' }} | |
run: | | |
chmod +x .github/scripts/generate-changelog.sh | |
cd documentation | |
./../.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: Send success message to Slack | |
id: slack | |
uses: slackapi/slack-github-action@v1.24.0 | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_OAUTH_TOKEN }} | |
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.package-name.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 | |
} | |
- name: Send failure message to Slack | |
if: failure() | |
id: slack-error | |
uses: slackapi/slack-github-action@v1.24.0 | |
env: | |
SLACK_BOT_TOKEN: ${{ secrets.SLACK_OAUTH_TOKEN }} | |
with: | |
channel-id: C062TM03HN2 # DSVA #va-mobile-library-alerts channel | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": ":red-x: Error publishing *${{ inputs.package }}* to NPM" | |
} | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{ | |
"type": "mrkdwn", | |
"text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Workflow Run>" | |
} | |
] | |
} | |
], | |
"unfurl_links": false, | |
"unfurl_media": false | |
} |