This repository demonstrates how to integrate the Resourcely guardrail validation job into a repository using GitLab CI/CD. It runs Terraform using the official Hashicorp Terraform Docker Images.
This repository uses GitLab CI/CD to run terraform plan. Once a plan is downloaded to a designated directory, the Resourcely guardrail validation job runs on the configured path. If you use a different runner, see the scaffolding repository for that runner:
- A Resourcely Account
- Resourcely GitLab SCM Configured
- GitLab Premium or Ultimate subscription
- Maintainer Role or Higher in the GitLab project
- AWS Provider Credentials
- Import this project to your GitLab group by URL
a. On the left sidebar, at the top, select Create new (+) and New project/repository
b. Select Import project
c. Select Repository by URL
d. Enter the Git repository URL: https://github.com/Resourcely-Inc/scaffolding-gitlab-pipeline.git
e. Complete the remaining fields
f. Select Create project - Allow Resourcely to monitor the newly created project
- Generate a Resourcely API Token and save it in a safe place
- Add your Resourcely API Token to your GitLab project CI/CD variables
a. Go to the GitLab project that Resourcely will validate
b. In the side tab, navigate to Settings > CI/CD
c. Expand the Variables tab
d. Click the Add variable button
e. Add theRESOURCELY_API_TOKEN
as the key and the token as the value f. Evaluate whether to unselect Protect variable, depending on the need to use the token in un-protected branches, while considering security implications
g. Select the Mask variable to protect sensitive data from being seen in job logs
h. Unselect Expand variable reference
i. Press the Add variable button - Add your AWS credentials
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
to GitLab following the same process in step 3 - Provision Infrastructure using Resourcely
Once a new Resource has been created via Merge-Request, the Resourcely job will automatically kick-off. It runs in the test stage by default.
When a merge-request is created using Resourcely:
- GitLab CI kicks off the
validate
stage
a. Thehashicorp/terraform:light
container image is loaded
b.terraform init
is run to initialize a working directory containing Terraform configuration files
c.terraform validate
is run to validate the Terraform configuration files - After
validate
stage completes, GitLab CI kicks off theplan
stage
a. Thehashicorp/terraform:light
container image is loaded
b.terraform init
is run to initialize a working directory containing Terraform configuration files
c.terraform plan
is run to create an execution plan
d.terraform show
is run to download the plan as a json
e. The plan json is stored as a GitLab artifact in the$TF_PLAN_DIRECTORY
- After the
plan
stage completes, GitLab CI kicks off thetest
stage
a. Thetest
stage is loaded by the Resourcely template that is included in this project's.gitlab-ci.yml
b. Theghcr.io/resourcely-inc/resourcely-cli:$RESOURCELY_IMAGE
container image is loaded
c. Theresourcely_guardrails
job runsresourcely-cli evaluate
scanning the Terraform plan json(s)
d. The resources generated with Resourcely within the merge-request are validated against your Resourcely guardrails - The
test
stage completes
a. If guardrail violations are found, Resourcely will assign a reviewer to the merge-request and require approval before it can be merged