Preview #422
This file contains 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: Preview | |
on: | |
issue_comment: | |
types: [created] | |
pull_request: | |
branches: [main] | |
jobs: | |
check: | |
runs-on: ubuntu-latest | |
outputs: | |
triggered: ${{ steps.check.outputs.triggered }} | |
triggered_by_ci: ${{ steps.checkCI.outputs.triggered }} | |
steps: | |
- name: Check if the comment was a command, and react with 👀 if it was | |
if: github.event.issue.pull_request | |
uses: famly/pull-request-comment-trigger@master | |
id: check | |
with: | |
trigger: "/preview" | |
reaction: eyes | |
prefix_only: true | |
env: | |
GITHUB_TOKEN: "${{ secrets.CI_GITHUB_TOKEN }}" | |
- name: Check if it was CI comment | |
if: github.event.issue.pull_request | |
uses: famly/pull-request-comment-trigger@master | |
id: checkCI | |
with: | |
trigger: "/preview CI" | |
env: | |
GITHUB_TOKEN: "${{ secrets.CI_GITHUB_TOKEN }}" | |
- name: Check if it was pull request open to main branch | |
if: github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
/preview CI | |
run-preview: | |
runs-on: ubuntu-latest | |
needs: [check] | |
if: ${{ needs.check.outputs.triggered == 'true' }} | |
concurrency: | |
group: ${{ github.event.issue.number || github.event.pull_request.number }} | |
cancel-in-progress: true | |
steps: | |
- name: Add info about starting new preview | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
**🖼️ Preview Environment:** _Starting..._ | |
- name: Extract current commit | |
id: sha | |
uses: actions/github-script@v7 | |
with: | |
result-encoding: string | |
script: | | |
const { owner, repo, number: pull_number } = context.issue; | |
const pr = await github.rest.pulls.get({ | |
owner, | |
repo, | |
pull_number, | |
}); | |
return pr.data.head.sha | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ steps.sha.outputs.result }} | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Start frontend tunnel | |
id: expose-tunnel | |
uses: codetalkio/[email protected] | |
with: | |
service: localhost.run | |
port: 3000 | |
- name: Fix tunnel url | |
id: tunnel-url | |
run: | | |
url=${{ steps.expose-tunnel.outputs.tunnel-url }} | |
fixedUrl=${url::-6} | |
echo "tunnel-url=${fixedUrl}" >> $GITHUB_OUTPUT | |
- name: Hide preview comment | |
uses: actions/github-script@v7 | |
if: ${{ needs.check.outputs.triggered_by_ci != 'true' }} | |
with: | |
result-encoding: string | |
github-token: ${{ secrets.CI_GITHUB_TOKEN }} | |
script: | | |
const { node_id } = context.payload.comment; | |
const query = ` | |
mutation minimizeComment($id: ID!) { | |
minimizeComment(input: { classifier: OUTDATED, subjectId: $id }) { | |
clientMutationId | |
} | |
} | |
`; | |
await github.graphql(query, { id: node_id }); | |
- name: Add info about dependency install | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
**🖼️ Preview Environment:** _Installing dependencies..._ | |
- name: Install | |
run: npm ci | |
- name: Cache build | |
uses: actions/cache@v3 | |
with: | |
path: ${{ github.workspace }}/frontend/.next/cache | |
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} | |
restore-keys: | | |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}- | |
- name: Add info about build | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
recreate: true | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
**🖼️ Preview Environment:** _Building..._ | |
- name: Build frontend | |
run: | | |
touch .env | |
echo NEXT_PUBLIC_DIRECTUS_URL="https://directus.universe.nexus" >> .env | |
npm run build | |
- name: Starting app | |
run: npm start & | |
- name: Comment PR | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
recreate: true | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
**🖼️ Preview Environment:** ${{ steps.tunnel-url.outputs.tunnel-url }} | |
You can stop the preview by clicking "Cancel" on the [GitHub Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}). | |
- name: Wait for 60 minutes before timing out (unless overwritten) | |
run: | | |
echo "Tunnel URL is ${{ steps.tunnel-url.outputs.tunnel-url }}" | |
timeout 61m sleep 3600 | |
# Gracefully exit if the timeout hits. | |
exit 0 | |
- name: Close comment | |
uses: marocchino/sticky-pull-request-comment@v2 | |
if: ${{ success() }} | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
🖼️ Preview Environment exceeded the maximum time of 60 minutes. To start it again, comment `/preview` on this PR. | |
- name: Close comment when cancelled | |
uses: marocchino/sticky-pull-request-comment@v2 | |
if: ${{ cancelled() }} | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
🖼️ Preview Environment was cancelled (because of newer preview or manual cancellation). To start it again, comment `/preview` on this PR. | |
- name: Close comment when failed | |
uses: marocchino/sticky-pull-request-comment@v2 | |
if: ${{ failure() }} | |
with: | |
recreate: true | |
number: ${{ github.event.issue.number || github.event.pull_request.number }} | |
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }} | |
message: | | |
🖼️ Preview Environment failed to start. You can see more info here [GitHub Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}). To start it again, comment `/preview` on this PR. |