Skip to content

wip

wip #144

Workflow file for this run

name: Deploy to production
env:
DEPLOY_HOST: 216.238.103.47
on:
push:
branches:
- main
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.detect.outputs.matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine changed apps
id: detect
run: |
node <<'NODE' > changed-apps.json
const { execSync } = require('child_process');
const apps = [
{ id: 'home', folder: 'apps/home', package: 'home', outputDir: 'dist', remote: '/var/www/guias/home/' },
{ id: 'next-auth', folder: 'apps/next-auth', package: 'next-auth', outputDir: 'dist', remote: '/var/www/guias/next-auth/' },
{ id: 'next-introdutorio', folder: 'apps/next-introdutorio', package: 'next-introdutorio', outputDir: 'dist', remote: '/var/www/guias/next-introdutorio/' },
{ id: 'next-intermediario', folder: 'apps/next-intermediario', package: 'next-intermediario', outputDir: 'dist', remote: '/var/www/guias/next-intermediario/' },
{ id: 'ts-no-react', folder: 'apps/ts-no-react', package: 'docs-workshop', outputDir: 'dist', remote: '/var/www/guias/ts-no-react/' },
{ id: 'zod', folder: 'apps/zod', package: 'zod', outputDir: 'dist', remote: '/var/www/guias/zod/' },
{ id: 'react-hook-form', folder: 'apps/react-hook-form', package: 'react-hook-form', outputDir: 'dist', remote: '/var/www/guias/react-hook-form/' },
{ id: 'react-intro', folder: 'apps/react-intro', package: 'react-intro', outputDir: 'dist', remote: '/var/www/guias/react-intro/' },
{ id: 'tailwindcss', folder: 'apps/tailwindcss', package: 'tailwindcss', outputDir: 'dist', remote: '/var/www/guias/tailwindcss/' },
{ id: 'next-v15', folder: 'apps/next-v15', package: 'next-v15', outputDir: 'dist', remote: '/var/www/guias/next-v15/' },
{ id: 'next-v16-fumadocs', folder: 'apps/next-v16-fumadocs', package: 'next-v16-fumadocs', outputDir: 'out', remote: '/var/www/guias/next-v16/' },
{ id: 'codando-com-ia', folder: 'apps/codando-com-ia', package: 'codando-com-ia', outputDir: 'out', remote: '/var/www/guias/codando-com-ia/' },
{ id: 'ia-para-devs', folder: 'apps/ia-para-devs', package: 'ia-para-devs', outputDir: 'dist', remote: '/var/www/guias/ia-para-devs/' }
];
const before = process.env.GITHUB_EVENT_BEFORE;
let range;
if (!before || /^0+$/.test(before)) {
range = 'HEAD^...HEAD';
} else {
range = `${before}...HEAD`;
}
let files = [];
try {
files = execSync(`git diff --name-only ${range}`)
.toString()
.trim()
.split('\n')
.filter(Boolean);
} catch (error) {
files = execSync('git show --pretty="" --name-only HEAD')
.toString()
.trim()
.split('\n')
.filter(Boolean);
}
const sharedPatterns = [
/^package\.json$/,
/^pnpm-lock\.yaml$/,
/^pnpm-workspace\.yaml$/,
/^tsconfig\.json$/,
/^tsconfig\.base\.json$/,
/^\.github\/workflows\//
];
const sharedChanged = files.some((file) => sharedPatterns.some((pattern) => pattern.test(file)));
const changed = apps.filter((app) => {
if (sharedChanged) return true;
return files.some((file) => file.startsWith(`${app.folder}/`));
});
process.stdout.write(JSON.stringify(changed));
NODE
MATRIX=$(cat changed-apps.json)
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
build:
needs: detect-changes
if: needs.detect-changes.outputs.matrix != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- name: Restore Next.js cache (${{ matrix.app.id }})
if: ${{ matrix.app.outputDir == 'out' }}
uses: actions/cache@v4
with:
path: ${{ matrix.app.folder }}/.next/cache
key: ${{ runner.os }}-${{ matrix.app.id }}-next-${{ hashFiles('pnpm-lock.yaml', format('{0}/next.config.*', matrix.app.folder)) }}
restore-keys: |
${{ runner.os }}-${{ matrix.app.id }}-next-
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v4
with:
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- name: Build ${{ matrix.app.id }}
run: pnpm --filter ${{ matrix.app.package }} run build
- name: Upload build artifact (${{ matrix.app.id }})
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.app.id }}-build
path: ${{ matrix.app.folder }}/${{ matrix.app.outputDir }}/
deploy:
needs: [detect-changes, build]
if: needs.detect-changes.outputs.matrix != '[]'
runs-on: ubuntu-latest
strategy:
matrix:
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- name: Download artifact (${{ matrix.app.id }})
uses: actions/download-artifact@v4
with:
name: ${{ matrix.app.id }}-build
path: deploy/${{ matrix.app.id }}
- name: Locate build output (${{ matrix.app.id }})
id: locate-build
run: |
set -euo pipefail
PREFERRED="deploy/${{ matrix.app.id }}/${{ matrix.app.folder }}/${{ matrix.app.outputDir }}"
ALT="deploy/${{ matrix.app.id }}/${{ matrix.app.outputDir }}"
if [ -d "$PREFERRED" ]; then
DIR="$PREFERRED"
elif [ -d "$ALT" ]; then
DIR="$ALT"
else
DIR=$(find "deploy/${{ matrix.app.id }}" -type d -name "${{ matrix.app.outputDir }}" -print -quit)
fi
if [ -z "$DIR" ]; then
# Some artifacts upload the contents of the output directory directly.
DIR="deploy/${{ matrix.app.id }}"
fi
if [ ! -d "$DIR" ]; then
echo "No directory named '${{ matrix.app.outputDir }}' found in artifact" >&2
exit 1
fi
echo "dir=$DIR" >> "$GITHUB_OUTPUT"
- name: Deploy ${{ matrix.app.id }}
uses: burnett01/rsync-deployments@5.2
with:
switches: -avzr --quiet --delete
path: ${{ steps.locate-build.outputs.dir }}/
remote_path: ${{ matrix.app.remote }}
remote_host: ${{ env.DEPLOY_HOST }}
remote_user: ${{ secrets.USERNAME }}
remote_key: ${{ secrets.SSH_KEY }}