Skip to content

Commit

Permalink
fix gitlab file
Browse files Browse the repository at this point in the history
  • Loading branch information
LamSut committed Oct 16, 2024
1 parent 2f1b6d2 commit e0b62de
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 93 deletions.
63 changes: 25 additions & 38 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,52 @@ on:
push:
branches: [ main ]

env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: '>= 1.5.0'

- name: Checkout code
uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Configure AWS credentials
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
AWS_DEFAULT_REGION: us-east-1
run: |
echo "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" >> $GITHUB_ENV
echo "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY" >> $GITHUB_ENV
- name: Configure GitLab access token (optional)
if: ${{ needs.validate || needs.plan || needs.apply || needs.destroy }}
env:
TF_VAR_gitlab_token: ${{ secrets.GITLAB_ACCESS_TOKEN }}
run: |
echo "TF_VAR_gitlab_token=$TF_VAR_gitlab_token" >> $GITHUB_ENV
- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest

- name: Cache Terraform state
- name: Cache Terraform modules (optional)
uses: actions/cache@v3
with:
path: .terraform
key: ${{ runner.os }}-terraform-${{ hashFiles('.terraform/backend.tf') }}
path: ~/.terraform/modules
key: ${{ runner.os }}-terraform-${{ hashFiles('.terraform') }}
restore-keys: |
${{ runner.os }}-terraform-
- name: Terraform Init
- name: Initialize Terraform
run: terraform init

- name: Validate Terraform configuration (optional)
if: ${{ needs.validate }}
- name: Validate Terraform configuration
run: terraform validate

- name: Create Terraform plan (optional)
if: ${{ needs.plan }}
run: terraform plan -out="planfile"
run: terraform plan -out=planfile
outputs: plan_file: planfile

- name: Upload Terraform plan (optional)
if: ${{ needs.plan }}
- name: Upload plan artifacts (optional)
uses: actions/upload-artifact@v3
with:
name: planfile
path: planfile
name: terraform-plan
if: steps.plan.outputs.plan_file
path: ${{ steps.plan.outputs.plan_file }}

- name: Apply Terraform changes (manual)
if: ${{ needs.apply }}
needs: [validate, plan]
run: terraform apply -input=false "planfile"
run: terraform apply planfile
needs: apply

- name: Destroy infrastructure (manual)
if: ${{ needs.destroy }}
run: terraform destroy --auto-approve
run: terraform destroy --auto-approve
needs: destroy
109 changes: 54 additions & 55 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
name: Terraform Workflow

on:
push:
branches: [ main ]

env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: us-east-1

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: latest

- name: Cache Terraform modules (optional)
uses: actions/cache@v3
with:
path: ~/.terraform/modules
key: ${{ runner.os }}-terraform-${{ hashFiles('.terraform') }}
restore-keys: |
${{ runner.os }}-terraform-
- name: Initialize Terraform
run: terraform init

- name: Validate Terraform configuration
run: terraform validate

- name: Create Terraform plan (optional)
run: terraform plan -out=planfile
outputs: plan_file: planfile

- name: Upload plan artifacts (optional)
uses: actions/upload-artifact@v3
with:
name: terraform-plan
if: steps.plan.outputs.plan_file
path: ${{ steps.plan.outputs.plan_file }}

- name: Apply Terraform changes (manual)
run: terraform apply planfile
needs: apply

- name: Destroy infrastructure (manual)
run: terraform destroy --auto-approve
needs: destroy
image:
name: registry.gitlab.com/gitlab-org/gitlab-build-images:terraform
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

variables:
TF_VAR_gitlab_token: ${GITLAB_ACCESS_TOKEN}
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY}
AWS_SECRET_ACCESS_KEY : ${AWS_SECRET_KEY}
AWS_DEFAULT_REGION: "us-east-1"

cache:
paths:
- .terraform

before_script:
- terraform --version
- terraform init

stages:
- validate
- plan
- apply
- destroy

validate:
stage: validate
script:
- terraform validate

plan:
stage: plan
script:
- terraform plan -out="planfile"
dependencies:
- validate
artifacts:
paths:
- planfile

apply:
stage: apply
script:
- terraform apply -input=false "planfile"
dependencies:
- plan
when: manual

destroy:
stage: destroy
script:
- terraform destroy --auto-approve
when: manual

0 comments on commit e0b62de

Please sign in to comment.