'How to get project version in gitlab-ci.yml file from the .net project file?

I am converting my project from Jenkins to GitLab CI. There is a .sh file which I am executing from .gitlab-ci.yml file where I am extracting the version from the project file using following statement:

VERSION=$(grep -oPm1 "(?<=)[^<]+" /Service.csproj

I am getting the project version and this is working fine. How can I run the above statement in .gitlab-ci.yml file and assign the version value to a variable?
I tried running statement but I am getting error like invalid option "P"



Solution 1:[1]

Somehow above grep didn't work for me. Below works for me.

$VERSION=[regex]::match((Get-Content .\AssemblyFileVersion.cs), "AssemblyFileVersion\(`"(.*)`"\)").Groups[1].Value'

Solution 2:[2]

I use in my example the sed command

stages:
  - test

test:
  stage: test
  script:
    - VERSION=$(sed -n "s/<Version>\(.*\)<\/Version>/\1/p" Service.csproj)
    - echo $VERSION

Solution 3:[3]

You need to execute the command in a script block, so your gitlab-ci.yml could look like this:

stages:
  - test

test:
  stage: test
  script:
    - VERSION=$(grep -oPm1 "(?<=)[^<]+" Service.csproj | xargs)
    - echo $VERSION

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rahul Techie
Solution 2 live2
Solution 3 live2