Skip to content

Merge branch 'infra_features' into infra_main #57

Merge branch 'infra_features' into infra_main

Merge branch 'infra_features' into infra_main #57

name: Terraform Apply
on:
workflow_dispatch:
inputs:
operation:
description: 'Choose the Terraform operation'
required: true
default: 'apply'
type: choice
options:
- apply
- destroy
push:
branches:
- 'infra_main'
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_volume_size: ${{ vars.TF_VOLUME_SIZE }}
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
if: steps.terraform-operation.outcome == 'success' && github.event.inputs.operation != 'destroy'
uses: actions/upload-artifact@v4
with:
name: ansible_inventory
path: ./terraform/inventory.ini
- name: Save Public IP
if: steps.terraform-operation.outcome == 'success' && github.event.inputs.operation != 'destroy'
run: |
PUBLIC_IP=$(terraform output instance_public_ip | sed 's/"//g')
echo "$PUBLIC_IP" > public_ip_env.txt
cat public_ip_env.txt
working-directory: ./terraform
- name: Upload Public_IP
if: steps.terraform-operation.outcome == 'success' && github.event.inputs.operation != 'destroy'
uses: actions/upload-artifact@v4
with:
name: Public_IP
path: ./terraform/public_ip_env.txt
- name: Invoke workflow without inputs
if: steps.terraform-operation.outcome == 'success' && github.event.inputs.operation != 'destroy'
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Ansible Monitoring
token: "${{ secrets.TOKEN }}"