Home > Blockchain >  Append Gitlab pipeline ID to version in .Net pipeline
Append Gitlab pipeline ID to version in .Net pipeline

Time:10-22

I am using a Gitlab pipeline to build and publish my application. We specify our version in the AssemblyInfo.Version.cs file:

[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyInformationalVersion("1.0.1")]

I am looking for a way to append the Gitlab pipeline ID to the end of this version string. So, after publishing, the version of the resulting DLL will become:

 1.0.1.7226532

If possible, this should only be done using msbuild command line tool, so no importing of target files or anything.

I already found the environment variable from Gitlab where I can read the pipeline id:

CI_PIPELINE_IID

Now I only need to append this to the existing version number using msbuild.

CodePudding user response:

I'm not sure about msbuild, but since it's just a file you can use sed to perform a search and replace.

Assuming that the version in your example comes from something the pipeline can use a variable for (for example a git tag, CI variable you update each time the version changes, etc.), you can search the AssemblyInfo.Version.cs file for that value, and replace it:

# ...
  script:
    - sed -i "s/${CI_COMMIT_TAG}/${CI_COMMIT_TAG}.${CI_PIPELINE_ID}/g" AssemblyInfo.Version.cs

Breaking down the sed command:

  1. The -i flag says to edit the file in place. Note: on BSD systems, -i works a little differently
  2. The s at the beginning of the argument tells sed we want to do a substitution, then sets the delimiter between arguments to / (you can change this to another character if it's a conflict).
  3. We're searching for this first parameter, ${CI_COMMIT_TAG}
  4. and replacing it with ${CI_COMMIT_TAG}.${CI_PIPELINE_ID}
  5. The /g at the end says to replace all instances of our search string in the file, not just the first
  6. At the end we provide the file we want to search/replace with

By the way, I'd recommend against using the $CI_PIPELINE_IID variable as that is the project-specific ID, opposed to $CI_PIPELINE_ID which is the instance-wide pipeline ID. In some items in GitLab you want to use the IID version, such as Merge Requests, Issues, etc. since the IID is what's displayed in the UI. For example, with an Issue, it has both an instance-wide ISSUE_ID and a project-specific ISSUE_IID. Since the IID is displayed in the UI, users will quickly be able to visually map those two together.

GitLab does the exact opposite with pipelines however: the instance-wide CI_PIPELINE_ID is shown in the UI, not the project-specific CI_PIPELINE_IID.

Using $CI_PIPELINE_IID in your use case could cause some confusion as you might be looking at the AssemblyInfo.Version.cs file, see an ID, but can't find it in the list of Pipelines.

  • Related