Publish API Contract to GitHub Packages #27
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
| # .github/workflows/publish-contract.yml | |
| name: Publish API Contract to GitHub Packages | |
| on: | |
| workflow_run: | |
| workflows: ["Update Version"] | |
| types: | |
| - completed | |
| workflow_dispatch: | |
| jobs: | |
| publish: | |
| if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| packages: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 | |
| ref: ${{ github.event.workflow_run.head_branch || github.ref_name }} | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| registry-url: 'https://npm.pkg.github.com' | |
| scope: '@tobenot' | |
| - name: Install dependencies | |
| run: npm ci | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build | |
| run: npm run build | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Skip if version already exists | |
| id: precheck | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| echo "Local version: $VERSION" | |
| if npm view @tobenot/basic-web-game-backend-contract@${VERSION} version --registry=https://npm.pkg.github.com >/dev/null 2>&1; then | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| echo "Version ${VERSION} already exists in registry. Skipping publish." | |
| else | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Publish package | |
| if: steps.precheck.outputs.exists == 'false' | |
| run: npm publish --registry=https://npm.pkg.github.com | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |