Skip to content

Run a Preview in GitLab CI

The following workflow is a blueprint and may require adjustments to fit your needs.

Terramate Cloud support

When synchronizing previews to Terramate Cloud, previews will be synchronized with a detailed status and can be reviewed on a stack level.

Preview Blueprint

Create the following GitLab CI workflow file at gitlab-ci/.plan.yml

yaml
plan:
  extends:
    - .common
    - .id_tokens
  only:
    - merge_requests
  stage: plan
  before_script:
    - !reference [.setup, script]
  script:
    - terramate fmt --check
    - export CHANGED_STACKS=$(terramate list --changed)
    - export CHANGED_STACKS_NUMBER=$(echo $CHANGED_STACKS |wc -w)
    - if [ $CHANGED_STACKS_NUMBER -eq 0 ]; then echo "No changed stacks. Exiting."; exit 0; else echo -e "Changed stacks:\n$CHANGED_STACKS"; fi
    - !reference [.auth, script]
    - terramate run --parallel 1 --changed -- terraform init -lock-timeout=5m
    - terramate run --parallel 5 --changed -- terraform validate
    - terramate run --parallel 5 --changed --sync-preview --terraform-plan-file=out.tfplan -- terraform plan -lock-timeout=5m -out=out.tfplan