Skip to content

Terraform

Terraform #72

Workflow file for this run

name: "Terraform"
on:
# Trigger a specific workflow run on demand without need for a code push/pull request
workflow_dispatch:
inputs:
repoName:
description: "Name of the repository to deploy"
required: true
githubRepo:
description: "Link of public github repo to deploy"
required: true
branchName:
description: "Name of the branch either main or master"
required: true
applicationName:
description: "Name of your application without spaces or dashes"
required: true
jobs:
apply_cluster:
permissions:
contents: "read"
id-token: "write"
runs-on: ubuntu-latest
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v3
- name: auth google cloud
uses: "google-github-actions/auth@v2"
with:
credentials_json: "${{ secrets.GCP_SA_KEY }}"
# Checkout the student's repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v3
with:
repository: ${{ github.event.inputs.githubRepo }}
ref: ${{ github.event.inputs.branchName }}
path: "./${{ github.event.inputs.repoName }}"
# - name: Login to Artifact Registry
# uses: docker/login-action@v1
# with:
# registry: europe-west3-docker.pkg.dev
# username: oauth2accesstoken
# password: ${{ steps.auth.outputs.access_token }}
- name: Copy docker file into the repo
run: cp dockerfile ./${{ github.event.inputs.repoName }}
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: deploying-with-terraform
- name: Setup Authentication to Docker repository
run: gcloud auth configure-docker europe-west3-docker.pkg.dev
- name: Open the application folder
run: cd ./${{ github.event.inputs.repoName }}
- name: Build Docker image
run: |
docker build -t europe-west3-docker.pkg.dev/deploying-with-terraform/express/api-skaffold:v1 ./${{ github.event.inputs.repoName }}
- name: Push Docker image to Google Container Registry
run: |
docker push europe-west3-docker.pkg.dev/deploying-with-terraform/express/api-skaffold:v1
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init
# formats the file
- name: Terraform Format
run: terraform fmt
# Generates an execution plan for Terraform
- name: Terraform Plan
run: terraform plan -var 'gcp_credentials=${{ secrets.GCP_SA_KEY }}' -var 'application_name=${{github.event.input.applicationName}}'
# Apply terraform
- name: Terraform Apply
run: terraform apply -var 'gcp_credentials=${{ secrets.GCP_SA_KEY }}' -var 'application_name=${{github.event.input.applicationName}}' -auto-approve
- name: Terraform output
run: terraform output