Check installation by executing gitlab-runner status. A successful response should look like the following:
1
2
Runtime platform arch=amd64 os=linux pid=12090revision=8fa89735 version=13.6.0
gitlab-runner: Service is running!
Register gitlab-runner
You can register gitlab-runner by executing the following command.
1
gitlab-runner register
You will be required to enter 4 inputs when prompted:
gitlab website: If you are hosting your own gitlab server, you need enter its address. Since I am using gitlab.com, I will enter https://gitlab.com/.
registration token: You can your project’s registration token in project’s Settings -> CI/CD -> Runners -> Set up a specific Runner manually.
tags: You can create a tag for this particular gitlab-runner, which you can later use in .gitlab-ci.yml to address/use this instance of gitlab-runner. In case you have more than one instance of gitlab-runner with different configurations and resources, tags will help you address the instance you want to use for a certain job.
executer: Here you select one of the options (docker-ssh, ssh, docker+machine, docker-ssh+machine, kubernetes, custom, docker, parallels, shell, virtualbox) to execute your jobs. You can read more about these options here. For the purposes of testing, I will select shell as executor.
Once the registration of gitlab-runner completes. You will see the following section appear in Settings -> CI/CD -> Runners -> Set up a specific Runner manually.
.gitlab-ci.yml examples
Example #1
Following is the Bash sample yml file provided by gitlab. Lets see analyse it before modifying it.
# This file is a template, and might need editing before it works on your project.# see https://docs.gitlab.com/ee/ci/yaml/README.html for all available options# you can delete this line if you're not using Dockerimage:busybox:latestbefore_script:- echo "Before script section"- echo "For example you might run an update here or install a build dependency"- echo "Or perhaps you might print out some debugging details"after_script:- echo "After script section"- echo "For example you might do some cleanup here"build1:stage:buildscript:- echo "Do your build here"test1:stage:testscript:- echo "Do a test here"- echo "For example run a test suite"test2:stage:testscript:- echo "Do another parallel test here"- echo "For example run a lint test"deploy1:stage:deployscript:- echo "Do your deploy here"
Preparing the "docker+machine" executor
00:13
Using Docker executor with image busybox:latest ...
Pulling docker image busybox:latest ...
Using docker image sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843 for busybox:latest with digest busybox@sha256:9f1c79411e054199210b4d489ae600a061595967adb643cd923f8515ad8123d2 ...
Preparing environment
00:01
Running on runner-ed2dce3a-project-22692563-concurrent-0 via runner-ed2dce3a-srm-1606320958-f3e4e349...
Getting source from Git repository
00:01
$ eval"$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/khan990/experiment-gitlab-runner/.git/
Created fresh repository.
Checking out c8ad39b1 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:01
$ echo"Before script section"
Before script section
$ echo"For example you might run an update here or install a build dependency"
For example you might run an update here or install a build dependency
$ echo"Or perhaps you might print out some debugging details"
Or perhaps you might print out some debugging details
$ echo"Do your build here"
Do your build here
Running after_script
00:01
Running after script...
$ echo"After script section"
After script section
$ echo"For example you might do some cleanup here"
For example you might do some cleanup here
Cleaning up file based variables
00:01
Job succeeded
Note, that the very first line shows that this build is not executed on our gitlab-runner, but it ran on one of the common Shared runners that you can see in Settings -> CI/CD -> Runners -> Set up a specific Runner manually.
To run it in our own runner, lets first disable shared runner.
To run jobs using our runner, we have two options.
Open up runner options in Settings -> CI/CD -> Runners -> Set up a specific Runner manually. Check Run untagged jobs and save.
Mention runner tag in .gitlab-ci.yml file like the following:
1
2
3
4
default:tags:- sample#...
Follow one of the options and then restart the job to check if runner works. Which it does, but fails, because we choose a shell runner, but our yml requires docker machine. Remove image property from ci. Resulting .gitlab-ci.yml will look like the following:
default:tags:- samplebefore_script:- echo "Before script section"- echo "For example you might run an update here or install a build dependency"- echo "Or perhaps you might print out some debugging details"after_script:- echo "After script section"- echo "For example you might do some cleanup here"build1:stage:buildscript:- echo "Do your build here"test1:stage:testscript:- echo "Do a test here"- echo "For example run a test suite"test2:stage:testscript:- echo "Do another parallel test here"- echo "For example run a lint test"deploy1:stage:deployscript:- echo "Do your deploy here"
Info
On your ubuntu(gitlab-runner host) machine, you can also execute gitlab-runner --debug run to verbose log of gitlab-runner.
Example #2
Lets invent a scenario, where we would like to do the following:
We have following stages:
Build
Test
Push
Tag(docker)
Deploy
Release
And following jobs:
build
test:lint
test:unit
test:e2e
push(docker)
tag(docker)
release(git version)
deploy:develop
deploy:staging
deploy:live
Scenarios (WIP)
Warning
This section is work in progress
WHEN:
Code is pushed to a feature branch, no pipeline runs.
Merge request is created from feature branch(feature/*) to develop(develop), pipeline runs with following jobs:
build
test:lint
test:unit
push(docker)
deploy:develop
Code is pushed to develop:
build
test:lint
test:unit
test:e2e
push(docker)
deploy:develop
Merge request is created from develop(develop) to master(master), pipeline is skipped.
bug
Above requirement is still not fulfilled.
Merge request of develop(develop) to master(master) is accepted.
Комментарии