Skip to content

Commit

Permalink
CI / Simplify project setup (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
suddenlyGiovanni committed May 7, 2024
1 parent 54b1134 commit 49142ef
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 121 deletions.
9 changes: 9 additions & 0 deletions .github/actions/fly-pr-review-apps/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine

RUN apk add --no-cache curl jq

RUN curl -L https://fly.io/install.sh | FLYCTL_INSTALL=/usr/local sh

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
44 changes: 44 additions & 0 deletions .github/actions/fly-pr-review-apps/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: "PR Review Apps on fly.io"
description: "Deploy temporary apps from pull requests on Fly.io"
author: Fly
branding:
icon: "upload-cloud"
color: "purple"
runs:
using: "docker"
image: "Dockerfile"
inputs:
name:
description: Fly app name
image:
description: Optional pre-existing Docker image to use
config:
description: Optional path to a custom Fly toml config. Config path should be relative to `path` parameter, if specified.
build_args:
description: Optional Docker --build-arg
build_secrets:
description: Optional Docker --build-secret
region:
description: Region to launch the app in (alternatively, set the env FLY_REGION)
org:
description: Organization to launch the app in (alternatively, set the env FLY_ORG)
path:
description: path to a directory containing a fly.toml to clone
postgres:
description: Optionally attach the app to a pre-existing Postgres cluster on Fly
secrets:
description: Secrets to be set on the app at runtime. Separate multiple secrets with a space
vmsize:
description: Set app VM to a named size, eg. shared-cpu-1x, dedicated-cpu-1x, dedicated-cpu-2x etc. Takes precedence over cpu, cpu kind, and memory inputs.
cpu:
description: Set app VM CPU (defaults to 1 cpu)
default: 1
cpukind:
description: Set app VM CPU kind - shared or performance. (defaults to shared)
default: shared
memory:
description: Set app VM memory in megabytes (defaults to 256 megabytes)
default: 256
ha:
description: Create spare machines that increases app availability (default false)
default: false
86 changes: 86 additions & 0 deletions .github/actions/fly-pr-review-apps/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/sh -l

set -ex

if [ -n "$INPUT_PATH" ]; then
# Allow user to change directories in which to run Fly commands.
cd "$INPUT_PATH" || exit
fi

PR_NUMBER=$(jq -r .number /github/workflow/event.json)
if [ -z "$PR_NUMBER" ]; then
echo "This action only supports pull_request actions."
exit 1
fi

GITHUB_REPOSITORY_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/}
EVENT_TYPE=$(jq -r .action /github/workflow/event.json)

# Default the Fly app name to pr-{number}-{repo_owner}-{repo_name}
app="${INPUT_NAME:-pr-$PR_NUMBER-$GITHUB_REPOSITORY_OWNER-$GITHUB_REPOSITORY_NAME}"
# Change underscores to hyphens.
app="${app//_/-}"
region="${INPUT_REGION:-${FLY_REGION:-iad}}"
org="${INPUT_ORG:-${FLY_ORG:-personal}}"
image="$INPUT_IMAGE"
config="${INPUT_CONFIG:-fly.toml}"
build_args=""
build_secrets=""

if ! echo "$app" | grep "$PR_NUMBER"; then
echo "For safety, this action requires the app's name to contain the PR number."
exit 1
fi

# PR was closed - remove the Fly app if one exists and exit.
if [ "$EVENT_TYPE" = "closed" ]; then
flyctl apps destroy "$app" -y || true
exit 0
fi

if [ -n "$INPUT_BUILD_ARGS" ]; then
for ARG in $(echo "$INPUT_BUILD_ARGS" | tr " " "\n"); do
build_args="$build_args --build-arg ${ARG}"
done
fi

if [ -n "$INPUT_BUILD_SECRETS" ]; then
for ARG in $(echo "$INPUT_BUILD_SECRETS" | tr " " "\n"); do
build_secrets="$build_secrets --build-secret ${ARG}"
done
fi

# Deploy the Fly app, creating it first if needed.
if ! flyctl status --app "$app"; then
# Backup the original config file since 'flyctl launch' messes up the [build.args] section
cp "$config" "$config.bak"
flyctl launch --no-deploy --copy-config --name "$app" --image "$image" --region "$region" --org "$org" ${build_args} ${build_secrets}
# Restore the original config file
cp "$config.bak" "$config"
fi

if [ -n "$INPUT_SECRETS" ]; then
echo $INPUT_SECRETS | tr " " "\n" | flyctl secrets import --app "$app"
fi

# Attach postgres cluster to the app if specified.
if [ -n "$INPUT_POSTGRES" ]; then
flyctl postgres attach "$INPUT_POSTGRES" --app "$app" || true
fi

# Trigger the deploy of the new version.
echo "Contents of config $config file: " && cat "$config"
if [ -n "$INPUT_VM" ]; then
flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} --vm-size "$INPUT_VMSIZE"
else
flyctl deploy --config "$config" --app "$app" --regions "$region" --image "$image" --strategy immediate --ha=$INPUT_HA ${build_args} ${build_secrets} --vm-cpu-kind "$INPUT_CPUKIND" --vm-cpus $INPUT_CPU --vm-memory "$INPUT_MEMORY"
fi

