Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to access variables from Gitlab CI? #2409

Closed
justmatt25 opened this issue Sep 21, 2020 · 8 comments
Closed

[Question] How to access variables from Gitlab CI? #2409

justmatt25 opened this issue Sep 21, 2020 · 8 comments

Comments

@justmatt25
Copy link

justmatt25 commented Sep 21, 2020

I am building my asp.net core project in Gitlab CI and Docker using Gitversion as an MSBuild task. I can't seem to find the correct environment variable to access the version within the Gitlab pipeline. Any help would be much appreciated :)

This should be tagged as a question but I can't quite figure out how to do that, sorry :(

@asbjornu
Copy link
Member

The variables should be available as GitVersion_<VariableName>. For a list of the specific variables exposed by GitVersion, please refer to the documentation.

@justmatt25
Copy link
Author

justmatt25 commented Sep 21, 2020

Thanks for getting back! Your help is appreciated :)

image

I tried that previously but it didn't seem to find any values there. I would expect the echo to output a variable.

@justmatt25
Copy link
Author

justmatt25 commented Sep 21, 2020

Maybe my Dockerfile is wrong?

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
# Copy project files
WORKDIR /source

COPY ["MiniAppApiServer/MiniAppApiServer.csproj", "./MiniAppApiServer/"]
# Restore
WORKDIR /source

RUN dotnet restore "./MiniAppApiServer/MiniAppApiServer.csproj"
#RUN gitversion /output buildserver
# Copy all source code
COPY . .
# Publish
WORKDIR /source
RUN dotnet publish -c Debug -o /publish
# Runtime
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /publish
COPY --from=build-env /publish .
ENTRYPOINT ["dotnet", "MiniAppApiServer.dll"]

@asbjornu
Copy link
Member

Running gitversion /output buildserver inside a Docker container is not going to expose anything to the outside. You need to read variables from the container and then expose them to the outside yourself if GitVersion is being executed within a container.

@a544jh
Copy link

a544jh commented Dec 21, 2020

I'm having the same problem in GitLab CI. I don't see how the buildserver output is supposed to work on Linux, because child processes can't modify the parent's environment variables.
The automatic build server detection also doesn't seem to be overridable with /output json, which makes this tool a bit annoying to use on Linux. Are there any plans to solve this?

@a544jh
Copy link

a544jh commented Dec 22, 2020

Adding GIT_DEPTH: 0 as a variable to the CI job fixed the issue for me, seems like it couldn't resolve the version otherwise.

@DanielRose
Copy link
Contributor

The way I solved this is based on https://stackoverflow.com/a/59686463/318317

I run the gitversion docker container in my gitlab ci script, and capture the output. In my case, I only want a single variable (FullSemVer), so I directly set the environment variable:

  script:
    - GitVersion_FullSemVer=$(docker run --rm -v "$(pwd):/repo" gittools/gitversion:5.6.10 /repo /output json /showvariable FullSemVer)
    - echo "${GitVersion_FullSemVer}"

As an alternative, if you need more variables, you could save the output to a file and parse it later:

  script:
    - docker run --rm -v "$(pwd):/repo" gittools/gitversion:5.6.10 /repo /output json > version,json
    - cat version.json

@stale
Copy link

stale bot commented Aug 28, 2021

This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 28, 2021
@GitTools GitTools locked and limited conversation to collaborators Feb 7, 2022
@asbjornu asbjornu converted this issue into discussion #2988 Feb 7, 2022
@stale stale bot removed the stale label Feb 7, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants