This post was originally published on the 56k.cloud blog.
Gitlab.com allows its users to run their own, self-hosted, Gitlab Runners as part of their Gitlab CI feature. This comes in handy when the CI jobs have to run in a user-owned environment or more powerful runners are needed. If that environment is AWS and you are looking for an automated way of setting up the Runner(s) on EC2, then stay tuned for a Hostatic recipe!
git clone https://github.com/danakim/packer.git
cd packer/gitlab-runner packer build -var 'aws_access_key=<your_access_key_id>' -var 'aws_secret_key=<your_access_key>' gitlab-runner.json
User datasection of EC2’s instance launch options to register the runner with gitlab.com:
/home/ubuntu/gitlab-runner-register.sh <environment (e.g.: dev)> <gitlab_cicd_token>
Enable for this projectto make the runner usable by the repository.
At this point, the Gitlab runner is attached to the project. When defining the Gitlab CI / CD pipeline in
.gitlab-ci.yml each job will have to use a
tag to run that job on the new Gitlab runner. This
tag matches the environment passed to the
gitlab-runner-register.sh script above. Example:
job: script: - echo "Hello world!" tags: - <environment (e.g. dev)>
Now that we have a reusable image (AMI) for a self-hosted Gitlab runner, it can be used as a building block for further automation.
One possible next improvement is to use this AMI and the User Data script in an infrastructure-as-code DSL like Terraform or Cloudformation. But that’s a subject for a future blog post!