Skip to content

Commit

Permalink
added http backend and ci/cd
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 9, 2024
1 parent e7751e2 commit fe580a0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/terraform-terragrunt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
jobs:
terraform:
runs-on: ubuntu-latest
env:
GITLAB_USERNAME: ${{ secrets.GITLAB_USERNAME }}
GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -30,19 +33,17 @@ jobs:
BRANCH_CONFIG=$(jq -r --arg branch "$BRANCH_NAME" '.branches[$branch] // .branches.default' $CONFIG_FILE)
TF_WORKSPACE=$(echo $BRANCH_CONFIG | jq -r '.TF_WORKSPACE')
TG_WORKDIR=$(echo $BRANCH_CONFIG | jq -r '.TG_WORKDIR')
echo "TF_WORKSPACE=$TF_WORKSPACE" >> $GITHUB_ENV
echo "TG_WORKDIR=$TG_WORKDIR" >> $GITHUB_ENV
echo "TF_VERSION=$TF_VERSION" >> $GITHUB_ENV
echo "TG_VERSION=$TG_VERSION" >> $GITHUB_ENV
echo "THE_TF_WORKSPACE=$TF_WORKSPACE" >> $GITHUB_ENV
- name: Set up Terraform CLI
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
terraform_wrapper: true
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}


- name: Set up Terragrunt CLI
run: |
wget -qO /tmp/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/v${{ env.TG_VERSION }}/terragrunt_linux_amd64
Expand Down Expand Up @@ -70,4 +71,4 @@ jobs:
- name: Display success message
if: github.event_name == 'workflow_dispatch'
run: |
echo "🚀 Terraform Terragrunt Action triggered manually!"
echo "🚀 Terraform Terragrunt Action triggered manually!"
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"dev": {
"TF_WORKSPACE": "dev-project",
"TG_WORKDIR": "environment/dev"
},
"gitlab-state": {
"TF_WORKSPACE": "prod-project",
"TG_WORKDIR": "environment/stage"
}
}
}
24 changes: 10 additions & 14 deletions terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
# Define the Terraform backend configuration to use Terraform Cloud
locals {
organization = "kintaro"
workspace_tags = "kintaro_devops"
project_id = "61383408"
}


generate "backend" {
path = "auto_generated_backend.tf"
if_exists = "overwrite"
contents = <<EOF
terraform {
cloud {
organization = "${local.organization}"
workspaces {
tags = ["${local.workspace_tags}"]
}
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
backend "http" {
address = "https://gitlab.com/api/v4/projects/${local.project_id}/terraform/state/${get_env("THE_TF_WORKSPACE")}"
lock_address = "https://gitlab.com/api/v4/projects/${local.project_id}/terraform/state/${get_env("THE_TF_WORKSPACE")}/lock"
unlock_address = "https://gitlab.com/api/v4/projects/${local.project_id}/terraform/state/${get_env("THE_TF_WORKSPACE")}/lock"
username = "${get_env("GITLAB_USERNAME")}"
password = "${get_env("GITLAB_ACCESS_TOKEN")}"
lock_method = "POST"
unlock_method = "DELETE"
retry_wait_min = 5
}
}
}
EOF
}

0 comments on commit fe580a0

Please sign in to comment.