Our C++ project's existing .gitlab-ci.yml reads simply:

executable:
  stage: build
  tags:
    - C++
    - Boost
  script:
    - gmake -C src -j11
  artifacts:
    name: the executable
    paths:
    - src/executable

This will build on any runner tagged with "C++" and "Boost" -- so far so good. However, we need it built for different OS-versions -- with runners having additional tags like "rhel7", "rhel8", or "freebsd14". The artifacts from such different builds need to be archived separately too.

How is this done?

3 Replies 3

.build_template:
  stage: build
  script:
    - gmake -C src -j11
  artifacts:
    name: "executable-$CI_JOB_NAME-$CI_COMMIT_SHORT_SHA"
    paths:
      - src/executable
    expire_in: 14 days

build:rhel7:
  extends: .build_template
  tags: [ "C++", "Boost", "rhel7" ]

build:rhel8:
  extends: .build_template
  tags: [ "C++", "Boost", "rhel8" ]

build:freebsd14:
  extends: .build_template
  tags: [ "C++", "Boost", "freebsd14" ]
.variables_base:
  variables:
    BUILD_TARGET: executable

.build_template:
  stage: build
  script:
    - gmake -C src -j11
  artifacts:
    name: "${BUILD_TARGET}-${CI_JOB_NAME}-${CI_COMMIT_SHORT_SHA}"
    paths: [ "src/executable" ]

.build_rhel7:
  extends: [ .variables_base, .build_template ]
  tags: [ "C++", "Boost", "rhel7" ]

.build_rhel8:
  extends: [ .variables_base, .build_template ]
  tags: [ "C++", "Boost", "rhel8" ]

.build_freebsd14:
  extends: [ .variables_base, .build_template ]
  tags: [ "C++", "Boost", "freebsd14" ]

You need to modify it most likely as I cant test it for obvious resons. But it should (more less) work

To keep your pipelines nice and DRY this is a perfect time to use gitlabs parallel.matrix:

executable:
  parallel:
    matrix:
     - PLATFORM: [ rhel7, rhel8, freebsd14 ] 
  stage: build
  tags:
    - C++
    - Boost
    - $PLATFORM
  script:
    - gmake -C src -j11
  artifacts:
    name: the executable
    paths:
    - src/executable

To explain: "parallel.matrix" creates named job variants for each combination of the supplied variables so in this case it will generate a pipeline largely equivalent to this ci.yaml:

"executable: [rhel7]":
  variables:
    PLATFORM: rhel7
  ...

"executable: [rhel8]":
  variables:
    PLATFORM: rhel8
  ...
  
"executable: [freebsd14]":
  variables:
    PLATFORM: freebsd14
  stage: build
  tags:
   ...

If you need the artifacts to be unique and gmake doesn't automatically include things like the platform in the name, the variable is injected for your use.

So, each platform has to be explicitly enumerated, eh? I was hoping for builds to automatically happen on all runners of all platforms, that have the "C++" and "Boost" tags...

So that, for example, if someone stands up a freebsd15 and an ubuntu24 runners in the future, I wouldn't need to modify the .gitlab-ci.yml...

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.