Files
ledger-finance/.github/workflows/deploy.yml
T

110 lines
3.7 KiB
YAML

name: Deploy to prod
concurrency:
group: deploy-production
cancel-in-progress: true
on:
workflow_dispatch:
workflow_run:
workflows: [ "Backend CI", "Frontend CI" ]
types: [ completed ]
jobs:
terraform-plan:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: easynode-debian
outputs:
has_changes: ${{ steps.check-changes.outputs.has_changes }}
permissions:
contents: read
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 -- bash -c 'echo "$KUBECONFIG_DATA" | base64 -d > ~/.kube/config'
chmod 600 ~/.kube/config
- name: Validate cluster access
run: |
kubectl cluster-info
kubectl get namespace ledger-finance
- name: Resolve latest Docker tags
id: resolve-tags
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: |
BACKEND_TAG=$(doppler run --config prd -- bash .github/scripts/get-latest-tag.sh rmcampos/ledger-backend)
FRONTEND_TAG=$(doppler run --config prd -- bash .github/scripts/get-latest-tag.sh rmcampos/ledger-frontend)
echo "backend_tag=$BACKEND_TAG" >> "$GITHUB_OUTPUT"
echo "frontend_tag=$FRONTEND_TAG" >> "$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 -- terraform init -input=false
- name: Terraform Validate
working-directory: terraform
run: terraform validate
- name: Terraform Plan
id: check-changes
working-directory: terraform
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
BACKEND_IMAGE: rmcampos/ledger-backend:${{ steps.resolve-tags.outputs.backend_tag }}
FRONTEND_IMAGE: rmcampos/ledger-frontend:${{ steps.resolve-tags.outputs.frontend_tag }}
run: |
doppler run --config prd -- bash -c '
TF_VAR_db_user="$TF_VAR_DB_USER" \
TF_VAR_db_password="$TF_VAR_DB_PASSWORD" \
TF_VAR_db_name="$TF_VAR_DB_NAME" \
TF_VAR_r2_access_key="$AWS_ACCESS_KEY_ID" \
TF_VAR_r2_secret_key="$AWS_SECRET_ACCESS_KEY" \
TF_VAR_jwt_private_key="$JWT_PRIVATE_KEY" \
TF_VAR_jwt_public_key="$JWT_PUBLIC_KEY" \
TF_VAR_backend_image="$BACKEND_IMAGE" \
TF_VAR_frontend_image="$FRONTEND_IMAGE" \
timeout 1m terraform plan -input=false -out=tfplan
'
terraform show -json tfplan > tfplan.json
if jq -e '.resource_changes | length == 0' tfplan.json >/dev/null; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
echo "No changes to apply."
exit 0
else
echo "Changes detected. Proceeding with apply"
echo "has_changes=true" >> "$GITHUB_OUTPUT"
fi
- name: Terraform Apply
working-directory: terraform
if: steps.check-changes.outputs.has_changes == 'true'
env:
DOPPLER_TOKEN: ${{ secrets.DOPPLER_AT_SECRETS }}
run: doppler run --config prd -- timeout 1m terraform apply tfplan