graceful cncrrency #52
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 Deployment | |
on: [ pull_request ] | |
permissions: | |
contents: write | |
env: | |
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} | |
NEON_PROJECT_ID: ${{ secrets.NEON_PROJECT_ID }} | |
jobs: | |
deploy-preview-backend: | |
name: Deploy Backend Preview | |
runs-on: ubuntu-latest | |
# Only run one deployment at a time per PR. | |
concurrency: | |
group: pr-${{ github.event.number }} | |
steps: | |
# Step 1: Checkout code | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
# Step 2: Get the branch name | |
- name: Get branch name | |
id: branch-name | |
uses: tj-actions/branch-names@v8 | |
# Step 3: Create a Neon DB branch | |
- name: Create Neon DB branch | |
id: create-branch | |
uses: neondatabase/create-branch-action@v5 | |
with: | |
project_id: ${{ env.NEON_PROJECT_ID }} | |
database: my-awesome-blog-db | |
username: "my-awesome-blog-db_owner" # Replace with your DB role | |
branch_name: preview/${{ steps.branch-name.outputs.current_branch }} | |
api_key: ${{ secrets.NEON_API_KEY }} | |
# Step 4: Set JDBC_DATABASE_URL | |
- name: Set JDBC_DATABASE_URL | |
run: | | |
# Extract components from the provided Neon database URL | |
NEON_DB_URL="${{ steps.create-branch.outputs.db_url }}" | |
USERNAME=$(echo $NEON_DB_URL | sed -n 's|.*//\([^:]*\):.*|\1|p') | |
PASSWORD=$(echo $NEON_DB_URL | sed -n 's|.*//[^:]*:\([^@]*\)@.*|\1|p') | |
HOST=$(echo $NEON_DB_URL | sed -n 's|.*@\(.*\)/.*|\1|p') | |
DATABASE=$(echo $NEON_DB_URL | sed -n 's|.*/\([^?]*\)?.*|\1|p') | |
# Construct the JDBC URL | |
JDBC_URL="jdbc:postgresql://$HOST:5432/$DATABASE?user=$USERNAME&password=$PASSWORD&sslmode=require" | |
# Set the DATABASE_URL environment variable | |
echo "DATABASE_URL=$JDBC_URL" >> $GITHUB_ENV | |
# Step 5: Build and Test Backend | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
cache: maven | |
- name: Build and Test Backend | |
working-directory: ./backend | |
run: mvn clean verify | |
# Step 6: Deploy backend to Fly.io preview app | |
- name: Deploy Backend Preview | |
id: deploy | |
uses: superfly/fly-pr-review-apps@1.3.0 | |
with: | |
ha: 'true' | |
region: cdg | |
memory: '512' | |
cpukind: 'shared' | |
vmsize: 'shared-cpu-1x' | |
path: ./backend | |
secrets: DATABASE_URL=${{ env.DATABASE_URL }} |