Skip to content

Commit

Permalink
Add CI/CD workflows for terraform automation
Browse files Browse the repository at this point in the history
- add transcrypt script
- workflow changes to terraform
  • Loading branch information
DaMandal0rian committed Oct 3, 2023
1 parent 2002836 commit 55c06e4
Show file tree
Hide file tree
Showing 8 changed files with 1,917 additions and 23 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/devnet_main_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Main CD for Devnet Deployment

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
paths:
- "aws/devnet/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
deploy:
uses: ./.github/workflows/terraform_template_deploy.yml
with:
project: aws
resource: devnet
tf_workspace_name: devnet-aws
tf_version: 1.5.7
tf_organization: subspace
secrets:
TRANSCRYPT: ${{ secrets.TRANSCRYPT }}
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
30 changes: 30 additions & 0 deletions .github/workflows/ephemeral_devnet_aws_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Main CD for Ephememeral Devnet Deployment

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
branches:
- "!main"
paths:
- "testing-framework/ec2/network/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
deploy:
uses: ./.github/workflows/terraform_template_ephemeral_deploy.yml
with:
project: testing-framework
instance: ec2
resource: network
tf_workspace_name: ephemeral-devnet
tf_version: 1.5.7
tf_organization: subspace
secrets:
TRANSCRYPT: ${{ secrets.TRANSCRYPT }}
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
30 changes: 30 additions & 0 deletions .github/workflows/ephemeral_devnet_hetzner_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Main CD for Ephememeral Devnet Deployment

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
branches:
- "!main"
paths:
- "testing-framework/hetzner/network/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
deploy:
uses: ./.github/workflows/terraform_template_ephemeral_deploy.yml
with:
project: testing-framework
instance: hetzner
resource: network
tf_workspace_name: ephemeral-devnet-hetzner
tf_version: 1.5.7
tf_organization: subspace
secrets:
TRANSCRYPT: ${{ secrets.TRANSCRYPT }}
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/gemini_3f_main_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Main CD for Gemini Deployment

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
branches:
- "!main"
paths:
- "aws/gemini-3f/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
deploy:
uses: ./.github/workflows/terraform_template_deploy.yml
with:
project: aws
resource: gemini-3f
tf_workspace_name: gemini-3f
tf_version: 1.5.7
tf_organization: subspace
secrets:
TRANSCRYPT: ${{ secrets.TRANSCRYPT }}
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
55 changes: 32 additions & 23 deletions .github/workflows/terraform_gh_runner.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name: Terraform Workflow
name: Terraform GH Runner Deployment

on:
push:
workflow_dispatch:
pull_request:
branches:
- main
paths:
- './github-runners/terraform/base/**'
workflow_dispatch:
- "./github-runners/terraform/base/**"

jobs:
terraform_gh_runner:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: Checkout repository
Expand All @@ -19,40 +21,47 @@ jobs:
- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: "1.4.2"
terraform_version: "1.5.7"
cli_config_credentials_token: ${{ secrets.TF_CLOUD_TOKEN }}

