Dynamic job tags

Hello,

I have been trying to make a job’s tags set dynamicly depending on a variable but to no effect.

I’m writing a .include-nuget-ci.yml that defines two jobs : .buildNuget and .copyNuget. The point of this file is to be included in .gitlab-ci.yml files in many different repos (on a self-managed GitLab). The idea is to have as much of the jobs definitions possible in the .include-nuget-ci.yml file so that the .gitlab-ci.yml jobs just extends from one of these 2 jobs and can stay as short and easy to implement as possible (so setting up GitLab CI on a repo is easy, fast and consistent).

The catch is that the Nuget can be built in configuration Debug or Release and this configuration defines on which runner the job should run. If we build a Nuget in Debug config, it needs to run on the runner that has the “Debug” tag. If no configuration has been explicited, it needs to default to building in Release configuration and run on the runner with the “Release” tag.

So I tried to implement the following :
In the .gitlab-ci.yml, we can define the following job :

MyNuget1:Build:
  extends: .buildNuget
  variables:
    SOLUTION_DIR: "."    # Path to the dir containing the .sln file
    PROJECT_PATH: "MyNuget1"    # Path to the dir containing the .csproj
    MSBUILD_CONFIGURATION: "Debug"    # Optional var. If not here, default to "Release"

And in the .include-nuget-ci.yml, the .buildNuget starts like so :

# Job to build a Nuget
.buildNuget:
  stage: Build
  
  # Map the configuration used to the tags for the gitlab runner
  variables:
    JOB_TAG: ${MSBUILD_CONFIGURATION:-Release}
  
  tags:
    - $JOB_TAG # Run on the gitlab runner corresponding to the specified configuration ; if no configuration defined, default to Release configuration and tag

But this implementation doesn’t seem to work. I get the following warning on GitLab :
This job is stuck because of one of the following problems. There are no active runners online, no runners for the protected branch, or no runners that match all of the job’s tags: ${MSBUILD_CONFIGURATION:-Release}

Maybe this syntax doesn’t work to set a default value ? I have only seen it in a forum.
Maybe the variable JOB_TAG is evaluated before the variable MSBUILD_CONFIGURATION too ?
Maybe the job’s tags are evaluated before its variables ?

If you have any idea on how to fix this warning or a different solution to my problem.

Thanks for your help.

Would you be able to provide further explanation for this particular part? Do you try to combine the CI-Variable with a String, or what is the meaning of :-Release?

I would expect:

variables:
    JOB_TAG: "${MSBUILD_CONFIGURATION}:-Release"

AFAIK this method with the hyphen and string afterwards is used to initialize a variable with a default value, if first variable value is empty.

Something like

  • if $MSBUILD_CONFIGURATION is set to a value, use that.
  • else, use this default value Release

There is a technical term for that but I cannot remember.

To my knowledge, this functionality is not available within the GitLab CI/CD YAML DSL.

2 Likes

We recently changed the way we use our GitLab runners and luckily, I don’t need the tags to be resolved dynamicly anymore.

Regardless, what I wanted to do might just be impossible if a job’s “tags” section is evaluated before its “variables” section.

As for the ${MSBUILD_CONFIGURATION:-Release} part, it’s a method that I have never used previously and have only seen in this : How to set variable with default value at the pipeline level?
I’ve also tried to wrap it in double quotes, but both ways, it did not work.
This method probably just doesn’t work (or at the very least, not inside the “variables” section).

1 Like