Merge pull request #14 from sicin/create-github-action #6
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: Build Next.js with PostgreSQL | |
on: | |
# Runs on pushes targeting the default branch | |
push: | |
branches: ["main"] | |
# Runs on any open or reopened pull request | |
pull_request: | |
types: [opened, reopened, synchronize] | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
env: | |
PRISMA_DATABASE_URL: ${{ secrets.PRISMA_DATABASE_URL }} | |
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
jobs: | |
# Build job | |
build: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:16.1-alpine3.18 | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: postgres | |
ports: | |
- 5435:5432 | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
environment: my_env | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Detect package manager | |
id: detect-package-manager | |
run: | | |
if [ -f "${{ github.workspace }}/package.json" ]; then | |
echo "manager=npm" >> $GITHUB_ENV | |
echo "command=ci" >> $GITHUB_ENV | |
echo "runner=npx --no-install" >> $GITHUB_ENV | |
exit 0 | |
elif [ -f "${{ github.workspace }}/yarn.lock" ]; then | |
echo "manager=yarn" >> $GITHUB_ENV | |
echo "command=install" >> $GITHUB_ENV | |
echo "runner=yarn" >> $GITHUB_ENV | |
exit 0 | |
else | |
echo "Unable to determine packager manager" | |
exit 1 | |
fi | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "20" | |
cache: ${{ env.manager }} | |
- name: Restore cache | |
uses: actions/cache@v3 | |
with: | |
path: | | |
.next/cache | |
# Generate a new cache whenever packages or source files change. | |
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} | |
# If source files changed but packages didn't, rebuild from a prior cache. | |
restore-keys: | | |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}- | |
- name: Install dependencies | |
run: ${{ env.manager }} ${{ env.command }} | |
- name: Synchronize Database Schema | |
run: ${{ env.runner }} prisma db push | |
env: | |
DATABASE_URL: ${{ secrets.PRISMA_DATABASE_URL }} | |
- name: Build with Next.js | |
run: ${{ env.runner }} next build |