summaryrefslogtreecommitdiff
path: root/.github/workflows/snapshot-release.yml
blob: 3aea333cde0d4d84f0fa8f7a7eb7e2368f99a0c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: Create a Snapshot Release

on:
  issue_comment:
    types: [created]

defaults:
  run:
    shell: bash

env:
  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
  TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
  FORCE_COLOR: true

jobs:
  snapshot-release:
    name: Create a snapshot release of a pull request
    if: ${{ github.repository_owner == 'withastro' && github.event.issue.pull_request && startsWith(github.event.comment.body, '!preview') }}
    runs-on: ubuntu-latest
    steps:
      - name: "Check if user has admin access (only admins can publish snapshot releases)."
        uses: "lannonbr/repo-permission-check-action@2.0.0"
        with:
          permission: "admin"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract the snapshot name from comment body
        id: getSnapshotName
        uses: actions/github-script@v6
        with:
          script: |
            const splitComment = context.payload.comment.body.split(' ');
            splitComment.length !== 2 && (github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Invalid comment format. Expected: "!preview <one-word-snapshot-name>"',
            }) || core.setFailed('Invalid comment format. Expected: "!preview <one-word-snapshot-name>"'));
            return splitComment[1].trim();
          result-encoding: string

      - name: resolve pr refs
        id: refs
        uses: eficode/resolve-pr-refs@main
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/checkout@v3
        with:
          ref: ${{ steps.refs.outputs.head_ref }}

      - name: Setup PNPM
        uses: pnpm/action-setup@v2

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16
          registry-url: "https://registry.npmjs.org"
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install

      - name: Build Packages
        run: pnpm run build

      - name: Bump Package Versions
        id: changesets
        run: |
          pnpm exec changeset version --snapshot ${{ steps.getSnapshotName.outputs.result }} > changesets.output.txt 2>&1
          echo ::set-output name=result::`cat changesets.output.txt`
        env:
          # Needs access to run the script
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish Release
        id: publish
        run: |
          pnpm run release --tag next--${{ steps.getSnapshotName.outputs.result }} > publish.output.txt 2>&1
          echo ::set-output name=result::`cat publish.output.txt`
        env:
          # Needs access to publish to npm
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Pull Request Notification
        uses: actions/github-script@v6
        env:
          MESSAGE: ${{ steps.publish.outputs.result }}
        with:
          script: |
            console.log(process.env.MESSAGE);
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '```\n' + process.env.MESSAGE + '\n```',
            })