Skip to content
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

refactor!: remove manual registration of Gitlab Runner #388

Draft
wants to merge 41 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
642924b
Remove variable secure_parameter_store_runner_token_key
kayman-mk Oct 14, 2021
00a9493
Add documentation on how to provide the Gitlab token safely
kayman-mk Oct 14, 2021
74763a4
Remove documentation about SSH and EC2 key pairs
kayman-mk Oct 14, 2021
991198a
Amend documentation
kayman-mk Oct 14, 2021
70b2032
Remove pre-registered example. No longer supported.
kayman-mk Oct 14, 2021
a5b44f8
Amend runner-default example
kayman-mk Oct 14, 2021
d56b788
Ammend runner-docker example
kayman-mk Oct 14, 2021
cb2b0f1
Amend runner-public example
kayman-mk Oct 14, 2021
fbaf149
Remove the check of the pre-registered example. It's no longer there.
kayman-mk Oct 14, 2021
bafe59c
Merge develop branch
kayman-mk Oct 30, 2021
1c52c8d
Remove variable secure_parameter_store_runner_token_key
kayman-mk Oct 14, 2021
1a2eedb
Add documentation on how to provide the Gitlab token safely
kayman-mk Oct 14, 2021
d704b3c
Remove documentation about SSH and EC2 key pairs
kayman-mk Oct 14, 2021
24a1026
Amend documentation
kayman-mk Oct 14, 2021
b531f39
Remove pre-registered example. No longer supported.
kayman-mk Oct 14, 2021
35a98ea
Amend runner-default example
kayman-mk Oct 14, 2021
9e9bd12
Ammend runner-docker example
kayman-mk Oct 14, 2021
7d924cd
Amend runner-public example
kayman-mk Oct 14, 2021
9e3eb37
Remove the check of the pre-registered example. It's no longer there.
kayman-mk Oct 14, 2021
99195e5
Merge branch 'kayma/register-runner-manually' of github.com:kayman-mk…
kayman-mk Nov 13, 2021
4a29aef
Add documentation on how to provide the Gitlab token safely
kayman-mk Oct 14, 2021
7017ac0
Remove documentation about SSH and EC2 key pairs
kayman-mk Oct 14, 2021
53dc945
Amend documentation
kayman-mk Oct 14, 2021
68d8488
Remove pre-registered example. No longer supported.
kayman-mk Oct 14, 2021
226ba9b
Amend runner-default example
kayman-mk Oct 14, 2021
330638d
Ammend runner-docker example
kayman-mk Oct 14, 2021
a0729f0
Amend runner-public example
kayman-mk Oct 14, 2021
bfe6a21
Remove the check of the pre-registered example. It's no longer there.
kayman-mk Oct 14, 2021
f5d146d
Merge branch 'kayma/register-runner-manually' of github.com:kayman-mk…
kayman-mk Nov 13, 2021
9270eec
Merge remote-tracking branch 'origin/develop' into kayma/register-run…
kayman-mk Feb 27, 2022
bb8bd3e
remove check for pre-registered runner
kayman-mk Feb 27, 2022
def8e10
merge develop
kayman-mk Aug 6, 2022
6bde737
small documentation changes
kayman-mk Aug 23, 2022
12dca69
Merge branch 'develop' into kayma/register-runner-manually
kayman-mk Dec 1, 2022
d04eb7a
fix docs
kayman-mk Dec 1, 2022
74fe573
Merge branch 'main' into kayma/register-runner-manually
kayman-mk Jan 3, 2023
e233e4c
merge main
kayman-mk Feb 26, 2023
9ff9f87
undo variable deletion to avoid breaking change
kayman-mk Mar 2, 2023
b897920
merge master
kayman-mk Nov 30, 2023
dc9867d
erge main
kayman-mk Nov 30, 2023
faedae2
remove multi-region example again
kayman-mk Nov 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jobs:
"runner-default",
"runner-docker",
"runner-multi-region",
"runner-pre-registered",
"runner-public",
]
defaults:
Expand Down Expand Up @@ -90,5 +89,5 @@ jobs:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
cp .release/* .
yarn
yarn
yarn release --repositoryUrl https://x-access-token:[email protected]/$GITHUB_REPOSITORY.git
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,38 @@ resource "aws_iam_service_linked_role" "autoscaling" {

By default the runner is registered on initial deployment. In previous versions of this module this was a manual process. The manual process is still supported but will be removed in future releases. The runner token will be stored in the AWS SSM parameter store. See [example](examples/runner-pre-registered/) for more details.

To register the runner automatically set the variable `gitlab_runner_registration_config["registration_token"]`. This token value can be found in your GitLab project, group, or global settings. For a generic runner you can find the token in the admin section. By default the runner will be locked to the target project, not run untagged. Below is an example of the configuration map.
To register the runner automatically set the variable `gitlab_runner_registration_config["registration_token"]`. This token value can be found in your GitLab project, group, or global settings. For a generic runner you can find the token in the admin section. By default the runner will be locked to the target project, not run untagged. Below is an example of the configuration map. As this token should not appear in your source code management tool, use a SSM parameter which is set manually.
kayman-mk marked this conversation as resolved.
Show resolved Hide resolved

```hcl
gitlab_runner_registration_config = {
registration_token = "<registration token>"
tag_list = "<your tags, comma separated>"
description = "<some description>"
locked_to_project = "true"
run_untagged = "false"
maximum_timeout = "3600"
access_level = "<not_protected OR ref_protected, ref_protected runner will only run on pipelines triggered on protected branches. Defaults to not_protected>"
}
```
module "gitlab_runner" {
...

For migration to the new setup simply add the runner token to the parameter store. Once the runner is started it will lookup the required values via the parameter store. If the value is `null` a new runner will be registered and a new token created/stored.
gitlab_runner_registration_config = {
registration_token = aws_ssm_parameter.gitlab_runner_registration_token.value
tag_list = "<your tags, comma separated>"
description = "<some description>"
locked_to_project = "true"
run_untagged = "false"
maximum_timeout = "3600"
access_level = "<not_protected OR ref_protected, ref_protected runner will only run on pipelines triggered on protected branches. Defaults to not_protected>"
}
}

```sh
# set the following variables, look up the variables in your Terraform config.
# see your Terraform variables to fill in the vars below.
aws-region=<${var.aws_region}>
token=<runner-token-see-your-gitlab-runner>
parameter-name=<${var.environment}>-<${var.secure_parameter_store_runner_token_key}>
# obtain this token from your Gitlab instance and store it manually in the SSM parameter

aws ssm put-parameter --overwrite --type SecureString --name "${parameter-name}" --value ${token} --region "${aws-region}"
```
resource "aws_ssm_parameter" "gitlab_runner_registration_token" {
kayman-mk marked this conversation as resolved.
Show resolved Hide resolved
name = "gitlab-registration-token"
type = "SecureString"
value = "Please fill manually."
description = "Gitlab registration token for a new runner."

Once you have created the parameter, you must remove the variable `runners_token` from your config. The next time your GitLab runner instance is created it will look up the token from the SSM parameter store.
lifecycle {
# the secret is set manually
ignore_changes = [value]
}
}```

Finally, the runner still supports the manual runner creation. No changes are required. Please keep in mind that this setup will be removed in future releases.
After deploying this infrastructure, fill in the token manually and kill the agents. After the automatic restart, all runners register automatically.
kayman-mk marked this conversation as resolved.
Show resolved Hide resolved

### Auto Scaling Group Instance Termination

Expand All @@ -159,8 +162,6 @@ persist that contains the packaged Lambda function.

A few option are provided to access the runner instance:

1. Provide a public ssh key to access the runner by setting \`\`.
2. Provide a EC2 key pair to access the runner by setting \`\`.
3. Access via the Session Manager (SSM) by setting `enable_runner_ssm_access` to `true`. The policy to allow access via SSM is not very restrictive.
4. By setting none of the above, no keys or extra policies will be attached to the instance. You can still configure you own policies by attaching them to `runner_agent_role_arn`.

Expand Down Expand Up @@ -492,7 +493,6 @@ Made with [contributors-img](https://contrib.rocks).
| <a name="input_runners_volumes_tmpfs"></a> [runners\_volumes\_tmpfs](#input\_runners\_volumes\_tmpfs) | Mount a tmpfs in runner container. https://docs.gitlab.com/runner/executors/docker.html#mounting-a-directory-in-ram | <pre>list(object({<br> volume = string<br> options = string<br> }))</pre> | `[]` | no |
| <a name="input_schedule_config"></a> [schedule\_config](#input\_schedule\_config) | Map containing the configuration of the ASG scale-in and scale-up for the runner instance. Will only be used if enable\_schedule is set to true. | `map(any)` | <pre>{<br> "scale_in_count": 0,<br> "scale_in_recurrence": "0 18 * * 1-5",<br> "scale_out_count": 1,<br> "scale_out_recurrence": "0 8 * * 1-5"<br>}</pre> | no |
| <a name="input_secure_parameter_store_runner_sentry_dsn"></a> [secure\_parameter\_store\_runner\_sentry\_dsn](#input\_secure\_parameter\_store\_runner\_sentry\_dsn) | The Sentry DSN name used to store the Sentry DSN in Secure Parameter Store | `string` | `"sentry-dsn"` | no |
| <a name="input_secure_parameter_store_runner_token_key"></a> [secure\_parameter\_store\_runner\_token\_key](#input\_secure\_parameter\_store\_runner\_token\_key) | The key name used store the Gitlab runner token in Secure Parameter Store | `string` | `"runner-token"` | no |
| <a name="input_sentry_dsn"></a> [sentry\_dsn](#input\_sentry\_dsn) | Sentry DSN of the project for the runner to use (uses legacy DSN format) | `string` | `"__SENTRY_DSN_REPLACED_BY_USER_DATA__"` | no |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | Subnet id used for the runner and executors. Must belong to the VPC specified above. | `string` | `""` | no |
| <a name="input_subnet_id_runners"></a> [subnet\_id\_runners](#input\_subnet\_id\_runners) | Deprecated! Use subnet\_id instead. List of subnets used for hosting the gitlab-runners. | `string` | `""` | no |
Expand Down
2 changes: 2 additions & 0 deletions examples/runner-default/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ variable "gitlab_url" {
}

variable "registration_token" {
description = "The registration token obtained from your Gitlab instance."
type = string
}

variable "timezone" {
Expand Down
2 changes: 2 additions & 0 deletions examples/runner-docker/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ variable "gitlab_url" {
}

variable "registration_token" {
description = "The registration token obtained from your Gitlab instance."
type = string
}
75 changes: 0 additions & 75 deletions examples/runner-pre-registered/README.md

This file was deleted.

Empty file.
48 changes: 0 additions & 48 deletions examples/runner-pre-registered/main.tf

This file was deleted.

11 changes: 0 additions & 11 deletions examples/runner-pre-registered/providers.tf

This file was deleted.

32 changes: 0 additions & 32 deletions examples/runner-pre-registered/variables.tf

This file was deleted.

26 changes: 0 additions & 26 deletions examples/runner-pre-registered/versions.tf

This file was deleted.

2 changes: 2 additions & 0 deletions examples/runner-public/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ variable "gitlab_url" {
}

variable "registration_token" {
description = "The registration token obtained from your Gitlab instance."
type = string
}
3 changes: 1 addition & 2 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ locals {
# Ensure max builds is optional
runners_max_builds_string = var.runners_max_builds == 0 ? "" : format("MaxBuilds = %d", var.runners_max_builds)

# Define key for runner token for SSM
secure_parameter_store_runner_token_key = "${var.environment}-${var.secure_parameter_store_runner_token_key}"
secure_parameter_store_runner_token_key = "${var.environment}-runner-token"
npalm marked this conversation as resolved.
Show resolved Hide resolved
secure_parameter_store_runner_sentry_dsn = "${var.environment}-${var.secure_parameter_store_runner_sentry_dsn}"

# Custom names for runner agent instance, security groups, and IAM objects
Expand Down
6 changes: 0 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,6 @@ variable "gitlab_runner_registration_config" {
}
}

variable "secure_parameter_store_runner_token_key" {
description = "The key name used store the Gitlab runner token in Secure Parameter Store"
type = string
default = "runner-token"
}

variable "secure_parameter_store_runner_sentry_dsn" {
description = "The Sentry DSN name used to store the Sentry DSN in Secure Parameter Store"
type = string
Expand Down