Pipeline Setup

Now we are going to setup the piepline to automatically deploy our terraform code to our gitlab runner on our kubernetes cluster.

Variables Setup

We will setup CICD variables for viewscape.io https://gitlab.com/groups/viewscape-io/-/settings/ci_cd

This is where you add and edit variables for all of viewscape Group.

src

.gitlab-ci.yml

This will plan terraform and save it into an artifact that the apply job will apply to your cloudflare and piholes. Can also deploy to cluster

 1image:
 2  name: registry.gitlab.com/gitlab-org/terraform-images/releases/terraform:1.1.9
 3
 4stages:
 5  - plan
 6  - deploy
 7
 8default:
 9  tags:
10    - viewscape
11    - k8s
12
13variables:
14  TF_ROOT: $CI_PROJECT_DIR  # The relative path to the root directory of the Terraform project
15  TF_STATE_NAME: default  # The name of the state file used by the GitLab Managed Terraform state backend
16  ARTIFACT_VERSION: "1.0.0"
17  REALEASE_VERSION: $ARTIFACT_VERSION
18  PIHOLE_PASSWORD: $TF_VAR_PIHOLE_PASSWORD
19
20
21
22cache:
23  key: "${TF_ROOT}"
24  paths:
25    - ${TF_ROOT}/.terraform/
26
27before_script:
28  - rm -rf .terraform
29  - terraform --version
30  - terraform init
31
32
33plan:
34  stage: plan
35  script:
36    - terraform plan -out "plans"
37  artifacts:
38    paths:
39      - plans
40  tags:
41    - viewscape
42    - k8s
43
44deploy:
45  stage: deploy
46  script:
47    - terraform apply -input=false "plans"
48  tags:
49    - viewscape
50    - k8s
51  only:
52    - main
53  dependencies:
54    - plan