From 47dd23e76c48af22156b7474c06a12ec23d566cd Mon Sep 17 00:00:00 2001 From: sumanmanna134 Date: Fri, 26 Jul 2024 15:11:32 +0530 Subject: [PATCH] add github actions for terrform and destroy infra --- .github/workflows/destroyinfra.yml | 19 ++++++++++++++ .github/workflows/terraform.yml | 40 ++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/destroyinfra.yml diff --git a/.github/workflows/destroyinfra.yml b/.github/workflows/destroyinfra.yml new file mode 100644 index 0000000..c3ca815 --- /dev/null +++ b/.github/workflows/destroyinfra.yml @@ -0,0 +1,19 @@ +name: Destroy Infrastructure + +on: + workflow_dispatch: + +jobs: + destroy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Terraform + uses: hashicorp/setup-terraform@v2 + with: + terraform_version: '1.8.3' # Replace with desired Terraform version + + - name: Destroy Infrastructure + run: | + terraform init + terraform destroy --var-file="./tfvars/terraform.tfvars" -auto-approve diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 01502b1..3ae2369 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -1,14 +1,14 @@ # This is a basic workflow to help you get started with Actions -name: CI +name: Terraform # Controls when the workflow will run on: # Triggers the workflow on push or pull request events but only for the "main" branch push: - branches: [ "main" ] + branches: ['main'] pull_request: - branches: [ "main" ] + branches: ['main'] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -16,9 +16,17 @@ on: # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" - build: + terraform: # The type of runner that the job will run on runs-on: ubuntu-latest + env: + ARM_CLIENT_ID: ${{secrets.AZURE_AD_CLIENT_ID}} + ARM_CLIENT_SECRET: ${{secrets.AZURE_AD_CLIENT_SECRET}} + ARM_SUBSCRIPTION_ID: ${{secrets.AZURE_SUBSCRIPTION_ID}} + ARM_TENANT_ID: ${{secrets.AZURE_AD_TENANT_ID}} + defaults: + run: + shell: bash # Steps represent a sequence of tasks that will be executed as part of the job steps: @@ -26,11 +34,21 @@ jobs: - uses: actions/checkout@v4 # Runs a single command using the runners shell - - name: Run a one-line script - run: echo Hello, world! + - name: Setup Terraform + uses: hashicorp/setup-terraform@v2 + with: + terraform_version: '1.8.3' + - name: Terraform fmt + id: fmt + run: terraform fmt -check - # Runs a set of commands using the runners shell - - name: Run a multi-line script - run: | - echo Add other actions to build, - echo test, and deploy your project. + - name: Terraform init + id: init + run: terraform init + - name: Terraform plan + id: plan + run: terraform plan --var-file="./tfvars/terraform.tfvars" -out azinfra.tfplan -no-color + - name: Terraform apply + id: apply + if: github.ref == 'refs/heads/"main"' && github.event_name == 'push' + run: terraform apply azinfra.tfplan -auto-approve