Skip to content
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

Save variables as environment variables #2721

Closed
RobSmyth opened this issue Jun 10, 2021 · 6 comments
Closed

Save variables as environment variables #2721

RobSmyth opened this issue Jun 10, 2021 · 6 comments

Comments

@RobSmyth
Copy link

When writing scripts to customise version formatting to our needs, it has been difficult to access GitVersion variables. It would be easier if GitVersion variable(s) could optionally be exposed as environment variables.

Detailed Description

When using a batch file (say) or an MsBuild task (target file) to customise version formatting to our needs we need to read the GitVersion variable from a file (this may be mutually exclusive to using command line output option of buildsystem).

If we could, by configuration, nominate all or some variables to be made available as system variables (current session only) then we could access those in many languages including even (shock) a batch file.

Context

Reduce dev time to use the tool.

Possible Implementation

Another retrospective solution. Done by awful means.

@asbjornu
Copy link
Member

It's not well documented, but GitVersion already exposes all version variables as environment variables in the form GitVersion_<variable_name> such as GitVersion_FullSemVer.

@RobSmyth
Copy link
Author

Thank you, I must be missing something as I tried that without success. I call version.exe as a Visual Studio prebuild event and then call set to list all variables. I do not see any GitVersion_... variables. Same on my box and the build box (TeamCity).

I've not been able to access the variable as %GitVersion_FullSemVer%.

@asbjornu
Copy link
Member

I see. gitversion.exe should set environment variables if it detects that it's running within a supported build server.

itself, that's done by the different build tasks (as how environment variables are set differs from build server to build server). You can see how it's done in AzDO here:

public override string[] GenerateSetParameterMessage(string name, string value)
{
return new[]
{
$"##vso[task.setvariable variable=GitVersion.{name}]{value}",
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
};
}

#2722 seeks to improve the documentation in this area.

@RobSmyth
Copy link
Author

Well I was actually thinking of the host OS environment variables. That looks like a build system's variables/parameters.

@asbjornu
Copy link
Member

asbjornu commented Jun 15, 2021

GitVersion does write OS environment variables, but I don't know AzDO well enough to say for certain that's what the linked code does. I can, however, say for certain that the GitHub Actions integration does it as per the following lines of code:

public const string GitHubSetEnvTempFileEnvironmentVariableName = "GITHUB_ENV";

var gitHubSetEnvFilePath = this.Environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);

writer($"Writing version variables to $GITHUB_ENV file for '{GetType().Name}'.");
using var streamWriter = File.AppendText(gitHubSetEnvFilePath);
foreach (var variable in variables)
{
if (!string.IsNullOrEmpty(variable.Value))
{
streamWriter.WriteLine($"GitVersion_{variable.Key}={variable.Value}");
}
}

In GitHub Actions, evironment variables are written to a file whose path is given in the environment variable GITHUB_ENV.

@stale
Copy link

stale bot commented Mar 2, 2022

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 Mar 2, 2022
@stale stale bot closed this as completed Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants