Skip to content

Terraform Apply

Terraform Apply #9

name: Terraform Apply
# run-name: ${{ github.actor }} triggered the pipeline
on:
workflow_dispatch:
inputs:
operation:
description: 'Choose the Terraform operation'
required: true
default: 'apply'
type: choice
options:
- apply
- destroy
push:
branches:
- 'infra_main'
# path:
# - '../terraform/**'
env:
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
TF_VAR_aws_region: ${{ vars.TF_AWS_REGION }}
TF_VAR_ami_id: ${{ vars.TF_AMI_ID }}
TF_VAR_instance_type: ${{ vars.TF_INSTANCE_TYPE }}
TF_VAR_key_pair_name: ${{ vars.TF_KEY_PAIR_NAME }}
TF_VAR_private_key: ${{ secrets.PRIVATE_KEY }}
TF_VAR_domain_name: ${{ vars.TF_DOMAIN_NAME }}
TF_VAR_frontend_domain: ${{ vars.TF_FRONTEND_DOMAIN }}
TF_VAR_db_domain: ${{ vars.TF_DB_DOMAIN }}
TF_VAR_traefik_domain: ${{ vars.TF_TRAEFIK_DOMAIN }}
TF_VAR_cert_email: ${{ secrets.TF_CERT_EMAIL }}
TF_VAR_private_key_path: ${{ vars.TF_KEY_PAIR_NAME }}.pem
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
jobs:
build-infra:
name: terraform-ci-cd
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Write Private Key to File
run: |
echo "${{ secrets.PRIVATE_KEY }}" > ${{ vars.TF_KEY_PAIR_NAME }}.pem
chmod 600 ${{ vars.TF_KEY_PAIR_NAME }}.pem
working-directory: ./terraform
- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
id: init
run: terraform init
working-directory: ./terraform
- name: Terraform Operation
id: terraform-operation
run: |
if [ "${{ github.event.inputs.operation }}" = "destroy" ]; then
terraform destroy --auto-approve
else
terraform apply --auto-approve
fi
working-directory: ./terraform
- name: Upload Ansible Inventory
uses: actions/upload-artifact@v3
with:
name: ansible_inventory
path: ./terraform/inventory.ini
- name: Save Public IP
run: |
echo "public_ip=$(terraform output -raw instance_public_ip)" > public_ip_env.txt
working-directory: ./terraform
- name: Upload Public_IP
uses: actions/upload-artifact@v3
with:
name: Public_IP
path: |
./public_ip_env.txt
- name: Trigger Ansible Monitoring Workflow
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.TOKEN }}
event-type: ansible-monitoring