diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3fa8d83..54f7c40 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -61,8 +61,7 @@ jobs: tofu_version: "1.12.6" tofu_wrapper: false - # Verify OpenTofu and TEST stack configurations are available. - # Actual deployment steps will be added after credentials are configured. + # Verify OpenTofu, credentials, and TEST stack configurations. - name: Preflight checks shell: bash run: | @@ -219,8 +218,8 @@ jobs: echo "TEST remote state access verified." - # Preview infrastructure changes without applying them. - - name: Plan TEST infrastructure + # Plan and automatically apply safe TEST infrastructure changes. + - name: Deploy TEST infrastructure shell: bash env: TF_IN_AUTOMATION: "true" @@ -228,45 +227,108 @@ jobs: TF_VAR_ssh_key_name: ${{ vars.SSH_KEY_NAME }} run: | set -euo pipefail - + umask 077 + + # Verify required variables and configuration files. : "${TF_VAR_admin_cidr:?Missing ADMIN_CIDR secret}" : "${TF_VAR_ssh_key_name:?Missing SSH_KEY_NAME variable}" test -f environments/test/network/deployment.tfvars test -f environments/test/frontend/deployment.tfvars + # Deploy network before frontend. for STACK in network frontend; do - echo "::group::TEST $STACK plan" + STACK_DIR="environments/test/$STACK" + + PLAN_FILE="$RUNNER_TEMP/test-$STACK.tfplan" + PLAN_JSON="$RUNNER_TEMP/test-$STACK.plan.json" + PLAN_LOG="$RUNNER_TEMP/test-$STACK.plan.log" + APPLY_LOG="$RUNNER_TEMP/test-$STACK.apply.log" + + echo "::group::Deploy TEST $STACK" + # Generate a saved plan. set +e - tofu -chdir="environments/test/$STACK" plan \ + tofu -chdir="$STACK_DIR" plan \ -var-file="deployment.tfvars" \ + -out="$PLAN_FILE" \ -input=false \ -no-color \ -lock-timeout=5m \ - -detailed-exitcode + -detailed-exitcode > "$PLAN_LOG" 2>&1 EXIT_CODE=$? set -e - case "$EXIT_CODE" in - 0) - echo "TEST $STACK: No changes required." - ;; - 2) - echo "::warning::TEST $STACK: Infrastructure changes detected. Review the plan." - ;; - *) - echo "::error::TEST $STACK: Planning failed." - exit "$EXIT_CODE" - ;; - esac + # Handle planning errors. + if [ "$EXIT_CODE" -ne 0 ] && [ "$EXIT_CODE" -ne 2 ]; then + echo "::error::TEST $STACK planning failed." + exit 1 + fi + + # Skip apply when there are no changes. + if [ "$EXIT_CODE" -eq 0 ]; then + echo "TEST $STACK: No changes required." + echo "::endgroup::" + continue + fi + + # Convert the saved plan into JSON for inspection. + test -s "$PLAN_FILE" + + tofu -chdir="$STACK_DIR" show -json \ + "$PLAN_FILE" > "$PLAN_JSON" + + # Only allow known non-destructive resource actions. + if ! jq -e ' + (.format_version | type == "string") and + (.resource_changes | type == "array") and + all(.resource_changes[]?; + (.change.actions == ["no-op"] or + .change.actions == ["read"] or + .change.actions == ["create"] or + .change.actions == ["update"]) + ) + ' "$PLAN_JSON" > /dev/null; then + echo "::error::TEST $STACK contains invalid or destructive changes." + echo "Automatic apply has been blocked." + exit 1 + fi + + # Display resource actions without exposing configuration values. + echo "Planned TEST $STACK changes:" + + jq -r ' + .resource_changes[]? + | select(.change.actions != ["no-op"]) + | "\(.address): \(.change.actions | join(", "))" + ' "$PLAN_JSON" + + # Apply the exact saved plan. + echo "Applying TEST $STACK..." + + if ! tofu -chdir="$STACK_DIR" apply \ + -input=false \ + -no-color \ + -lock-timeout=5m \ + "$PLAN_FILE" > "$APPLY_LOG" 2>&1; then + echo "::error::TEST $STACK apply failed." + exit 1 + fi + echo "TEST $STACK deployed successfully." echo "::endgroup::" done - # Remove temporary credentials even when verification fails. - - name: Clean up Kubernetes credentials + echo "TEST infrastructure deployment completed." + + # Remove temporary credentials, saved plans and logs. + - name: Clean up temporary files if: always() shell: bash run: | - rm -f "$RUNNER_TEMP/assignment2-kubeconfig" \ No newline at end of file + rm -f "$RUNNER_TEMP/assignment2-kubeconfig" + + rm -f "$RUNNER_TEMP"/test-*.tfplan + rm -f "$RUNNER_TEMP"/test-*.plan.json + rm -f "$RUNNER_TEMP"/test-*.plan.log + rm -f "$RUNNER_TEMP"/test-*.apply.log \ No newline at end of file