Build on GitLab CI

Last update: 16 Aug 2024 [History] [Edit]

Now let’s try something a little more useful, we want the GitLab CI to be able to compile the code you commit and run things if possible.

As was mentioned earlier, GitLab CI relies on Docker images to run the scripts we give it. The default is a basic linux distribution which is why we are able to execute echo "Hello World". Inside your .gitlab-ci.yml file you can use the image keyword to define the docker image you want to be the base of your jobs. The default behaviour for GitLab CI is to pull images from Docker Hub.

For our analysis we would need a linux based image with CMake, Make and knowledge of ATLAS packages. We don’t want to have to commit everything to our Git repository. Fortunately, ATLAS provides docker images of both AthAnalysis and AnalysisBase here.

The first thing we need to do is define the image we want. We will use AnalysisBase for this example and use version 21.2.189 again. However our repository should in theory work with AthAnalysis.

Modify your .gitlab-ci.yml to create another job called build, you can add this in addition to the previous job hello world like the following…

hello world:
    script: echo "Hello World"

build:
  image: atlas/analysisbase:21.2.189
  before_script:
    # setup the analysisbase release
    - source /release_setup.sh
    - pwd
    - ls
  script:
    # echo Hello World after setting up the ATLAS image
    - echo "Hello World"

This script should behave exactly as if you were using the docker container directly. You should be able to navigate and use bash commands as you wish.

Commit and push this to your project and you should (hopefully) see the pipeline completing successfully.

Finally, we can build our code. We would need to create a build directory, run our cmake commands and then build with make.

Note, some initial variables have been set to make sure the git repository can pull your repository and the submodule correctly. Make sure to change the name of your repository to reflect your package name

hello world:
    script: echo "Hello World"

build:
  image: atlas/analysisbase:21.2.189
  variables:
    # Define some pre variables to make life a little easier
    GIT_STRATEGY: fetch
    GIT_SUBMODULE_STRATEGY: recursive
    GIT_SSL_NO_VERIFY: "true"

  before_script:
    # setup the analysisbase release
    - source /release_setup.sh
    - pwd
    - ls
  script:
    # echo Hello World after setting up the ATLAS image
    - echo "Hello World"
    - mkdir ../build
    - cd ../build
    - cmake ../<packagename> # <----- Change this
    - make -j8

Again, stage, commit and push to your remote repository on Git. If everything was done properly you should see your jobs completed and if you read the output, you should see that your analysis code was compiled properly on the Gitlab Runner.

Optional

If you wanted to make the _gitlab-ci.yml a little more sophisticated you could do something like following snippet

hello world:
    script: echo "Hello World"

build:
  image: atlas/analysisbase:21.2.189
  variables:
    # Define some pre variables to make life a little easier
    GIT_STRATEGY: fetch
    GIT_SUBMODULE_STRATEGY: recursive
    GIT_SSL_NO_VERIFY: "true"

    #This may differ depending on your repository name
    SRC_DIR: myxaodanalysis
    BUILD_DIR: build
    SRC_DIR_ABS: "${CI_PROJECT_DIR}"
    BUILD_DIR_ABS: "${CI_PROJECT_DIR}/../${BUILD_DIR}"
  before_script:
    - pwd
    - ls
    # Provide a bit more information to the output
    - echo "Project Directory    ${CI_PROJECT_DIR}"
    - echo "Source Directory     ${SRC_DIR_ABS}"
    - echo "      Directory Name ${SRC_DIR}"
    - echo "Build Directory      ${BUILD_DIR_ABS}"
    - echo "      Directory Name ${BUILD_DIR}"
  script:
    - source /release_setup.sh
    - mkdir ${BUILD_DIR_ABS}
    - cd ${BUILD_DIR_ABS}
    - cmake ${SRC_DIR_ABS}
    - make -j8 

This script may look a little complicated but it is no different to what you have been doing on lxplus or your tier3 compiling your xAOD analysis code. Please read through carefully though to make sure you understand each stage.

If you commit and push this to your project, hopefully things should compile correctly! Success!

[submodule "JetSelectionHelper"]
  path = JetSelectionHelper
  url = https://gitlab.cern.ch/aparker/JetSelectionHelper.git