Skip to content

Commit

Permalink
Switch to OpenTofu as far as currently supported
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Robinson <[email protected]>
  • Loading branch information
autarchprinceps committed Jun 25, 2024
1 parent c48dfaa commit b480c40
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 56 deletions.
36 changes: 17 additions & 19 deletions .github/workflows/githubaction-comment-apply.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'TerraformApply'
name: 'TofuApply'

on:
issue_comment:
Expand Down Expand Up @@ -75,10 +75,10 @@ jobs:
echo "stack_changed set to: ${{ steps.filter.outputs.stack_changed }}"
if: always()

terraform:
tofu:
needs: check-changes
if: needs.check-changes.outputs.stack_changed == 'true'
name: 'Apply Terraform'
name: 'Apply Tofu'
runs-on: arvato
outputs:
output1: ${{ steps.job.outputs.job_id }}
Expand Down Expand Up @@ -129,17 +129,15 @@ jobs:
- run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV
shell: bash

# Install the latest version of Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest
# Install the latest version of tofu CLI
- name: Setup Tofu
uses: opentofu/setup-opentofu@v1

# Initialize Terraform
- name: Terraform Init
# Initialize Tofu
- name: Tofu Init
run: |
cd ${{ inputs.stack }}
terraform init -upgrade
tofu init -upgrade
# Terraform Validation Steps
- name: terraform validate ${{ inputs.stack }}
Expand Down Expand Up @@ -171,13 +169,13 @@ jobs:
cd ${{ inputs.stack }}
aws s3 cp s3://${{ inputs.s3bucketName }}/plans/${{ github.repository }}/${{ inputs.stack }}/${{ inputs.github_event_number }}/tfplan ./tfplan
# Build or change infrastructure according to Terraform configuration files
- name: Terraform Apply
# Build or change infrastructure according to Tofu configuration files
- name: Tofu Apply
id: apply
continue-on-error: true
run: |
cd ${{ inputs.stack }}
terraform apply -input=false -no-color tfplan
tofu apply -input=false -no-color tfplan
# Upload the plan to S3
- name: Upload Plan to S3
run: |
Expand All @@ -193,7 +191,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd ${{ inputs.stack }}
(printf "**Terraform Apply ${{ inputs.stack }} environment**\n\n\`\`\`" && echo -n '${{ steps.apply.outputs.stdout }}' && printf "\`\`\`\n\n") > comment.txt
(printf "**Tofu Apply ${{ inputs.stack }} environment**\n\n\`\`\`" && echo -n '${{ steps.apply.outputs.stdout }}' && printf "\`\`\`\n\n") > comment.txt
jq -R -s '.' < comment.txt > comment2.txt
truncate -s -1 comment2.txt
(echo -n '{ "body": ' && cat comment2.txt && echo -n ' }') > comment3.txt
Expand Down Expand Up @@ -224,7 +222,7 @@ jobs:
# $URL \
# -H "Content-Type: application/json" \
# -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
# -d '{"commit_title":"terraform update applied"}'
# -d '{"commit_title":"Tofu update applied"}'

# remove the organization plan from S3 whether successful or not
- name: Delete Plan from S3
Expand Down Expand Up @@ -257,7 +255,7 @@ jobs:
logging:
name: 'Save logs'
needs: terraform
needs: tofu
runs-on: arvato
if: always() # This job will always run
permissions:
Expand All @@ -279,9 +277,9 @@ jobs:
- name: Retrieve log file and upload to s3
run: |
TIMESTAMP=$(date +'%Y-%m-%d-%H:%M:%S')
LOG_FILENAME="TerraformApply_${{ inputs.github_event_number }}_PR_$TIMESTAMP.txt"
LOG_FILENAME="TofuApply_${{ inputs.github_event_number }}_PR_$TIMESTAMP.txt"
# Get log file
gh api repos/{owner}/{repo}/actions/jobs/${{ needs.terraform.outputs.output1 }}/logs > $LOG_FILENAME
gh api repos/{owner}/{repo}/actions/jobs/${{ needs.tofu.outputs.output1 }}/logs > $LOG_FILENAME
# Upload it to s3
aws s3 cp $LOG_FILENAME s3://${{ inputs.s3bucketName }}/logs/Apply/
env:
Expand Down
42 changes: 20 additions & 22 deletions .github/workflows/githubaction-comment-plan.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'TerraformPlan'
name: 'TofuPlan'

on:
issue_comment:
Expand Down Expand Up @@ -26,8 +26,8 @@ on:
default: "."

jobs:
terraform:
name: 'Plan Terraform'
tofu:
name: 'Plan Tofu'
runs-on: arvato
outputs:
output1: ${{ steps.job.outputs.job_id }}
Expand Down Expand Up @@ -81,17 +81,15 @@ jobs:
- run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV
shell: bash

# Install the latest version of Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest
# Install the latest version of tofu CLI
- name: Setup Tofu
uses: opentofu/setup-opentofu@v1

# Initialize Terraform
- name: Terraform Init ${{ inputs.stack }}
# Initialize Tofu
- name: Tofu Init ${{ inputs.stack }}
run: |
cd ${{ inputs.stack }}
terraform init -upgrade
tofu init -upgrade
# Terraform Validation Steps
- name: terraform validate ${{ inputs.stack }}
Expand All @@ -117,27 +115,27 @@ jobs:
GITHUB_APP_ID: ${{ secrets.TERRAFORM_GITHUB_ACTION_APP_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.TERRAFORM_GITHUB_ACTION_PRIVATE_KEY }}

