|
| 1 | +name: Create a Snapshot Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + issue_comment: |
| 6 | + types: [created] |
| 7 | + |
| 8 | +defaults: |
| 9 | + run: |
| 10 | + shell: bash |
| 11 | + |
| 12 | +env: |
| 13 | + FORCE_COLOR: 1 |
| 14 | + |
| 15 | +jobs: |
| 16 | + snapshot-release: |
| 17 | + name: Create a snapshot release of a pull request |
| 18 | + if: ${{ github.repository_owner == 'QwikDev' && github.event.issue.pull_request && (contains(github.event.comment.body, '!preview') || contains(github.event.comment.body, '/preview') || contains(github.event.comment.body, '!snapshot') || contains(github.event.comment.body, '/snapshot')) }} |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + id-token: write |
| 23 | + issues: write |
| 24 | + pull-requests: write |
| 25 | + steps: |
| 26 | + - name: "Check if user has admin access (only admins can publish snapshot releases)." |
| 27 | + uses: "lannonbr/repo-permission-check-action@2.0.0" |
| 28 | + with: |
| 29 | + permission: "admin" |
| 30 | + env: |
| 31 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Extract the snapshot name from comment body |
| 34 | + id: getSnapshotName |
| 35 | + uses: actions/github-script@v7 |
| 36 | + with: |
| 37 | + script: | |
| 38 | + const { body } = context.payload.comment; |
| 39 | + const PREVIEW_RE = /^[!\/](?:preview|snapshot)\s+(\S*)\s*$/gim; |
| 40 | + const [_, name] = PREVIEW_RE.exec(body) ?? []; |
| 41 | + if (name) return name; |
| 42 | +
|
| 43 | + const error = 'Invalid command. Expected: "/preview <snapshot-name>"' |
| 44 | + github.rest.issues.createComment({ |
| 45 | + issue_number: context.issue.number, |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + body: error, |
| 49 | + }) |
| 50 | + core.setFailed(error) |
| 51 | + result-encoding: string |
| 52 | + |
| 53 | + - name: resolve pr refs |
| 54 | + id: refs |
| 55 | + uses: eficode/resolve-pr-refs@main |
| 56 | + with: |
| 57 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - uses: actions/checkout@v4 |
| 60 | + with: |
| 61 | + ref: ${{ steps.refs.outputs.head_ref }} |
| 62 | + fetch-depth: 0 |
| 63 | + |
| 64 | + - run: git checkout main |
| 65 | + - run: git checkout ${{ steps.refs.outputs.head_ref }} |
| 66 | + |
| 67 | + - name: Setup PNPM |
| 68 | + uses: pnpm/action-setup@v3 |
| 69 | + |
| 70 | + - name: Setup Node |
| 71 | + uses: actions/setup-node@v4 |
| 72 | + with: |
| 73 | + node-version: 18 |
| 74 | + registry-url: "https://registry.npmjs.org" |
| 75 | + cache: "pnpm" |
| 76 | + |
| 77 | + - name: Install dependencies |
| 78 | + run: pnpm install |
| 79 | + |
| 80 | + - name: Build Packages |
| 81 | + run: pnpm run build |
| 82 | + |
| 83 | + - name: Bump Package Versions |
| 84 | + id: changesets |
| 85 | + run: | |
| 86 | + pnpm exec changeset status --output status.output.json 2>&1 |
| 87 | + pnpm exec changeset version --snapshot ${{ steps.getSnapshotName.outputs.result }} |
| 88 | +
|
| 89 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 90 | + echo "status<<$EOF" >> $GITHUB_OUTPUT |
| 91 | + echo "$(cat status.output.json)" >> $GITHUB_OUTPUT |
| 92 | + echo "$EOF" >> $GITHUB_OUTPUT |
| 93 | + env: |
| 94 | + # Needs access to run the script |
| 95 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + # Disable color |
| 97 | + FORCE_COLOR: 0 |
| 98 | + NO_COLOR: 1 |
| 99 | + |
| 100 | + - name: Publish Release |
| 101 | + id: publish |
| 102 | + run: | |
| 103 | + GITHUB_ACTIONS=0 pnpm run build > build.output.txt 2>&1 |
| 104 | + pnpm exec changeset publish --tag experimental--${{ steps.getSnapshotName.outputs.result }} > publish.output.txt 2>&1 |
| 105 | +
|
| 106 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 107 | +
|
| 108 | + echo "build<<$EOF" >> $GITHUB_OUTPUT |
| 109 | + echo "$(cat build.output.txt)" >> $GITHUB_OUTPUT |
| 110 | + echo "$EOF" >> $GITHUB_OUTPUT |
| 111 | + cat build.output.txt |
| 112 | +
|
| 113 | + echo "publish<<$EOF" >> $GITHUB_OUTPUT |
| 114 | + echo "$(cat publish.output.txt)" >> $GITHUB_OUTPUT |
| 115 | + echo "$EOF" >> $GITHUB_OUTPUT |
| 116 | + cat publish.output.txt |
| 117 | + env: |
| 118 | + # Needs access to publish to npm |
| 119 | + NPM_TOKEN: ${{ secrets.CHANGESETS_RELEASE_PR_WORKFLOW_NPM_TOKEN }} |
| 120 | + NODE_AUTH_TOKEN: ${{ secrets.CHANGESETS_RELEASE_PR_WORKFLOW_NPM_TOKEN }} |
| 121 | + # Disable color |
| 122 | + FORCE_COLOR: 0 |
| 123 | + NO_COLOR: 1 |
| 124 | + |
| 125 | + - name: Pull Request Notification |
| 126 | + uses: actions/github-script@v7 |
| 127 | + env: |
| 128 | + TAG: ${{ steps.getSnapshotName.outputs.result }} |
| 129 | + STATUS_DATA: ${{ steps.changesets.outputs.status }} |
| 130 | + BUILD_LOG: ${{ steps.publish.outputs.build }} |
| 131 | + PUBLISH_LOG: ${{ steps.publish.outputs.publish }} |
| 132 | + with: |
| 133 | + script: | |
| 134 | + let changeset = { releases: [] }; |
| 135 | + try { |
| 136 | + changeset = JSON.parse(process.env.STATUS_DATA); |
| 137 | + } catch (e) {} |
| 138 | + let message = 'Snapshots have been released for the following packages:' |
| 139 | + for (const release of changeset.releases) { |
| 140 | + if (release.type === 'none') continue; |
| 141 | + message += `\n- \`${release.name}@experimental--${process.env.TAG}\``; |
| 142 | + } |
| 143 | +
|
| 144 | + function details(title, body) { |
| 145 | + message += '\n'; |
| 146 | + message += `<details><summary><strong>${title}</strong></summary>` |
| 147 | + message += '\n\n```\n'; |
| 148 | + message += body; |
| 149 | + message += '\n```\n\n</details>'; |
| 150 | + } |
| 151 | +
|
| 152 | + details('Publish Log', process.env.PUBLISH_LOG); |
| 153 | + details('Build Log', process.env.BUILD_LOG); |
| 154 | +
|
| 155 | + github.rest.issues.createComment({ |
| 156 | + issue_number: context.issue.number, |
| 157 | + owner: context.repo.owner, |
| 158 | + repo: context.repo.repo, |
| 159 | + body: message, |
| 160 | + }) |
0 commit comments