fix(ci): use custom terraform-ci container image
- Use gitea.arnodo.fr/damien/terraform-ci:latest (Terraform 1.5.7 + TFLint) - Remove setup-terraform and setup-tflint actions (already in image) - Replace actions/github-script with curl + Gitea API for PR comments - Use gitea.* context variables instead of github.* - Fix plan exit code handling (0=no changes, 2=changes, 1=error) Refs #3
This commit is contained in:
@@ -29,28 +29,18 @@ jobs:
|
|||||||
validate:
|
validate:
|
||||||
name: Validate
|
name: Validate
|
||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
container:
|
||||||
|
image: gitea.arnodo.fr/damien/terraform-ci:latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Terraform
|
|
||||||
uses: hashicorp/setup-terraform@v3
|
|
||||||
with:
|
|
||||||
terraform_version: "1.9"
|
|
||||||
|
|
||||||
- name: Setup TFLint
|
|
||||||
uses: terraform-linters/setup-tflint@v4
|
|
||||||
|
|
||||||
- name: Terraform Format Check
|
- name: Terraform Format Check
|
||||||
id: fmt
|
id: fmt
|
||||||
run: terraform fmt -check -recursive -diff
|
run: terraform fmt -check -recursive -diff
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
|
||||||
- name: TFLint Init
|
|
||||||
run: tflint --init
|
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
|
||||||
|
|
||||||
- name: TFLint
|
- name: TFLint
|
||||||
id: lint
|
id: lint
|
||||||
run: tflint --recursive --format compact
|
run: tflint --recursive --format compact
|
||||||
@@ -89,6 +79,8 @@ jobs:
|
|||||||
plan:
|
plan:
|
||||||
name: Plan
|
name: Plan
|
||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
container:
|
||||||
|
image: gitea.arnodo.fr/damien/terraform-ci:latest
|
||||||
needs: validate
|
needs: validate
|
||||||
outputs:
|
outputs:
|
||||||
plan_exitcode: ${{ steps.plan.outputs.exitcode }}
|
plan_exitcode: ${{ steps.plan.outputs.exitcode }}
|
||||||
@@ -96,11 +88,6 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Terraform
|
|
||||||
uses: hashicorp/setup-terraform@v3
|
|
||||||
with:
|
|
||||||
terraform_version: "1.9"
|
|
||||||
|
|
||||||
- name: Terraform Init
|
- name: Terraform Init
|
||||||
run: terraform init -input=false
|
run: terraform init -input=false
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
@@ -108,16 +95,24 @@ jobs:
|
|||||||
- name: Terraform Plan
|
- name: Terraform Plan
|
||||||
id: plan
|
id: plan
|
||||||
run: |
|
run: |
|
||||||
|
set +e
|
||||||
terraform plan -input=false -no-color -detailed-exitcode -out=tfplan 2>&1 | tee plan_output.txt
|
terraform plan -input=false -no-color -detailed-exitcode -out=tfplan 2>&1 | tee plan_output.txt
|
||||||
echo "exitcode=$?" >> $GITHUB_OUTPUT
|
EXITCODE=$?
|
||||||
|
echo "exitcode=${EXITCODE}" >> $GITHUB_OUTPUT
|
||||||
|
# Exit 0 for no changes, exit 2 for changes - both are OK
|
||||||
|
if [ $EXITCODE -eq 1 ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
continue-on-error: true
|
|
||||||
|
|
||||||
- name: Upload Plan
|
- name: Upload Plan
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: tfplan
|
name: tfplan
|
||||||
path: ${{ env.TF_WORKING_DIR }}/tfplan
|
path: |
|
||||||
|
${{ env.TF_WORKING_DIR }}/tfplan
|
||||||
|
${{ env.TF_WORKING_DIR }}/plan_output.txt
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
|
|
||||||
- name: Plan Summary
|
- name: Plan Summary
|
||||||
@@ -125,81 +120,74 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo "## Terraform Plan" >> $GITHUB_STEP_SUMMARY
|
echo "## Terraform Plan" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "**Exit code:** \`${{ steps.plan.outputs.exitcode }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- \`0\` = No changes" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- \`2\` = Changes detected" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo '```' >> $GITHUB_STEP_SUMMARY
|
echo '```' >> $GITHUB_STEP_SUMMARY
|
||||||
cat plan_output.txt >> $GITHUB_STEP_SUMMARY
|
cat plan_output.txt >> $GITHUB_STEP_SUMMARY
|
||||||
echo '```' >> $GITHUB_STEP_SUMMARY
|
echo '```' >> $GITHUB_STEP_SUMMARY
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
|
|
||||||
- name: Comment PR with Plan
|
- name: Comment PR with Plan
|
||||||
if: github.event_name == 'pull_request'
|
if: gitea.event_name == 'pull_request'
|
||||||
uses: actions/github-script@v7
|
env:
|
||||||
with:
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
script: |
|
run: |
|
||||||
const fs = require('fs');
|
PLAN_OUTPUT=$(cat plan_output.txt | head -c 60000)
|
||||||
const plan = fs.readFileSync('${{ env.TF_WORKING_DIR }}/plan_output.txt', 'utf8');
|
COMMENT_BODY=$(cat <<EOF
|
||||||
const maxLength = 60000;
|
## 🔍 Terraform Plan
|
||||||
const truncated = plan.length > maxLength
|
|
||||||
? plan.substring(0, maxLength) + '\n... (truncated)'
|
|
||||||
: plan;
|
|
||||||
|
|
||||||
const body = `## 🔍 Terraform Plan
|
**Exit code:** \`${{ steps.plan.outputs.exitcode }}\`
|
||||||
|
- \`0\` = No changes
|
||||||
|
- \`2\` = Changes detected
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Click to expand</summary>
|
<summary>Click to expand plan output</summary>
|
||||||
|
|
||||||
\`\`\`hcl
|
\`\`\`hcl
|
||||||
${truncated}
|
${PLAN_OUTPUT}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
**Exit code:** \`${{ steps.plan.outputs.exitcode }}\`
|
# Post comment via Gitea API
|
||||||
- \`0\` = No changes
|
curl -s -X POST \
|
||||||
- \`1\` = Error
|
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||||
- \`2\` = Changes detected
|
-H "Content-Type: application/json" \
|
||||||
`;
|
-d "$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')" \
|
||||||
|
"${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/${{ gitea.event.pull_request.number }}/comments"
|
||||||
github.rest.issues.createComment({
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
issue_number: context.issue.number,
|
|
||||||
owner: context.repo.owner,
|
|
||||||
repo: context.repo.repo,
|
|
||||||
body: body
|
|
||||||
});
|
|
||||||
|
|
||||||
- name: Check Plan Status
|
|
||||||
if: steps.plan.outputs.exitcode == '1'
|
|
||||||
run: exit 1
|
|
||||||
|
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
# Apply - Runs on push to dev OR after PR merge
|
# Apply - Runs on push to dev only (when changes detected)
|
||||||
# ===========================================================================
|
# ===========================================================================
|
||||||
apply:
|
apply:
|
||||||
name: Apply
|
name: Apply
|
||||||
runs-on: self-hosted
|
runs-on: self-hosted
|
||||||
|
container:
|
||||||
|
image: gitea.arnodo.fr/damien/terraform-ci:latest
|
||||||
needs: plan
|
needs: plan
|
||||||
if: |
|
if: |
|
||||||
(github.event_name == 'push' && github.ref == 'refs/heads/dev') &&
|
gitea.event_name == 'push' &&
|
||||||
|
gitea.ref == 'refs/heads/dev' &&
|
||||||
needs.plan.outputs.plan_exitcode == '2'
|
needs.plan.outputs.plan_exitcode == '2'
|
||||||
environment: production
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Setup Terraform
|
|
||||||
uses: hashicorp/setup-terraform@v3
|
|
||||||
with:
|
|
||||||
terraform_version: "1.9"
|
|
||||||
|
|
||||||
- name: Terraform Init
|
|
||||||
run: terraform init -input=false
|
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
|
||||||
|
|
||||||
- name: Download Plan
|
- name: Download Plan
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: tfplan
|
name: tfplan
|
||||||
path: ${{ env.TF_WORKING_DIR }}
|
path: ${{ env.TF_WORKING_DIR }}
|
||||||
|
|
||||||
|
- name: Terraform Init
|
||||||
|
run: terraform init -input=false
|
||||||
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
|
|
||||||
- name: Terraform Apply
|
- name: Terraform Apply
|
||||||
run: terraform apply -input=false -auto-approve tfplan
|
run: terraform apply -input=false -auto-approve tfplan
|
||||||
working-directory: ${{ env.TF_WORKING_DIR }}
|
working-directory: ${{ env.TF_WORKING_DIR }}
|
||||||
|
|||||||
Reference in New Issue
Block a user