# Generate terraform plan
- name: Terraform Plan ${{ inputs.stack }}
# Generate tofu plan
- name: Tofu Plan ${{ inputs.stack }}
id: tfplan
continue-on-error: true
run: |
cd ${{ inputs.stack }}
# Bash script to build terraform plan command dynamically
COMMAND="terraform plan -input=false -no-color -out=tfplan"
# Bash script to build tofu plan command dynamically
COMMAND="tofu plan -input=false -no-color -out=tfplan"
SECRETS_JSON='${{ toJson(secrets) }}'
SECRETS=$(echo "$SECRETS_JSON" | jq -r 'keys[]')
# Loop over each secret in the GitHub Actions environment
for key in $SECRETS; do
value=$(echo "$SECRETS_JSON" | jq -r ".[\"$key\"]")
# Check if the secret name has the prefix "TF_VAR_"
if [[ $key == TF_VAR_* ]]; then
# Add the secret as an input variable to the terraform command
# Add the secret as an input variable to the tofu command
COMMAND="$COMMAND -var '${key#TF_VAR_}=$value'"
fi
done
# Execute the dynamically generated terraform command
eval $COMMAND && terraform show -no-color tfplan | sed 's/\x27/ /g' | sed -E 's/^([[:space:]]+)([-+])/\2\1/g' > plan.txt
# Execute the dynamically generated tofu command
eval $COMMAND && tofu show -no-color tfplan | sed 's/\x27/ /g' | sed -E 's/^([[:space:]]+)([-+])/\2\1/g' > plan.txt
# Upload the plan to S3
- name: Upload ${{ inputs.stack }} Plan to S3
Expand All @@ -154,7 +152,7 @@ jobs:
run: |
set -x
cd ${{ inputs.stack }}
(printf "**Terraform Plan ${{ inputs.stack }} Environment**\n\n\`\`\`diff" && sed '/^::/d' plan.txt) > comment.txt
(printf "**Tofu Plan ${{ inputs.stack }} Environment**\n\n\`\`\`diff" && sed '/^::/d' plan.txt) > comment.txt
jq -R -s '.' < comment.txt > comment2.txt
truncate -s -1 comment2.txt
(echo -n '{ "body": ' && cat comment2.txt && echo -n ' }') > comment3.txt
Expand All @@ -173,7 +171,7 @@ jobs:
logging:
name: 'Save logs'
needs: terraform
needs: tofu
runs-on: arvato
if: always() # This job will always run
permissions:
Expand All @@ -195,9 +193,9 @@ jobs:
- name: Retrieve log file and upload to s3
run: |
TIMESTAMP=$(date +'%Y-%m-%d-%H:%M:%S')
LOG_FILENAME="TerraformPlan_${{ inputs.github_event_number }}_PR_$TIMESTAMP.txt"
LOG_FILENAME="TofuPlan_${{ inputs.github_event_number }}_PR_$TIMESTAMP.txt"
# Get log file
gh api repos/{owner}/{repo}/actions/jobs/${{ needs.terraform.outputs.output1 }}/logs > $LOG_FILENAME
gh api repos/{owner}/{repo}/actions/jobs/${{ needs.tofu.outputs.output1 }}/logs > $LOG_FILENAME
# Upload it to s3
aws s3 cp $LOG_FILENAME s3://${{ inputs.s3bucketName }}/logs/Plan/
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/githubaction-mergeblock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
workflow_call:

jobs:
terraform:
tofu:
name: 'Block Merge'
runs-on: arvato
permissions:
Expand Down
24 changes: 11 additions & 13 deletions .github/workflows/githubaction-scheduled-plan.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'TerraformPlanScheduled'
name: 'TofuPlanScheduled'

on:
workflow_dispatch:
Expand All @@ -16,8 +16,8 @@ on:
default: "."

jobs:
terraform:
name: 'Plan Terraform'
tofu:
name: 'Plan Tofu'
runs-on: arvato
permissions:
id-token: write
Expand Down Expand Up @@ -48,11 +48,9 @@ jobs:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v4
# Install the latest version of Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest
# Install the latest version of Tofu CLI
- name: Setup Tofu
uses: opentofu/setup-opentofu@v1
# Initialize Terraform
- name: Terraform Init
run: |
Expand Down Expand Up @@ -81,25 +79,25 @@ jobs:
GITHUB_APP_ID: ${{ secrets.TERRAFORM_GITHUB_ACTION_APP_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.TERRAFORM_GITHUB_ACTION_PRIVATE_KEY }}
# Generate plan
- name: Terraform Plan Landingzone
- name: Tofu Plan Landingzone
id: tfplan
continue-on-error: true
run: |
cd ${{ inputs.stack }}
# Bash script to build terraform plan command dynamically
COMMAND="terraform plan -input=false -no-color -out=tfplanlz --detailed-exitcode"
# Bash script to build tofu plan command dynamically
COMMAND="tofu plan -input=false -no-color -out=tfplanlz --detailed-exitcode"
SECRETS_JSON='${{ toJson(secrets) }}'
SECRETS=$(echo "$SECRETS_JSON" | jq -r 'keys[]')
# Loop over each secret in the GitHub Actions environment
for key in $SECRETS; do
value=$(echo "$SECRETS_JSON" | jq -r ".[\"$key\"]")
# Check if the secret name has the prefix "TF_VAR_"
if [[ $key == TF_VAR_* ]]; then
# Add the secret as an input variable to the terraform command
# Add the secret as an input variable to the tofu command
COMMAND="$COMMAND -var '${key#TF_VAR_}=$value'"
fi
done
# Execute the dynamically generated terraform command
# Execute the dynamically generated tofu command
eval $COMMAND
# CONCLUDE
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# github-actions-aws-terraform
Repository for Reusable Terraform GitHub Actions used to deploy resources in AWS.
Repository for Reusable OpenTofu GitHub Actions used to deploy resources in AWS.

0 comments on commit b480c40

Please sign in to comment.