-
Notifications
You must be signed in to change notification settings - Fork 248
docs(instances): add GitLab runner Terraform tutorial ext-add-instances #5284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SIMVIA-lucas-sovre
wants to merge
4
commits into
scaleway:main
Choose a base branch
from
SIMVIA-lucas-sovre:ext-add-instances
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
--- | ||
title: Dynamically create and destroy GitLab runner using Terraform | ||
description: How to dynamically create and destroy GitLab runner using Scaleway Terraform provider | ||
tags: automation terraform instances GitLab ci-cd | ||
categories: | ||
- instances | ||
- devtools | ||
--- | ||
|
||
import Requirements from '@macros/iam/requirements.mdx' | ||
|
||
## Gitlab runner overview | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
GitLab runners are the components that execute the jobs defined in your GitLab CI/CD pipelines. They can be installed on various operating systems and can run jobs on different platforms. Runners can be configured to be specific to a project or shared across multiple projects. | ||
With terraform Scaleway provider and GitLab one, runners can be dynamically created and destroyed. | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<Requirements /> | ||
|
||
- A Scaleway account logged into the [console](https://console.scaleway.com) | ||
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization | ||
- [Created IAM API key](/iam/how-to/create-api-keys/) | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- A GitLab account and project | ||
|
||
## Provision a GitLab runner on a Scaleway instance with terraform. | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Gitlab setup | ||
|
||
The following GitLab CI/CD needs 3 secret variables: | ||
- GITLAB_TOKEN | ||
- SCW_SECRET_KEY | ||
- SCW_ACCES_KEY | ||
|
||
The `GITLAB_TOKEN` needs to be a GitLab personal access token with the **Owner** role, and `create_runner` scope. | ||
Create the environment variables in your [project cicd settings](https://docs.gitlab.com/ci/variables/) | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### terraform setup | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Start by providing a basic Terraform setup: | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. Create the `main.tf` file: | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```hcl | ||
//providers | ||
terraform { | ||
required_providers { | ||
scaleway = { | ||
source = "scaleway/scaleway" | ||
} | ||
gitlab = { | ||
source = "gitlabhq/gitlab" | ||
} | ||
} | ||
required_version = ">= 0.13" | ||
} | ||
|
||
provider "gitlab" { | ||
token = var.gitlab_token | ||
} | ||
|
||
provider "scaleway" { | ||
zone = "fr-par-1" //change to the zone you want to use | ||
region = "fr-par" | ||
access_key = var.access_key | ||
secret_key = var.secret_key | ||
} | ||
|
||
resource "scaleway_instance_ip" "main" { | ||
tags = ["cicd"] //or any tag you want to apply to all ci/cd instances | ||
project_id = var.project_id | ||
} | ||
|
||
resource "scaleway_instance_server" "main" { | ||
type = var.instance_type | ||
image = "debian_bookworm" | ||
|
||
project_id = var.project_id | ||
|
||
ip_id = scaleway_instance_ip.main.id | ||
|
||
tags = ["ci-cd", gitlab_user_runner.project_runner.id, var.gitlab_project_name, var.gitlab_commit_branch] | ||
|
||
root_volume { | ||
size_in_gb = var.volume_size | ||
sbs_iops = 15000 | ||
} | ||
|
||
user_data = { | ||
cloud-init = local.cloud_init | ||
} | ||
} | ||
|
||
resource "gitlab_user_runner" "project_runner" { | ||
runner_type = "project_type" | ||
project_id = var.gitlab_project_id | ||
untagged = false | ||
tag_list = var.runner_tags | ||
maximum_timeout = var.job_timeout | ||
} | ||
|
||
locals { | ||
cloud_init = templatefile("${path.module}/cloud-init.yml", { | ||
token = gitlab_user_runner.project_runner.token, | ||
tags = join(",", var.runner_tags) | ||
}) | ||
} | ||
|
||
|
||
output "runner_id" { | ||
value = gitlab_user_runner.project_runner.id | ||
} | ||
``` | ||
2. Create the `var.tf` file: | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```hcl | ||
variable "project_id" { | ||
type = string | ||
description = "Your project ID." | ||
} | ||
|
||
variable "scw_org_id" { | ||
type = string | ||
description = "Your scaleway organisation ID" | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
variable "secret_key" { | ||
type = string | ||
sensitive = true | ||
} | ||
|
||
variable "access_key" { | ||
type = string | ||
} | ||
|
||
variable "gitlab_token" { | ||
type = string | ||
sensitive = true | ||
} | ||
|
||
variable "gitlab_project_id" { | ||
type = string | ||
} | ||
|
||
variable "gitlab_project_name" { | ||
type = string | ||
} | ||
|
||
variable "gitlab_commit_branch" { | ||
type = string | ||
} | ||
|
||
variable "runner_tags" { | ||
type = list(string) | ||
default = [] | ||
} | ||
|
||
variable "instance_type" { | ||
type = string | ||
default = "DEV1-S" | ||
} | ||
|
||
variable "job_timeout" { | ||
type = number | ||
default = 3600 | ||
} | ||
|
||
variable "volume_size" { | ||
type = number | ||
default = 50 | ||
description = "volume in Gb" | ||
} | ||
``` | ||
3. Create the `cloud-init.yml` file: | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```yml | ||
#cloud-config | ||
package_update: true | ||
package_upgrade: false | ||
|
||
runcmd: | ||
- sudo apt install -y curl | ||
- curl -fsSL https://get.docker.com | sudo sh | ||
- curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash | ||
- apt install -y gitlab-runner | ||
- | | ||
gitlab-runner register -n \ | ||
--url "https://gitlab.com/" \ | ||
--registration-token ${token} \ | ||
--executor docker \ | ||
--description "My Docker Runner" \ | ||
--docker-image "docker:24.0.5" \ | ||
--docker-privileged \ | ||
--tag-list "${tags}" \ | ||
--docker-volumes "/certs/client" | ||
``` | ||
|
||
### Gitlab setup | ||
|
||
1. Create the .gitlab-ci.yml : | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```yml | ||
stages: | ||
- provision | ||
- work | ||
- clean | ||
|
||
.default_env_vars: &default_env_vars | | ||
cat <<EOF > env.tfvars | ||
scw_org_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
access_key = "$SCW_ACCES_KEY" | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
secret_key = "$SCW_SECRET_KEY" | ||
gitlab_token = "$GITLAB_TOKEN" | ||
gitlab_project_id = "$CI_PROJECT_ID" | ||
runner_tags = [ "custom" ] | ||
instance_type = "PLAY2-PICO" | ||
job_timeout = 1200 | ||
volume_size = 20 | ||
gitlab_project_name="${CI_PROJECT_NAMESPACE_SLUG}-${CI_PROJECT_NAME}" | ||
gitlab_commit_branch="$CI_COMMIT_BRANCH" | ||
|
||
EOF | ||
|
||
provision: | ||
stage: provision | ||
image: alpine | ||
before_script: | ||
- apk update | ||
- apk add --no-cache wget unzip bash curl openssl libc6-compat jq | ||
- wget https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip | ||
- unzip terraform_1.6.6_linux_amd64.zip | ||
- mv terraform /usr/local/bin/ | ||
- terraform -version | ||
script: | ||
- *default_env_vars | ||
- terraform init -var-file="env.tfvars" | ||
- terraform apply -var-file="env.tfvars" -auto-approve | ||
- bash ./look.bash "$(terraform output -raw runner_id)" "$GITLAB_TOKEN" 600 | ||
artifacts: | ||
paths: | ||
- "*.tfstate*" | ||
when: always | ||
access: none | ||
|
||
work: | ||
stage: work | ||
image: alpine | ||
tags: | ||
- custom | ||
needs: | ||
- provision | ||
script: | ||
- echo "hello-world" | ||
|
||
clean: | ||
stage: clean | ||
image: alpine | ||
when: always | ||
needs: | ||
- job: work | ||
optional: true | ||
artifacts: false | ||
- provision | ||
before_script: | ||
- apk update | ||
- apk add --no-cache wget unzip bash curl openssl libc6-compat | ||
- wget https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip | ||
- unzip terraform_1.6.6_linux_amd64.zip | ||
- mv terraform /usr/local/bin/ | ||
- terraform -version | ||
script: | ||
- *default_env_vars | ||
- terraform init -var-file="env.tfvars" | ||
- terraform apply -destroy -var-file="env.tfvars" -auto-approve | ||
``` | ||
2. Create the `look.bash` file: | ||
```bash | ||
#!/bin/bash | ||
|
||
URL="https://gitlab.com/api/v4/runners/$1" | ||
TOKEN="$2" | ||
TIMEOUT=$3 | ||
START_TIME=$(date +%s) | ||
|
||
while true; do | ||
CURRENT_TIME=$(date +%s) | ||
ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) | ||
|
||
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then | ||
echo "Timeout: Runner did not come online within 5 minutes." | ||
exit 1 # Exit with error code | ||
fi | ||
|
||
STATUS=$(curl --silent --header "PRIVATE-TOKEN: $TOKEN" "$URL" | jq .status) | ||
|
||
if [ "$STATUS" == "\"online\"" ]; then | ||
echo "Runner is online." | ||
break | ||
else | ||
echo "Runner status: $STATUS. Retrying in 5 seconds..." | ||
sleep 5 | ||
fi | ||
done | ||
``` | ||
|
||
With this setup, GitLab-CI/CD creates an Instance and registers it as a GitLab runner, runs the workload on the Instance, then deletes the Instance and the runner. | ||
This setup allows you to dynamically provide high-computation runners in a cost-effective way. You can also customize the cloud init to fit your business needs, such as connecting to your company's VPN. | ||
SIMVIA-lucas-sovre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Going further | ||
|
||
If you want to use a custom Instance image, you can edit the `main.tf` file as below: | ||
|
||
```hcl | ||
// ... previous code | ||
|
||
data "scaleway_instance_image" "my_image" { | ||
image_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
} | ||
|
||
resource "scaleway_instance_server" "main" { | ||
type = var.instance_type | ||
image = data.scaleway_instance_image.my_image.id | ||
|
||
// ... |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.