Skip to content

Commit 9153a3a

Browse files
authored
feat: add option to use a pre-registered Runner (#1115)
## Description GitLab announced then [Next GitLab Runner Token Architecture](https://docs.gitlab.com/ee/architecture/blueprints/runner_tokens/index.html#proposal). Runners have to be registered manually. This PR adds a new import parameter `runner_gitlab.preregistered_runner_token_ssm_parameter_name` holding the name of a SSM parameter (type: `SecuredString`). This parameter contains the GitLab Runner token obtained from GitLab. All other registration methods will still work, but have been marked as deprecated and will be removed with [v8.0.0](https://github.com/cattle-ops/terraform-aws-gitlab-runner/milestone/4) end of the year. This also solves the problems with Runners removed from GitLab at shutdown, resulting in new Runners not able to start. Closes #1074 and #1109 ## Verification - [x] deployed the module using the new registration version. Runner is online. - [x] deployed the module using the old authentication schema. Runner is online.
1 parent 19cb34b commit 9153a3a

File tree

14 files changed

+186
-314
lines changed

14 files changed

+186
-314
lines changed

docs/usage.md

+14-123
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,22 @@ Common pitfalls are documented in [pitfalls.md](pitfalls.md).
55
## Configuration
66

77
The examples are configured with defaults that should work in general. The examples are in general configured for the
8-
region Ireland `eu-west-1`. The only parameter that needs to be provided is the GitLab are the registration token and the
9-
URL of your GitLab instance. The token can be found in GitLab in the runner section (global, group or repo scope).
10-
Create a file `terraform.tfvars` and the registration token.
8+
region Ireland `eu-west-1`. The only parameter that needs to be provided is the name of a SSM parameter holding the Runner
9+
registration token and the URL of your GitLab instance. The Runner token is created in GitLab during the Runner registration process.
10+
Create a file `terraform.tfvars` and put the Runner registration token in the SSM parameter.
1111

1212
```hcl
13-
registration_token = "MY_TOKEN"
13+
preregistered_runner_token_ssm_parameter_name = "my-gitlab-runner-token-ssm-parameter-name"
1414
gitlab_url = "https://my.gitlab.instance/"
1515
```
1616

1717
The base image used to host the GitLab Runner agent is the latest available Amazon Linux 2 HVM EBS AMI. In previous versions of
1818
this module a hard coded list of AMIs per region was provided. This list has been replaced by a search filter to find the latest
1919
AMI. Setting the filter to `amzn2-ami-hvm-2.0.20200207.1-x86_64-ebs` will allow you to version lock the target AMI if needed.
2020

21-
> 💥 **If you are using GitLab >= 16.0.0**: `registration_token` will be deprecated!
22-
23-
>GitLab >= 16.0.0 has removed the `registration_token` since they are working on a [new token architecture](https://docs.gitlab.com/ee/architecture/blueprints/runner_tokens/). This module handle these changes, you need to provide a personal access token with `api` scope for the runner to authenticate itself.
24-
25-
>The workflow is as follows ([migration steps](https://github.com/cattle-ops/terraform-aws-gitlab-runner/pull/876)):
26-
>1. The runner make an API call (with the access token) to create a new runner on GitLab depending on its type (`instance`, `group` or `project`).
27-
>2. GitLab answers with a token prefixed by `glrt-` and we put it in SSM.
28-
>3. The runner will get the config from `/etc/gitlab-runner/config.toml` and will listen for new jobs from your GitLab instance.
21+
The Runner uses a token to register with GitLab. This token is stored in the AWS SSM parameter store. The token has to be created
22+
manually in GitLab and stored in the SSM parameter store. All other registration methods are deprecated and will be removed in
23+
v8.0.0.
2924

3025
## Install the module
3126

@@ -45,40 +40,7 @@ terraform destroy
4540

4641
## Scenarios
4742

48-
### Scenario: Basic usage on GitLab **< 16.0.0**
49-
50-
Below is a basic examples of usages of the module. Regarding the dependencies such as a VPC, have a look at the [default example](https://github.com/cattle-ops/terraform-aws-gitlab-runner/tree/main/examples/runner-default).
51-
52-
```hcl
53-
module "runner" {
54-
# https://registry.terraform.io/modules/cattle-ops/gitlab-runner/aws/
55-
source = "cattle-ops/gitlab-runner/aws"
56-
57-
environment = "basic"
58-
59-
vpc_id = module.vpc.vpc_id
60-
subnet_id = element(module.vpc.private_subnets, 0)
61-
62-
runner_gitlab = {
63-
url = "https://gitlab.com"
64-
}
65-
66-
runner_gitlab_registration_config = {
67-
registration_token = "my-token"
68-
tag_list = "docker"
69-
description = "runner default"
70-
locked_to_project = "true"
71-
run_untagged = "false"
72-
maximum_timeout = "3600"
73-
}
74-
75-
runner_worker_docker_machine_instance = {
76-
subnet_ids = module.vpc.private_subnets
77-
}
78-
}
79-
```
80-
81-
### Scenario: Basic usage on GitLab **>= 16.0.0**
43+
### Scenario: Basic usage
8244

8345
Below is a basic examples of usages of the module if your GitLab instance version is >= 16.0.0.
8446

@@ -99,20 +61,9 @@ module "runner" {
9961
10062
runner_gitlab = {
10163
url = "https://gitlab.com"
102-
access_token_secure_parameter_store_name = "gitlab_access_token_ssm_name"
64+
65+
preregistered_runner_token_ssm_parameter_name = "my-gitlab-runner-token-ssm-parameter-name"
10366
}
104-
105-
runner_gitlab_registration_config = {
106-
type = "instance" # or "group" or "project"
107-
# group_id = 1234 # for "group"
108-
# project_id = 5678 # for "project"
109-
tag_list = "docker"
110-
description = "runner default"
111-
locked_to_project = "true"
112-
run_untagged = "false"
113-
maximum_timeout = "3600"
114-
}
115-
11667
}
11768
```
11869

@@ -135,15 +86,8 @@ module "runner" {
13586
13687
runner_gitlab = {
13788
url = "https://gitlab.com"
138-
}
139-
140-
runner_gitlab_registration_config = {
141-
registration_token = "my-token"
142-
tag_list = "docker"
143-
description = "runner default"
144-
locked_to_project = "true"
145-
run_untagged = "false"
146-
maximum_timeout = "3600"
89+
90+
preregistered_runner_token_ssm_parameter_name = "my-gitlab-runner-token-ssm-parameter-name"
14791
}
14892
14993
runner_worker_cache = {
@@ -181,16 +125,9 @@ module "runner" {
181125
182126
runner_gitlab = {
183127
url = "https://gitlab.com"
184-
}
185128
186-
runner_gitlab_registration_config = {
187-
registration_token = "my-token"
188-
tag_list = "docker"
189-
description = "runner default"
190-
locked_to_project = "true"
191-
run_untagged = "false"
192-
maximum_timeout = "3600"
193-
}
129+
preregistered_runner_token_ssm_parameter_name = "my-gitlab-runner-token-ssm-parameter-name"
130+
}
194131
195132
runner_worker = {
196133
type = "docker+machine"
@@ -243,52 +180,6 @@ If a KMS key is set via `kms_key_id`, make sure that you also give proper access
243180
get errors, e.g. the build cache can't be decrypted or logging via CloudWatch is not possible. For a CloudWatch
244181
example checkout [kms-policy.json](https://github.com/cattle-ops/terraform-aws-gitlab-runner/blob/main/policies/kms-policy.json)
245182

246-
### GitLab runner token configuration
247-
248-
By default, the runner is registered on initial deployment. In previous versions of this module this was a manual process. The
249-
manual process is still supported but will be removed in future releases. The runner token will be stored in the AWS SSM parameter
250-
store. See [example](../examples/runner-pre-registered) for more details.
251-
252-
To register the runner automatically set the variable `gitlab_runner_registration_config["registration_token"]`. This token value
253-
can be found in your GitLab project, group, or global settings. For a generic runner you can find the token in the admin section.
254-
By default, the runner will be locked to the target project and not run untagged jobs. Below is an example of the configuration map.
255-
256-
```hcl
257-
runner_gitlab_registration_config = {
258-
registration_token = "<registration token>"
259-
tag_list = "<your tags, comma separated>"
260-
description = "<some description>"
261-
locked_to_project = "true"
262-
run_untagged = "false"
263-
maximum_timeout = "3600"
264-
# ref_protected runner will only run on pipelines triggered on protected branches. Defaults to not_protected
265-
access_level = "<not_protected OR ref_protected>"
266-
}
267-
```
268-
269-
The registration token can also be read in via SSM parameter store. If no registration token is passed in, the module
270-
will look up the token in the SSM parameter store at the location specified by
271-
`runner_gitlab_registration_token_secure_parameter_store_name`.
272-
273-
For migration to the new setup simply add the runner token to the parameter store. Once the runner is started it will look up the
274-
required values via the parameter store. If the value is `null` a new runner will be registered and a new token created/stored.
275-
276-
```sh
277-
# set the following variables, look up the variables in your Terraform config.
278-
# see your Terraform variables to fill in the vars below.
279-
aws-region=<${var.aws_region}>
280-
token=<runner-token-see-your-gitlab-runner>
281-
parameter-name=<${var.environment}>-<${var.secure_parameter_store_runner_token_key}>
282-
283-
aws ssm put-parameter --overwrite --type SecureString --name "${parameter-name}" --value ${token} --region "${aws-region}"
284-
```
285-
286-
Once you have created the parameter, you must remove the variable `runner_gitlab.registration_token` from your config. The next
287-
time your GitLab runner instance is created it will look up the token from the SSM parameter store.
288-
289-
Finally, the runner still supports the manual runner creation. No changes are required. Please keep in mind that this setup will be
290-
removed in future releases.
291-
292183
### Auto Scaling Group
293184

294185
#### Scheduled scaling

examples/runner-certificates/main.tf

+4-13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module "vpc_endpoints" {
4242
}
4343
}
4444

45+
4546
module "runner" {
4647
source = "../../"
4748

@@ -57,7 +58,9 @@ module "runner" {
5758
# Public cert of my company's gitlab instance
5859
# Other public certs relating to my company.
5960
runner_gitlab = {
60-
url = var.gitlab_url
61+
url = var.gitlab_url
62+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
63+
6164
certificate = file("${path.module}/my_gitlab_instance_cert.crt")
6265
ca_certificate = file("${path.module}/my_company_ca_cert_bundle.crt")
6366
}
@@ -74,18 +77,6 @@ module "runner" {
7477
]
7578
}
7679

77-
###############################################
78-
# Registration
79-
###############################################
80-
runner_gitlab_registration_config = {
81-
registration_token = var.registration_token
82-
tag_list = "docker_runner"
83-
description = "runner docker - auto"
84-
locked_to_project = "true"
85-
run_untagged = "false"
86-
maximum_timeout = "3600"
87-
}
88-
8980
###############################################
9081
# Network
9182
###############################################

examples/runner-certificates/variables.tf

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ variable "gitlab_url" {
2222
default = "https://gitlab.com"
2323
}
2424

25-
variable "registration_token" {
26-
description = "Gitlab runner registration token"
25+
variable "preregistered_runner_token_ssm_parameter_name" {
26+
description = "The name of the SSM parameter to read the preregistered GitLab Runner token from."
2727
type = string
28-
default = "something"
29-
}
28+
}

examples/runner-default/main.tf

+1-8
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,8 @@ module "runner" {
6767

6868
runner_gitlab = {
6969
url = var.gitlab_url
70-
}
7170

72-
runner_gitlab_registration_config = {
73-
registration_token = var.registration_token
74-
tag_list = "docker_spot_runner"
75-
description = "runner default - auto"
76-
locked_to_project = "true"
77-
run_untagged = "false"
78-
maximum_timeout = "3600"
71+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
7972
}
8073

8174
runner_worker_gitlab_pipeline = {

examples/runner-default/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ variable "gitlab_url" {
2222
default = "https://gitlab.com"
2323
}
2424

25-
variable "registration_token" {
26-
description = "Registration token for the runner."
25+
variable "preregistered_runner_token_ssm_parameter_name" {
26+
description = "The name of the SSM parameter to read the preregistered GitLab Runner token from."
2727
type = string
2828
}
2929

examples/runner-docker/main.tf

+1-8
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,8 @@ module "runner" {
5656

5757
runner_gitlab = {
5858
url = var.gitlab_url
59-
}
6059

61-
runner_gitlab_registration_config = {
62-
registration_token = var.registration_token
63-
tag_list = "docker_runner"
64-
description = "runner docker - auto"
65-
locked_to_project = "true"
66-
run_untagged = "false"
67-
maximum_timeout = "3600"
60+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
6861
}
6962

7063
runner_worker = {

examples/runner-docker/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ variable "gitlab_url" {
2222
default = "https://gitlab.com"
2323
}
2424

25-
variable "registration_token" {
26-
description = "Registration token for the runner."
25+
variable "preregistered_runner_token_ssm_parameter_name" {
26+
description = "The name of the SSM parameter to read the preregistered GitLab Runner token from."
2727
type = string
2828
}

examples/runner-pre-registered/main.tf

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ module "runner" {
5454
}
5555

5656
runner_gitlab = {
57-
url = var.gitlab_url
58-
registration_token = var.runner_token
57+
url = var.gitlab_url
58+
59+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
5960
}
6061

6162
# working 9 to 5 :)

examples/runner-pre-registered/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ variable "gitlab_url" {
2020
type = string
2121
}
2222

23-
variable "runner_token" {
24-
description = "Token for the runner, will be used in the runner config.toml"
23+
variable "preregistered_runner_token_ssm_parameter_name" {
24+
description = "The name of the SSM parameter to read the preregistered GitLab Runner token from."
2525
type = string
2626
}
2727

examples/runner-public/main.tf

+2-17
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,8 @@ module "runner" {
4242

4343
runner_gitlab = {
4444
url = var.gitlab_url
45-
}
4645

47-
runner_gitlab_registration_config = {
48-
registration_token = var.registration_token
49-
tag_list = "docker_spot_runner"
50-
description = "runner public - auto"
51-
locked_to_project = "true"
52-
run_untagged = "false"
53-
maximum_timeout = "3600"
54-
access_level = "ref_protected"
46+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
5547
}
5648

5749
runner_worker = {
@@ -89,15 +81,8 @@ module "runner2" {
8981

9082
runner_gitlab = {
9183
url = var.gitlab_url
92-
}
9384

94-
runner_gitlab_registration_config = {
95-
registration_token = var.registration_token
96-
tag_list = "docker_spot_runner_2"
97-
description = "runner public - auto"
98-
locked_to_project = "true"
99-
run_untagged = "false"
100-
maximum_timeout = "3600"
85+
preregistered_runner_token_ssm_parameter_name = var.preregistered_runner_token_ssm_parameter_name
10186
}
10287

10388
runner_worker_cache = {

examples/runner-public/variables.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ variable "gitlab_url" {
2222
default = "https://gitlab.com"
2323
}
2424

25-
variable "registration_token" {
26-
description = "Registration token for the runner."
25+
variable "preregistered_runner_token_ssm_parameter_name" {
26+
description = "The name of the SSM parameter to read the preregistered GitLab Runner token from."
2727
type = string
2828
}

main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ locals {
5959
post_install = var.runner_install.post_install_script
6060
runners_gitlab_url = var.runner_gitlab.url
6161
runners_token = var.runner_gitlab.registration_token
62+
preregistered_runner_token_ssm_parameter_name = var.runner_gitlab.preregistered_runner_token_ssm_parameter_name
6263
secure_parameter_store_gitlab_runner_registration_token_name = var.runner_gitlab_registration_token_secure_parameter_store_name
6364
secure_parameter_store_runner_token_key = local.secure_parameter_store_runner_token_key
6465
secure_parameter_store_runner_sentry_dsn = local.secure_parameter_store_runner_sentry_dsn

0 commit comments

Comments
 (0)