Gitlab CI Tutorial
Gitlab CI Tutorial
Gitlab Project
Including Creating a Runner
Duncan C. White,
[email protected]
Dept of Computing,
Imperial College London
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 1 / 15
What is Gitlab-CI?
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 2 / 15
What is Gitlab-CI?
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 2 / 15
What is Gitlab-CI?
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 2 / 15
What is Gitlab-CI?
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 2 / 15
Setting up the .gitlab-ci.yml file
In Gitlab, click again on the Project button and you should see
the results of the first commit. On Project Settings, check that
the Builds feature is enabled, and, further down, that your
project has a CI Token. You’ll need to copy this token later.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 3 / 15
Setting up the .gitlab-ci.yml file
In Gitlab, click again on the Project button and you should see
the results of the first commit. On Project Settings, check that
the Builds feature is enabled, and, further down, that your
project has a CI Token. You’ll need to copy this token later.
In your new mini-list-flattening repo directory, look around and
see what the code does. In particular, read the README and do
what it says to compile and run the test program.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 3 / 15
Setting up the .gitlab-ci.yml file
In Gitlab, click again on the Project button and you should see
the results of the first commit. On Project Settings, check that
the Builds feature is enabled, and, further down, that your
project has a CI Token. You’ll need to copy this token later.
In your new mini-list-flattening repo directory, look around and
see what the code does. In particular, read the README and do
what it says to compile and run the test program.
No Gitlab project will use CI unless you set up a YAML file called
.gitlab-ci.yml defining the actions to run. You will spend a lot of
time editing/committing/pushing this file, until it works.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 3 / 15
Setting up the .gitlab-ci.yml file
In Gitlab, click again on the Project button and you should see
the results of the first commit. On Project Settings, check that
the Builds feature is enabled, and, further down, that your
project has a CI Token. You’ll need to copy this token later.
In your new mini-list-flattening repo directory, look around and
see what the code does. In particular, read the README and do
what it says to compile and run the test program.
No Gitlab project will use CI unless you set up a YAML file called
.gitlab-ci.yml defining the actions to run. You will spend a lot of
time editing/committing/pushing this file, until it works.
After a lot of failures, the first roughly correct version read:
before_script:
- sudo apt-get update -qq && sudo apt-get install -y -qq gcc make
- which gcc
runtests:
script:
# compile it up
- export TOOLDIR=$HOME/c-tools
- export ARCH=x86_64
- make
# and run the tests..
- make test
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 3 / 15
Setting up the .gitlab-ci.yml file
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 4 / 15
Setting up the .gitlab-ci.yml file
When you push this file up to your remote repo on Gitlab, Gitlab
will automatically enable CI facilities on the project.
In the Gitlab UI, there’s a Builds menu item, click on it and you
will see that it attempted to run a build, initially this will be
marked as Pending.
Click on the Pending Build and you’ll see that it’s Pending
because you haven’t yet created and registered a Runner.
A Runner is a special test machine, belonging to you and
running special software, that Gitlab-CI will use to run your
actions. We’ll see how to set up and customize the Runner later
in this tutorial. In particular, we’ll need to ensure that the
$HOME/c-tools directory exists with the right contents.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 5 / 15
Setting up the .gitlab-ci.yml file
When you push this file up to your remote repo on Gitlab, Gitlab
will automatically enable CI facilities on the project.
In the Gitlab UI, there’s a Builds menu item, click on it and you
will see that it attempted to run a build, initially this will be
marked as Pending.
Click on the Pending Build and you’ll see that it’s Pending
because you haven’t yet created and registered a Runner.
A Runner is a special test machine, belonging to you and
running special software, that Gitlab-CI will use to run your
actions. We’ll see how to set up and customize the Runner later
in this tutorial. In particular, we’ll need to ensure that the
$HOME/c-tools directory exists with the right contents.
Note: If you see the Build marked as Failed, not Pending, at this
stage, it probably means there are syntax errors in the YML file -
these are displayed on the Build page. Fix the YML file, commit
it and push it up and check the Build status again.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 5 / 15
Creating the Runner VM
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 6 / 15
Creating the Runner VM
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 7 / 15
Creating the Runner VM
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 8 / 15
Creating the Runner VM
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 8 / 15
After Runner Registration.. Build status changes
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 9 / 15
After Runner Registration.. Build status changes
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 9 / 15
After Runner Registration.. Build status changes
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project1st Feb 2016 9 / 15
Retry the Build
So, to allow our runner to use sudo, add the gitlab-runner user
to the sudo group, in the root ssh session:
usermod -G sudo gitlab-runner
Now, click Retry Build. If you got it right, you should see that
the runner VM installs gcc and make, as the before script
section told it to.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 10 / 15
Retry the Build
So, to allow our runner to use sudo, add the gitlab-runner user
to the sudo group, in the root ssh session:
usermod -G sudo gitlab-runner
Now, click Retry Build. If you got it right, you should see that
the runner VM installs gcc and make, as the before script
section told it to.
Of course, every change made to the runner VM - either by the
before script section, or done manually as root on the VM,
persists forever. Hence, once we’ve successfully installed gcc and
make, we probably don’t want to leave the apt-get commands
live, because they run every time and slow things down. So
comment most of the before script section out (by another vi;
commit and push sequence):
before_script:
#- whoami
#- sudo apt-get update -qq && sudo apt-get install -y -qq gcc make
- which gcc
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 11 / 15
The Build attempts runtests
When I did this, the Build process cd’d into the correct directory,
set the above environment variables, and then ran make.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 11 / 15
The Build attempts runtests
When I did this, the Build process cd’d into the correct directory,
set the above environment variables, and then ran make.
At this point, of course, $HOME/c-tools did not exist on the
runner VM, so the make failed to find mem.h anywhere.
We could add rules to the before script section to fetch a
c-tools tarball from somewhere and extract it, but it’s simpler to
do this from the root ssh session.
First, as you on a DoC workstation, build a c-tools.tgz tarball
containing your ~/c-tools directory:
cd
tar czf /tmp/c-tools.tgz c-tools
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 11 / 15
Making persistent changes to images
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 12 / 15
Making persistent changes to images
Now, click Retry Build - you should see that the runner VM
successfully compiles and links testmld, and then runs it,
producing the output:
basic members: { a,b,c,d,e,ldk,dcw,gnb, }
lists initially:
Build succeeded.
Note that the Build process fetches the output from the runner
and presents it to us unaltered, recording it for posterity.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 12 / 15
Our First Successful Build
Our Build is now - for the first time - successful. Clicking back
on Gitlab’s Builds menu item shows the whole Build history,
many failures plus one successful build.
Now, every time you make any change to your repo, and push it
up to Gitlab, another Build will automatically happen, and a few
seconds later Gitlab’s Builds section will show whether the new
version ran the tests successfully - try this a few times.
You may well want to invest a little time wrapping your test runs
in some small test harness that summarises (as Perl’s prove
does) your test runs, in order to make it clearer how many tests
there were, and how many failed.
Some languages will have their own test framework you should
use, but in our case you will notice that the output comprises
informational messages interleaved with test success/failure
messages, the latter marked with a “T ” prefix.
As a first step, change the Makefile test invocation to:
test: testmld
./testmld | grep ’^T ’
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 13 / 15
Our First Successful Build
Then git commit Makefile and git push the change up. In a
few seconds, the latest Build output will only show:
T allbasic( one: b,c, nonbasic two, ): is false: ok
T allbasic( two: a,d,ldk,dcw, ): is true: ok
T allbasic( three: e, nonbasic two,one, ): is false: ok
T allbasic( one: a,b,c,d,ldk,dcw, ): is true: ok
T allbasic( two: a,d,ldk,dcw, ): is true: ok
T allbasic( three: e, nonbasic two,one, ): is false: ok
T allbasic( one: a,b,c,d,ldk,dcw, ): is true: ok
T allbasic( two: a,d,ldk,dcw, ): is true: ok
T allbasic( three: a,d,e,ldk,dcw, nonbasic one, ): is false: ok
T allbasic( one: a,b,c,d,ldk,dcw, ): is true: ok
T allbasic( two: a,d,ldk,dcw, ): is true: ok
T allbasic( three: a,b,c,d,e,ldk,dcw, ): is true: ok
Build succeeded.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 15 / 15
Conclusion
Build succeeded.
That’s enough for now. In these notes, you’ve seen how to set up
a fresh Gitlab project repository to use Gitlab-CI to do
automatic testing.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 15 / 15
Conclusion
Build succeeded.
That’s enough for now. In these notes, you’ve seen how to set up
a fresh Gitlab project repository to use Gitlab-CI to do
automatic testing.
Note that gitlab-ci-multi-runner can be used for testing several
of your Gitlab projects. Set up Gitlab-CI for a second Gitlab
project repo (as before) and then, on your existing runner VM,
just rerun the registration - using the second project’s CI Token.
Duncan White (Imperial) Gitlab-CI: Setting up Continuous Integration for a Gitlab Project
1st Feb 2016 15 / 15