# Make some info available to the GitHub workflow.
flyctl status --app "$app" --json >status.json
hostname=$(jq -r .Hostname status.json)
appid=$(jq -r .ID status.json)
echo "hostname=$hostname" >> $GITHUB_OUTPUT
echo "url=https://$hostname" >> $GITHUB_OUTPUT
echo "id=$appid" >> $GITHUB_OUTPUT
echo "name=$app" >> $GITHUB_OUTPUT
27 changes: 27 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 💉Setup the project
description: Perform standard setup and install dependencies using pnpm.
inputs:
github_token:
description: 'GitHub token'
required: true
runs:
using: composite
steps:
- name: 🗂️ Install pnpm
uses: pnpm/action-setup@v3
with:
package_json_file: 'package.json'
run_install: false

- name: 🛠️ Install node
uses: actions/setup-node@v4
with:
cache: 'pnpm'
node-version-file: '.nvmrc'
registry-url: 'https://npm.pkg.github.com'

- name: 🧩 Install dependencies
shell: bash
env:
NODE_AUTH_TOKEN: ${{ inputs.github_token }}
run: pnpm install --frozen-lockfile --prod=false
28 changes: 3 additions & 25 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,10 @@ jobs:
with:
fetch-depth: 0

- name: Install Node.js
uses: actions/setup-node@v4
- name: 🧩 Install dependencies
uses: ./.github/actions/setup
with:
node-version-file: '.nvmrc'

- name: Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to Chromatic
uses: chromaui/action@latest
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/deply-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ jobs:

- name: Deploy PR app to Fly.io
id: deploy
uses: superfly/fly-pr-review-apps@1.2.0
uses: ./.github/actions/fly-pr-review-apps
with:
config: ./apps/web/fly.review.toml
path: './apps/web'
config: ./fly.review.toml
name: suddenlygiovanni-dev-${{ github.event.number }}
secrets: GH_PACKAGES_TOKEN=${{ secrets.GH_PACKAGES_TOKEN }} GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
build_args: GH_PACKAGES_TOKEN=${{ secrets.GH_PACKAGES_TOKEN }}
45 changes: 8 additions & 37 deletions .github/workflows/pull-request-checks-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,12 @@ jobs:
with:
fetch-depth: 0

- name: 📦 Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: false

- name: 🗂️ Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: "pnpm"
registry-url: 'https://npm.pkg.github.com'

- name: 🗄️ Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: 🧩 Install dependencies
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: pnpm install --frozen-lockfile --prod=false
- name: 🧬 Setup Biome
uses: biomejs/setup-biome@v2

- name: 🧬 check code for errors (with Biome)
run: |
pnpm exec biome ci \
biome ci \
--formatter-enabled=false \
--linter-enabled=true \
--organize-imports-enabled=false \
Expand All @@ -57,6 +29,11 @@ jobs:
--diagnostic-level=error \
.
- name: 🧩 Install dependencies
uses: ./.github/actions/setup
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: 🚨 Lint code (with ESLint)
continue-on-error: true
run: pnpm lint
Expand All @@ -77,11 +54,5 @@ jobs:
directory: coverage
fail_ci_if_error: false

- name: 🧪 Run integration tests
run: pnpm test:integration

- name: 🧪 Run smoke tests
run: pnpm test:smoke

- name: 🏗️ Build project
run: pnpm build
32 changes: 10 additions & 22 deletions .github/workflows/push-checks-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,19 @@ jobs:
with:
fetch-depth: 0

- name: 📦 Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: false

- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: "pnpm"
registry-url: 'https://npm.pkg.github.com'


- name: 🧩 Install dependencies
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: pnpm install --frozen-lockfile --prod=false
- name: 🧬 Setup Biome
uses: biomejs/setup-biome@v2

- name: 👣 Retrieve Git REF
run: echo "GIT_REF=$(git rev-parse origin/main)" >> $GITHUB_ENV

- name: 🐛 Debug Git REF
run: echo "${{ env.GIT_REF }}"
# - name: 🐛 Debug Git REF
# run: echo "${{ env.GIT_REF }}"

- name: 🧬 Lint code (with Biome)
continue-on-error: true
run: |
pnpm exec biome check \
biome check \
--apply \
--changed \
--since=${{ env.GIT_REF }} \
Expand All @@ -68,7 +52,7 @@ jobs:
- name: 💅 Format code (with Biome)
continue-on-error: true
run: |
pnpm exec biome check \
biome check \
--apply \
--changed \
--since=${{ env.GIT_REF }} \
Expand All @@ -87,6 +71,10 @@ jobs:
with:
commit_message: "chore: lint code with Biome [skip ci]"

- name: 🧩 Install dependencies
uses: ./.github/actions/setup
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: 🔍 Typecheck code
continue-on-error: true
Expand Down
30 changes: 6 additions & 24 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,10 @@ jobs:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 📦 Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9

- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: "pnpm"

- name: 🧩 Install dependencies
run: pnpm install --frozen-lockfile --prod=false
uses: ./.github/actions/setup
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: 🐶 reviewdog with tsc (TypeScript) - packages/ui
uses: EPMatt/reviewdog-action-tsc@v1
Expand Down Expand Up @@ -194,19 +185,10 @@ jobs:
- name: 📥 Checkout code
uses: actions/checkout@v4

- name: 📦 Install pnpm
uses: pnpm/action-setup@v3
with:
version: 9

- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: "pnpm"

- name: 🧩 Install dependencies
run: pnpm install --frozen-lockfile
uses: ./.github/actions/setup
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: 🐶 reviewdog with ESLint (TypeScript) - apps/web
uses: reviewdog/action-eslint@v1
Expand Down

0 comments on commit 49142ef

Please sign in to comment.