- name: Install dependencies
run: |
# Install any dependencies required by your Terraform code
- name: Run Bash Script
env:
GH_TOKEN: ${{ github.token }}
run: |
# GitHub repository and access token
repo="subspace/infra"
token=${{ secrets.PAT_TOKEN }}
# GitHub repository and access token
repo="subspace/infra"
token=${{ secrets.PAT_TOKEN }}
# API endpoint
url="https://api.github.com/repos/$repo/actions/runners/registration-token"
# API endpoint
url="https://api.github.com/repos/$repo/actions/runners/registration-token"
# Send POST request to get the registration token
response=$(curl -X POST -H "Authorization: token $token" -s "$url")
# Send POST request to get the registration token
response=$(curl -X POST -H "Authorization: token $token" -s "$url")
# Extract the token value from the response
runner_token=$(echo "$response" | jq -r '.token')
# Extract the token value from the response
runner_token=$(echo "$response" | jq -r '.token')
# Export the token as an environment variable
echo "export RUNNER_TOKEN=$runner_token" >> $GITHUB_ENV
# Export the token as an environment variable
echo "export RUNNER_TOKEN=$runner_token" >> $GITHUB_ENV
# Set the runner token as an environment variable
export RUNNER_TOKEN="$runner_token"
# Set the runner token as an environment variable
export RUNNER_TOKEN="$runner_token"
# Store the token as a secret in GitHub Actions
gh secret set RUNNER_TOKEN -r "$repo" -b "$runner_token"
# Store the token as a secret in GitHub Actions
gh secret set RUNNER_TOKEN -r "$repo" -b "$runner_token"
- name: Fetch and write terraform.tfvars
run: |
echo ${{ secrets.TF_VARS_FILE }} > terraform.tfvars
chmod 600 terraform.tfvars
- name: Run Terraform
working-directory: ./github-runners/terraform/base
run: |
terraform init-backend-config="organization=${{ secrets.ORGANIZATION_NAME }}" -backend-config="workspaces=${{ secrets.WORKSPACE_NAME }}"
terraform plan -var-file=${{ secrets.VAR_FILE }}
terraform apply -auto-approve -var "gh_token=${{ secrets.RUNNER_TOKEN }}"
terraform init-backend-config="organization=subspace" -backend-config="workspaces=${{ secrets.WORKSPACE_NAME }}"
terraform plan -var-file=terraform.tfvars
terraform apply -auto-approve -var "gh_token=${{ env.RUNNER_TOKEN }}"
106 changes: 106 additions & 0 deletions .github/workflows/terraform_template_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Template Deploy

on:
workflow_call:
inputs:
project:
required: true
type: string
resource:
required: true
type: string
tf_workspace_name:
description: "Name of the workspace in terraform cloud"
required: false
type: string
tf_version:
description: "Version of the terraform"
required: true
type: string
tf_organization:
description: "Name of the TF organization"
default: "subspace"
type: string
required: true
run_apply:
description: "The code needs to be deployed or not"
type: string
default: "no"
run_destroy:
description: "The resources need to be destroyed or not"
type: string
default: "no"
secrets:
TRANSCRYPT:
required: true
TF_API_TOKEN:
required: false
env:
TF_CLOUD_ORGANIZATION: "${{ inputs.tf_organization }}"
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
TF_VERSION: "${{ inputs.tf_version }}"

jobs:
template-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v3

- name: Decrypt the secrets
run: |
chmod +x ./scripts/transcrypt
scripts/transcrypt -c aes-256-cbc -p ${{ secrets.TRANSCRYPT }} -y
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: false
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Setup Remote Config Backend
run: |
cat > config.remote.tfbackend <<EOT
workspaces { name = "${{ inputs.tf_workspace_name }}"}
hostname = "app.terraform.io"
organization = "${{ inputs.tf_organization }}"
EOT
- name: Terraform fmt
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: terraform fmt -check
continue-on-error: true

- name: Terraform Init for ${{ inputs.project }}/${{ inputs.resource }}
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: |
cat config.remote.tfbackend
terraform init -backend-config=config.remote.tfbackend
- name: Terraform Validate
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: terraform validate

- name: Fetch and write terraform.tfvars
run: |
echo ${{ secrets.TF_VARS_FILE }} > terraform.tfvars
chmod 600 terraform.tfvars
- name: Terraform Plan for ${{ inputs.project }}/${{ inputs.resource }}
if: ${{ (inputs.run_destroy == 'no') }}
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: |
terraform plan -var-file=terraform.tfvars
- name: Terraform Apply for ${{ inputs.project }}/${{ inputs.resource }}
if: ${{ (inputs.run_apply == 'yes') && (inputs.run_destroy == 'no') }}
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: |
terraform apply -auto-approve -var-file=terraform.tfvars
- name: Terraform Destroy for ${{ inputs.project }}/${{ inputs.resource }}
if: ${{ (inputs.run_destroy == 'yes') }}
working-directory: ${{ inputs.project }}/${{ inputs.resource }}
run: |
terraform plan -destroy -var-file=terraform.tfvars
terraform destroy -auto-approve -var-file=terraform.tfvars
Loading

0 comments on commit 55c06e4

Please sign in to comment.