1# Terraform/Base.latest
2#
3# The purpose of this template is to provide flexibility to the user so
4# they are able to only include the jobs that they find interesting.
5#
6# Therefore, this template is not supposed to run any jobs. The idea is to only
7# create hidden jobs. See: https://docs.gitlab.com/ee/ci/yaml/#hide-jobs
8#
9# There is a more opinionated template which we suggest the users to abide,
10# which is the lib/gitlab/ci/templates/Terraform.latest.gitlab-ci.yml
11
12image:
13  name: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
14
15variables:
16  TF_ROOT: ${CI_PROJECT_DIR}  # The relative path to the root directory of the Terraform project
17  TF_STATE_NAME: ${TF_STATE_NAME:-default}  # The name of the state file used by the GitLab Managed Terraform state backend
18
19cache:
20  key: "${TF_ROOT}"
21  paths:
22    - ${TF_ROOT}/.terraform/
23
24.terraform:fmt: &terraform_fmt
25  stage: validate
26  script:
27    - cd ${TF_ROOT}
28    - gitlab-terraform fmt
29  allow_failure: true
30
31.terraform:validate: &terraform_validate
32  stage: validate
33  script:
34    - cd ${TF_ROOT}
35    - gitlab-terraform validate
36
37.terraform:build: &terraform_build
38  stage: build
39  script:
40    - cd ${TF_ROOT}
41    - gitlab-terraform plan
42    - gitlab-terraform plan-json
43  resource_group: ${TF_STATE_NAME}
44  artifacts:
45    paths:
46      - ${TF_ROOT}/plan.cache
47    reports:
48      terraform: ${TF_ROOT}/plan.json
49
50.terraform:deploy: &terraform_deploy
51  stage: deploy
52  script:
53    - cd ${TF_ROOT}
54    - gitlab-terraform apply
55  resource_group: ${TF_STATE_NAME}
56  rules:
57    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
58      when: manual
59
60.terraform:destroy: &terraform_destroy
61  stage: cleanup
62  script:
63    - cd ${TF_ROOT}
64    - gitlab-terraform destroy
65  resource_group: ${TF_STATE_NAME}
66  when: manual
67