name: Snapshot on: issue_comment: types: - created concurrency: ${{ github.workflow }}-${{ github.ref }} jobs: snapshot: name: Snapshot Release if: | github.event.issue.pull_request && (startsWith(github.event.comment.body, '/snapit') || startsWith(github.event.comment.body, '/snapshot-release')) runs-on: ubuntu-latest steps: - name: Enforce permission requirement uses: prince-chrismc/check-actor-permissions-action@72c9eb81384517cbc49d765edc200af3131897ce # v1.0.0 with: permission: write - name: Add initial reaction uses: peter-evans/create-or-update-comment@67dcc547d311b736a8e6c5c236542148a47adc3d # v2.1.1 with: comment-id: ${{ github.event.comment.id }} reactions: eyes - name: Get PR branch uses: xt0rted/pull-request-comment-branch@653a7d5ca8bd91d3c5cb83286063314d0b063b8e # v1.4.0 id: comment-branch - name: Set latest commit status as pending uses: myrotvorets/set-commit-status-action@7b093ccbb10e14939b7a4ae2630fe4cbc67c0651 # v2.0.1 with: sha: ${{ steps.comment-branch.outputs.head_sha }} token: ${{ secrets.GITHUB_TOKEN }} status: pending - name: Validate pull request uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 id: pr_data env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: script: | try { const pullRequest = await github.rest.pulls.get({ owner: context.repo.owner, repo: context.repo.repo, pull_number: context.issue.number, }) // Pull request from fork if (context.payload.repository.full_name !== pullRequest.data.head.repo.full_name) { const errorMessage = '`/snapit` is not supported on pull requests from forked repositories.' await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: errorMessage, }) core.setFailed(errorMessage) } } catch (err) { core.setFailed(`Request failed with error ${err}`) } - name: Checkout default branch uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 - name: Checkout pull request branch run: hub pr checkout ${{ github.event.issue.number }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Reset changeset entries on changeset-release/main branch run: | if [[ $(git branch --show-current) == 'changeset-release/main' ]]; then git checkout origin/main -- .changeset fi - name: Setup Node.js uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3.9.1 with: node-version: '18.17.1' - name: Install dependencies run: yarn --frozen-lockfile - name: Create an .npmrc env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: | cat << EOF > "$HOME/.npmrc" //registry.npmjs.org/:_authToken=$NPM_TOKEN EOF - name: Create and publish snapshot release uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 id: snapshot-release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: script: | const execa = require('execa') await execa.command('yarn changeset version --snapshot snapshot-release', { stdio: 'inherit' }) const releaseProcess = execa.command('yarn release --no-git-tags --snapshot --tag snapshot-release') releaseProcess.stdout.pipe(process.stdout) const {stdout} = await releaseProcess const newTags = Array .from(stdout.matchAll(/New tag:\s+([^\s\n]+)/g)) .map(([_, tag]) => tag) if (newTags.length) { const multiple = newTags.length > 1 const body = ( `🫰✨ **Thanks @${context.actor}! ` + `Your snapshot${multiple ? 's have' : ' has'} been published to npm.**\n\n` + `Test the snapshot${multiple ? 's' : ''} by updating your \`package.json\` ` + `with the newly published version${multiple ? 's' : ''}:\n` + newTags.map(tag => ( '```sh\n' + `yarn add ${tag}\n` + '```' )).join('\n') ) await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body, }) core.setOutput('succeeded', 'true') } else { await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `💥 **Snapshot release unsuccessful!** No tags have been found.\n\n` + 'Try running the command below and committing your changes.\n\n' + '```sh\n' + 'yarn changeset\n' + '```', }) core.setOutput('succeeded', 'false') } - name: Add success reaction uses: peter-evans/create-or-update-comment@67dcc547d311b736a8e6c5c236542148a47adc3d # v2.1.1 if: ${{ steps.snapshot-release.outputs.succeeded == 'true' }} with: comment-id: ${{ github.event.comment.id }} reactions: rocket - name: Add failure reaction uses: peter-evans/create-or-update-comment@67dcc547d311b736a8e6c5c236542148a47adc3d # v2.1.1 if: ${{ steps.snapshot-release.outputs.succeeded == 'false' }} with: comment-id: ${{ github.event.comment.id }} reactions: confused - name: Fail workflow if snapshot failed uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 if: ${{ steps.snapshot-release.outputs.succeeded == 'false' }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: script: | core.setFailed('No snapshot tags have been found') - name: Set latest commit status as ${{ job.status }} uses: myrotvorets/set-commit-status-action@7b093ccbb10e14939b7a4ae2630fe4cbc67c0651 # v2.0.1 if: always() with: sha: ${{ steps.comment-branch.outputs.head_sha }} token: ${{ secrets.GITHUB_TOKEN }} status: ${{ job.status }}