Terraspace Gitlab Pipelines

Hi is there documentation for using Terraspace in Gitlab CI/CD pipelines? Thanks!

1 Like

Not yet. It’s actually something that am working on. Hopefully will have some docs soon. Thanks!

Hello Tung,

i hope this help. I have build a gitlab ci with the follow code

Follow features are included:
Usage of terraform fmt / validate / gitlab terraform output / Log output as artifact

Terraspace Gitlab ci piple deployment

default :
  image:
    name : boltops/terraspace:ubuntu
    entrypoint: [""] # force an empty entrypoint
variables:
  TF_ROOT: "${CI_PROJECT_DIR}"
  TF_SUBFOLDER: "***"  # The relative path to the root directory of the Terraform project equal projectname

  AWS_REGION: "+++"
  PLAN: plan.cache
  PLAN_JSON: plan.json
  PLAN_JSON_nomad: plan_nomad.json
  PLAN_SUMMARY: plan_summary.json
  PLAN_SUMMARY_CACHE: plan_summary_cache.json

cache:
  paths:
  - "${TF_ROOT}/${TF_SUBFOLDER}/.terraspace-cache"
stages:
  - validate
  - init
  - build
  - deploy

before_script:
  - apt-get update && apt-get install -y locales
  - echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
  - locale-gen
  - export LC_ALL=en_US.UTF-8
  - cd "${TF_ROOT}/${TF_SUBFOLDER}"
  - shopt -s expand_aliases
  - alias convert_report="jq -r '([.resource_changes[]?.change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}'"

terraspace_fmt:
  stage: validate
  script:
    - terraform --version
    - bundle install
    - terraspace fmt
  allow_failure: true

terraspace_validate:
  stage: validate
  script:
    - bundle
    - terraspace all validate
  allow_failure: false

terraspace_init:
  stage: init
  script:
    - bundle
    - terraspace all init
  allow_failure: false

terraspace_build:
  stage: build
  script:
    - apt-get update && apt-get install -y jq
    - bundle install
    - terraspace all plan --out ":MOD_NAME.plan"
    - find ./app/stacks -maxdepth 1 | cut -c 14- >> stacks.txt
    - sed -i '/^$/d' stacks.txt
    - awk 'NF' stacks.txt > stacks.tmp
    - rm stacks.txt
    - mv stacks.tmp stacks.txt
    - while IFS= read -r line; do terraspace show "$line" --json --plan "$line.plan" | convert_report >  "$line.json" ; done < stacks.txt
    - while IFS= read -r line; do cat "$line.json" >> "$PLAN_SUMMARY_CACHE"; done < stacks.txt
    - jq --slurp 'reduce (paths(numbers) as $p | [$p[1:], getpath($p)]) as $i ({}; setpath($i[0]; getpath($i[0]) + $i[1]))' "$PLAN_SUMMARY_CACHE" >> "$PLAN_SUMMARY"
    - cat $PLAN_SUMMARY
    - rm *.plan

  artifacts:
    paths:
      - "${TF_ROOT}/${TF_SUBFOLDER}/$PLAN_SUMMARY"
      - "${TF_ROOT}/${TF_SUBFOLDER}/log/plan"

    reports:
      terraform: "${TF_ROOT}/${TF_SUBFOLDER}/$PLAN_SUMMARY"

terraspace_deploy:
  stage: deploy
  script:
    - bundle
    - terraspace all up --yes
  dependencies:
    - terraspace_build
  artifacts:
    paths:
      - "${TF_ROOT}/${TF_SUBFOLDER}/log/up"
  when: manual
  only:
    - main
1 Like

Awesome. Thanks. There are also now some docs GitLab - Terraspace The docs use the terraspace_ci_gitlab plugin to add some extra conveniences like creating a PR comment with a link to Terraspace Cloud. You can also use the template as an example and modify it for your own needs. https://github.com/boltops-tools/terraspace_ci_gitlab/blob/main/lib/template/.gitlab-ci.yml

Hi Tung, when i attempt to create a new CI i get this error: /Users/alexisnieves/.rvm/gems/ruby-2.7.2/gems/terraspace_ci_gitlab-0.1.0/lib/terraspace_ci_gitlab.rb:14:in <top (required)>’: uninitialized constant Terraspace::Cloud (NameError)`

Can you please assist as to why I am getting this? Thanks!

Seems like that project is still using terraspace v1. Wondering what this returns:

cd project-folder
bundle info terraspace

It should show something like this:

$ bundle info terraspace
  * terraspace (2.0.0)
        Summary: Terraspace: The Terraspace Framework
        Homepage: https://terraspace.cloud
        Source Code: https://github.com/boltops-tools/terraspace
        Changelog: https://github.com/boltops-tools/terraspace/blob/master/CHANGELOG.md
        Path: /home/ec2-user/.rvm/gems/ruby-3.1.1/gems/terraspace-2.0.0
$

If not, to upgrade:

bundle update terraspace

You can also more generally update all gem libraries

bundle update

Then check again with:

bundle info terraspace

We’re looking to confirm that terraspace v2 is being used. Also:

Hope that helps :+1: