Migrate tooling to pnpm (#109) #37
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: Deploy to npm | |
| on: | |
| push: | |
| branches: | |
| - main | |
| permissions: | |
| id-token: write # Required for OIDC authentication | |
| contents: read | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v7 | |
| - name: Setup pnpm | |
| uses: pnpm/action-setup@v6 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v6 | |
| with: | |
| node-version: "24" | |
| registry-url: "https://registry.npmjs.org" | |
| cache: pnpm | |
| - name: Install dependencies | |
| run: pnpm install --frozen-lockfile | |
| - name: Build project | |
| run: pnpm run build | |
| - name: Check if version exists | |
| id: check-version | |
| run: | | |
| if npm view $(node -p "require('./package.json').name")@$(node -p "require('./package.json').version") > /dev/null 2>&1; then | |
| echo "exists=true" >> $GITHUB_ENV | |
| else | |
| echo "exists=false" >> $GITHUB_ENV | |
| fi | |
| - name: Publish to npm | |
| id: publish_step | |
| if: env.exists == 'false' | |
| run: pnpm publish --provenance --access public | |
| - name: Notify Slack (success) | |
| if: env.exists == 'false' && steps.publish_step.outcome == 'success' | |
| env: | |
| SLACK_DEPLOY_WEBHOOK_URL: ${{ secrets.SLACK_DEPLOY_WEBHOOK_URL }} | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [ -z "${SLACK_DEPLOY_WEBHOOK_URL:-}" ]; then | |
| echo "SLACK_DEPLOY_WEBHOOK_URL not set; skipping Slack notification" | |
| exit 0 | |
| fi | |
| node <<'JS' | |
| const fs = require('node:fs'); | |
| const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); | |
| const version = packageJson.version; | |
| const packageName = packageJson.name; | |
| let latestChanges = 'See CHANGELOG for details.'; | |
| if (fs.existsSync('CHANGELOG.md')) { | |
| const lines = fs.readFileSync('CHANGELOG.md', 'utf8').split(/\r?\n/); | |
| const bullets = []; | |
| let inTargetSection = false; | |
| for (const line of lines) { | |
| if (line.startsWith(`## [${version}]`)) { | |
| inTargetSection = true; | |
| continue; | |
| } | |
| if (inTargetSection && line.startsWith('## [')) break; | |
| if (inTargetSection && line.startsWith('- ')) { | |
| bullets.push(line.slice(2).trim()); | |
| } | |
| } | |
| if (bullets.length) { | |
| latestChanges = bullets.slice(0, 5).map((bullet) => `• ${bullet}`).join('\n'); | |
| } | |
| } | |
| fs.writeFileSync('/tmp/slack_payload.json', JSON.stringify({ | |
| text: `Facturapi Node SDK ${version} published to npm`, | |
| blocks: [ | |
| { | |
| type: 'header', | |
| text: { | |
| type: 'plain_text', | |
| text: `Node SDK ${version} published to npm`, | |
| }, | |
| }, | |
| { | |
| type: 'section', | |
| fields: [ | |
| { type: 'mrkdwn', text: `*Package:* \`${packageName}@${version}\`` }, | |
| { type: 'mrkdwn', text: `*Branch:* \`${process.env.GITHUB_REF_NAME}\`` }, | |
| { type: 'mrkdwn', text: `*Commit:* \`${process.env.GITHUB_SHA}\`` }, | |
| { type: 'mrkdwn', text: `*Actor:* \`${process.env.GITHUB_ACTOR}\`` }, | |
| ], | |
| }, | |
| { | |
| type: 'section', | |
| text: { | |
| type: 'mrkdwn', | |
| text: [ | |
| '*Useful links*', | |
| `• npm: <https://www.npmjs.com/package/${packageName}/v/${version}|View package>`, | |
| `• Workflow run: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}|Open run>`, | |
| `• Changelog: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/blob/${process.env.GITHUB_SHA}/CHANGELOG.md|Read changes>`, | |
| ].join('\n'), | |
| }, | |
| }, | |
| { | |
| type: 'section', | |
| text: { | |
| type: 'mrkdwn', | |
| text: `*Latest changes*\n${latestChanges}`, | |
| }, | |
| }, | |
| ], | |
| })); | |
| JS | |
| curl -sS -X POST -H "Content-type: application/json" --data "@/tmp/slack_payload.json" "$SLACK_DEPLOY_WEBHOOK_URL" || true | |
| - name: Notify Slack (failure) | |
| if: failure() && env.exists == 'false' && steps.publish_step.outcome == 'failure' | |
| env: | |
| SLACK_DEPLOY_WEBHOOK_URL: ${{ secrets.SLACK_DEPLOY_WEBHOOK_URL }} | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [ -z "${SLACK_DEPLOY_WEBHOOK_URL:-}" ]; then | |
| echo "SLACK_DEPLOY_WEBHOOK_URL not set; skipping Slack notification" | |
| exit 0 | |
| fi | |
| node <<'JS' | |
| const fs = require('node:fs'); | |
| const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); | |
| fs.writeFileSync('/tmp/slack_payload_failure.json', JSON.stringify({ | |
| text: `Facturapi Node SDK ${packageJson.version} publish failed`, | |
| blocks: [ | |
| { | |
| type: 'section', | |
| text: { | |
| type: 'mrkdwn', | |
| text: [ | |
| `*Node SDK ${packageJson.version} publish failed*`, | |
| `• Workflow run: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}|Open run>`, | |
| `• Commit: \`${process.env.GITHUB_SHA}\``, | |
| ].join('\n'), | |
| }, | |
| }, | |
| ], | |
| })); | |
| JS | |
| curl -sS -X POST -H "Content-type: application/json" --data "@/tmp/slack_payload_failure.json" "$SLACK_DEPLOY_WEBHOOK_URL" || true |