174 lines
5.9 KiB
YAML
174 lines
5.9 KiB
YAML
name: Pull Request CD-Deploy to Staging
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
workflow_run:
|
|
workflows: [ "Pull Request CI-Backend", "Pull Request CI-Frontend" ]
|
|
types: [ completed ]
|
|
|
|
jobs:
|
|
terraform-plan-stg:
|
|
name: Plan changs to staging
|
|
if: ${{ github.event.workflow_run.conclusion == 'success' }}
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
no_changes: ${{ steps.check-changes.outputs.no_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 Kubeconfig
|
|
run: |
|
|
mkdir -p ~/.kube
|
|
echo "${{ secrets.KUBECONFIG_DATA }}" | base64 -d > ~/.kube/config
|
|
chmod 600 ~/.kube/config
|
|
|
|
- name: Validate cluster access
|
|
run: |
|
|
kubectl cluster-info
|
|
kubectl get namespace tasknote-stg
|
|
|
|
- name: Determine deployment values
|
|
id: deploy-vars
|
|
run: |
|
|
backend_image="ghcr.io/rmcampos/tasknote/api:candidate"
|
|
frontend_image="ghcr.io/rmcampos/tasknote/app:candidate"
|
|
|
|
echo "backend_image=$backend_image" >> "$GITHUB_OUTPUT"
|
|
echo "frontend_image=$frontend_image" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Terraform Fmt -check -diff
|
|
working-directory: terraform-stg
|
|
run: terraform fmt -check -diff
|
|
|
|
- name: Terraform Init
|
|
working-directory: terraform-stg
|
|
env:
|
|
AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
|
run: terraform init -input=false
|
|
|
|
- name: Terraform Validate
|
|
working-directory: terraform-stg
|
|
run: terraform validate
|
|
|
|
- name: Debug cluster connectivity
|
|
run: |
|
|
echo "--- Testing TCP connectivity ---"
|
|
timeout 10 bash -c "cat < /dev/null > /dev/tcp/191.252.159.227/6443" \
|
|
&& echo "TCP OK" || echo "TCP FAILED"
|
|
|
|
echo "--- Testing TLS handshake ---"
|
|
openssl s_client -connect 191.252.159.227:6443 -showcerts </dev/null 2>&1 | head -20
|
|
|
|
echo "--- kubectl raw ---"
|
|
kubectl cluster-info --request-timeout=10s
|
|
|
|
- name: Check kubeconfig cert expiry
|
|
run: |
|
|
echo "--- Client cert in kubeconfig ---"
|
|
kubectl config view --raw -o jsonpath='{.users[0].user.client-certificate-data}' \
|
|
| base64 -d | openssl x509 -noout -dates -subject
|
|
|
|
echo "--- CA cert in kubeconfig ---"
|
|
kubectl config view --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' \
|
|
| base64 -d | openssl x509 -noout -dates -subject
|
|
|
|
- name: Show Terraform provider versions
|
|
working-directory: terraform-stg
|
|
run: terraform version
|
|
|
|
- name: Check API server serving cert
|
|
run: |
|
|
echo | openssl s_client -connect 191.252.159.227:6443 2>/dev/null \
|
|
| openssl x509 -noout -fingerprint -sha256 -dates
|
|
|
|
- name: Terraform Plan
|
|
id: check-changes
|
|
working-directory: terraform-stg
|
|
env:
|
|
AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
|
KUBE_CONFIG_PATH: ~/.kube/config
|
|
run: |
|
|
echo "Start: $(date)"
|
|
timeout 3m terraform plan -input=false -out=tfplan \
|
|
-var="kubeconfig_path=$HOME/.kube/config" \
|
|
-var="db_user=${{ secrets.DB_USER }}" \
|
|
-var="db_password=${{ secrets.DB_PASSWORD }}" \
|
|
-var="db_name=${{ secrets.DB_NAME }}" \
|
|
-var="security_key=${{ secrets.JWT_SECURITY_KEY }}" \
|
|
-var="mailgun_apikey=${{ secrets.MAILGUN_API_KEY }}" \
|
|
-var="backend_image=${{ steps.deploy-vars.outputs.backend_image }}" \
|
|
-var="frontend_image=${{ steps.deploy-vars.outputs.frontend_image }}" \
|
|
-var="deploy_version=${{ github.run_id }}"
|
|
echo "End: $(date)"
|
|
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@v4
|
|
with:
|
|
name: tfplan
|
|
path: terraform-stg/tfplan
|
|
|
|
terraform-apply:
|
|
runs-on: ubuntu-latest
|
|
needs: terraform-plan-stg
|
|
if: needs.terraform-plan-stg.outputs.no_changes == 'false'
|
|
environment:
|
|
name: staging
|
|
url: https://tasknote-stg.darkroasted.vps-kinghost.net
|
|
permissions:
|
|
contents: read
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v6
|
|
|
|
- name: Setup Terraform
|
|
uses: hashicorp/setup-terraform@v3
|
|
|
|
- name: Download plan artifact
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: tfplan
|
|
path: terraform-stg
|
|
|
|
- name: Setup Kubeconfig
|
|
run: |
|
|
mkdir -p ${{ runner.temp }}/.kube
|
|
echo "${{ secrets.KUBECONFIG_DATA }}" | base64 -d > ~/.kube/config
|
|
chmod 600 ~/.kube/config
|
|
|
|
- name: Terraform Init
|
|
working-directory: terraform-stg
|
|
env:
|
|
AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
|
run: terraform init -input=false
|
|
|
|
- name: Terraform Apply
|
|
working-directory: terraform-stg
|
|
env:
|
|
AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
|
|
AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
|
|
KUBE_CONFIG_PATH: ~/.kube/config
|
|
run: timeout 1m terraform apply tfplan
|