NPM Release Automation #5
This file contains hidden or 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: NPM Release Automation | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: 'The library release version' | |
| required: true | |
| default: '' | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.PAT }} | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: 20 | |
| registry-url: "https://registry.npmjs.org/" | |
| - name: Install packages | |
| run: npm install | |
| - name: Build modules | |
| run: npm run build | |
| - name: Test | |
| run: npm run test | |
| - name: Update package.json version | |
| run: | | |
| VERSION="${{ github.event.inputs.environment }}" | |
| echo "Release v $VERSION" | |
| jq --arg ver "$VERSION" '.version = $ver' package.json > temp.json && mv temp.json package.json | |
| jq --arg ver "$VERSION" '.version = $ver' modules/cmpapi/package.json > temp.json && mv temp.json modules/cmpapi/package.json | |
| jq --arg ver "$VERSION" '.version = $ver' modules/stub/package.json > temp.json && mv temp.json modules/stub/package.json | |
| - name: Check input version TAG | |
| id: check | |
| run: | | |
| git status | |
| VERSION="${{ github.event.inputs.environment }}" | |
| echo "Release v $VERSION" | |
| if git show-ref --tags --verify --quiet "refs/tags/${VERSION}"; then | |
| echo "Tag ${VERSION} exists" | |
| echo "version_changed=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "Tag ${VERSION} does not exist" | |
| echo "version_changed=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Publish to NPM | |
| if: ${{steps.check.outputs.version_changed == 'true'}} | |
| run: | | |
| npm --version | |
| cd modules/stub | |
| pwd | |
| npm publish --access public #actual release to NPM repository | |
| cd ../cmpapi | |
| pwd | |
| npm publish --access public #actual release to NPM repository | |
| env: | |
| NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} | |
| - name: Commit and Push Changes | |
| if: ${{steps.check.outputs.version_changed == 'true'}} | |
| run: | | |
| git status | |
| git config user.email "[email protected]" | |
| git config user.name "Mayank Mishra" | |
| VERSION="${{ github.event.inputs.environment }}" | |
| echo "Release v $VERSION" | |
| #Stage the files | |
| git add . | |
| git commit -m "Release v $VERSION" | |
| git tag "$VERSION" | |
| git push --tags #actual tag push to GitHub Repo | |
| #git push origin master #actual push to GitHub Repo | |
| env: | |
| GITHUB_TOKEN: ${{secrets.PAT}} |