|
| 1 | +name: Publish Packages |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ['Linting and Tests'] |
| 6 | + types: [completed] |
| 7 | + branches: [main] |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + package: |
| 11 | + description: 'Specific package to publish (leave empty for all packages)' |
| 12 | + required: false |
| 13 | + type: string |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + |
| 18 | +env: |
| 19 | + COMMIT_SHA: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 20 | + |
| 21 | +jobs: |
| 22 | + detect-packages: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + outputs: |
| 25 | + packages: ${{ steps.find-packages.outputs.packages }} |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 29 | + |
| 30 | + - name: Find packages |
| 31 | + id: find-packages |
| 32 | + env: |
| 33 | + PACKAGE: ${{ github.event.inputs.package }} |
| 34 | + run: | |
| 35 | + if [ "$PACKAGE" != "" ]; then |
| 36 | + echo "packages=[\"$PACKAGE\"]" >> $GITHUB_OUTPUT |
| 37 | + else |
| 38 | + PACKAGES=$(ls -d packages/* | xargs -n 1 basename | jq -R -s -c 'split("\n")[:-1]') |
| 39 | + echo "packages=$PACKAGES" >> $GITHUB_OUTPUT |
| 40 | + fi |
| 41 | +
|
| 42 | + verify-commit: |
| 43 | + runs-on: ubuntu-latest |
| 44 | + if: github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push') |
| 45 | + steps: |
| 46 | + - name: Checkout repository |
| 47 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 48 | + |
| 49 | + - name: Verify commit authenticity |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + run: | |
| 53 | + COMMIT_DATA=$(gh api repos/${{ github.repository }}/commits/$COMMIT_SHA) |
| 54 | + VERIFIED=$(echo "$COMMIT_DATA" | jq -r '.commit.verification.verified') |
| 55 | + COMMITTER=$(echo "$COMMIT_DATA" | jq -r '.commit.committer.email') |
| 56 | +
|
| 57 | + if [[ "$VERIFIED" != "true" ]]; then |
| 58 | + echo "❌ Unverified commit! Aborting." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +
|
| 62 | + if [[ "$COMMITTER" != "[email protected]" ]]; then |
| 63 | + echo "❌ Not merged with the merge queue! Aborting." |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +
|
| 67 | + echo "✅ Commit is verified and trusted." |
| 68 | +
|
| 69 | + publish: |
| 70 | + needs: [detect-packages, verify-commit] |
| 71 | + runs-on: ubuntu-latest |
| 72 | + strategy: |
| 73 | + matrix: |
| 74 | + package: ${{ fromJson(needs.detect-packages.outputs.packages) }} |
| 75 | + fail-fast: false |
| 76 | + steps: |
| 77 | + - name: Checkout repository |
| 78 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 79 | + with: |
| 80 | + fetch-depth: 2 |
| 81 | + |
| 82 | + - name: Check for package changes |
| 83 | + if: github.event_name != 'workflow_dispatch' |
| 84 | + id: check_changes |
| 85 | + env: |
| 86 | + PACKAGE: ${{ matrix.package }} |
| 87 | + run: | |
| 88 | + if git diff --quiet $COMMIT_SHA~1 $COMMIT_SHA -- "packages/$PACKAGE/"; then |
| 89 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 90 | + else |
| 91 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 92 | + fi |
| 93 | +
|
| 94 | + - name: Set up pnpm |
| 95 | + uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 |
| 96 | + with: |
| 97 | + cache: true |
| 98 | + |
| 99 | + - name: Setup Node.js |
| 100 | + if: github.event_name == 'workflow_dispatch' || steps.check_changes.outputs.changed == 'true' |
| 101 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 102 | + with: |
| 103 | + node-version-file: '.nvmrc' |
| 104 | + registry-url: 'https://registry.npmjs.org' |
| 105 | + cache: pnpm |
| 106 | + |
| 107 | + - name: Publish |
| 108 | + if: github.event_name == 'workflow_dispatch' || steps.check_changes.outputs.changed == 'true' |
| 109 | + working-directory: packages/${{ matrix.package }} |
| 110 | + env: |
| 111 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 112 | + run: > |
| 113 | + npm version --no-git-tag-version 0.0.0-$COMMIT_SHA |
| 114 | + pnpm publish --access public |
0 commit comments