Files
books/.github/workflows/deploy-frontend.yml
T

167 lines
5.1 KiB
YAML

name: Deploy Frontend to Prod
on:
workflow_dispatch:
inputs:
frontend_image:
description: "Frontend image tag (full image reference)"
required: false
apply:
description: "Apply changes after plan"
required: false
default: "true"
workflow_run:
workflows: [ "Frontend CD" ]
types: [ completed ]
jobs:
terraform-plan:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: easynode-debian
outputs:
no_changes: ${{ steps.check-changes.outputs.no_changes }}
permissions:
contents: read
env:
CONVEX_URL: ""
CLERK_PUBLISHABLE_KEY: ""
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Setup kubectl
uses: azure/setup-kubectl@v4
- name: Setup Doppler CLI
uses: dopplerhq/cli-action@v4
- name: Setup Kubeconfig
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: |
mkdir -p ~/.kube
doppler run --config prd_secrets -- bash -c 'echo "$KUBECONFIG_DATA" | base64 -d > ~/.kube/config'
chmod 600 ~/.kube/config
- name: Validate cluster access
run: |
kubectl cluster-info
kubectl get namespace books
- name: Determine deployment values
id: deploy-vars
run: |
frontend_image="${{ github.event.inputs.frontend_image }}"
latest_frontend_tag="$(git tag --list 'v*' | sort -V | tail -n1)"
echo "latest frontend tag=$latest_frontend_tag"
if [ -z "$frontend_image" ]; then
frontend_image="docker.io/rmcampos/books:$latest_frontend_tag"
fi
echo "Resolved frontend_image=$frontend_image"
echo "frontend_image=$frontend_image" >> "$GITHUB_OUTPUT"
- name: Terraform Fmt -check -diff
working-directory: terraform
run: terraform fmt -check -diff
- name: Terraform Init
working-directory: terraform
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: doppler run --config prd_secrets -- terraform init -input=false
- name: Terraform Validate
working-directory: terraform
run: terraform validate
- name: Fetch public tokens
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_TOKENS }}
run: |
doppler secrets download --no-file --format env \
--config prd_tokens | sed 's/"//g' >> $GITHUB_ENV
- name: Terraform Plan
id: check-changes
working-directory: terraform
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: |
doppler run --config prd_secrets -- \
timeout 1m terraform plan -input=false -out=tfplan \
-var="convex_url=${{ env.CONVEX_URL }}" \
-var="clerk_publishable_key=${{ env.CLERK_PUBLISHABLE_KEY }}" \
-var="frontend_image=${{ steps.deploy-vars.outputs.frontend_image }}"
terraform show -json tfplan > tfplan.json
if jq -e '.resource_changes | length == 0' tfplan.json >/dev/null; then
echo "no_changes=true" >> "$GITHUB_OUTPUT"
echo "No changes to apply."
exit 0
else
echo "Changes detected. Proceeding with apply"
echo "no_changes=false" >> "$GITHUB_OUTPUT"
fi
- name: Upload plan artifact
uses: actions/upload-artifact@v3
with:
name: tfplan
path: terraform/tfplan
terraform-apply:
runs-on: easynode-debian
needs: terraform-plan
if: >
(github.event_name == 'push' || github.event_name == 'workflow_run' || inputs.apply == 'true')
&& needs.terraform-plan.outputs.no_changes == 'false'
environment:
name: production
url: https://books.darkroasted.vps-kinghost.net
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Download plan artifact
uses: actions/download-artifact@v3
with:
name: tfplan
path: terraform
- name: Setup Doppler CLI
uses: dopplerhq/cli-action@v4
- name: Setup Kubeconfig
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: |
mkdir -p ~/.kube
doppler run --config prd_secrets -- bash -c 'echo "$KUBECONFIG_DATA" | base64 -d > ~/.kube/config'
chmod 600 ~/.kube/config
- name: Terraform Init
working-directory: terraform
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: doppler run --config prd_secrets -- terraform init -input=false
- name: Terraform Apply
working-directory: terraform
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: doppler run --config prd_secrets -- timeout 1m terraform apply tfplan