-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Infrastructure github action
- Loading branch information
1 parent
97e7074
commit b00ea97
Showing
2 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
name: Apply Infrastructure | ||
on: | ||
workflow_dispatch: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
jobs: | ||
plan: | ||
name: Terraform Plan | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v3 | ||
with: | ||
terraform_wrapper: false | ||
terraform_version: "5.52.0" | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: eu-central-1 | ||
|
||
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | ||
- name: Terraform Init | ||
working-directory: ./terraform/ | ||
run: | | ||
terraform init | ||
# Generates an execution plan for Terraform | ||
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes. | ||
- name: Terraform Plan | ||
id: tf-plan | ||
working-directory: ./terraform/ | ||
run: | | ||
export exitcode=0 | ||
terraform plan -var-file $(pwd)/tf_$ENVIRONMENT.tfvars \ | ||
-detailed-exitcode -no-color -out tfplan || export exitcode=$? | ||
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT | ||
if [ $exitcode -eq 1 ]; then | ||
echo Terraform Plan Failed! | ||
exit 1 | ||
else | ||
exit 0 | ||
fi | ||
# Upload plan to artifacts | ||
- name: Publish Terraform Plan | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: tfplan | ||
path: ./terraform/tfplan | ||
|
||
apply: | ||
needs: [plan] | ||
if: ${{ needs.plan.outputs.exitcode == 2 }} | ||
name: Terraform Apply | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v3 | ||
with: | ||
terraform_wrapper: false | ||
terraform_version: "5.52.0" | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: eu-central-1 | ||
|
||
- name: Download Terraform Plan | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: tfplan | ||
path: ./terraform/ | ||
|
||
# Terraform Apply | ||
- name: Terraform Apply | ||
id: tf-apply-string | ||
working-directory: ./terraform/ | ||
run: | | ||
terraform apply -auto-approve tfplan -no-color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters