Skip to content

Preview

Preview #339

Workflow file for this run

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'
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' }}
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
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@v6
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@v3
with:
ref: ${{ steps.sha.outputs.result }}
- uses: actions/setup-node@v3
with:
node-version: 16
- 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@v6
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 dependencies
uses: bahmutov/npm-install@v1
- 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 NEXTAUTH_URL=${{ steps.tunnel-url.outputs.tunnel-url }} >> .env
echo NEXTAUTH_SECRET=secret >> .env
echo GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }} >> .env
echo GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} >> .env
echo DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres" >> .env
echo SMTP_HOST="host" >> .env
echo SMTP_USERNAME="username" >> .env
echo SMTP_PASSWORD="password" >> .env
echo EMAIL_FROM="emailfrom" >> .env
npm run build
npm run db:push
npm run db:seed
- 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.