Skip to content

Support for Dotenv output format #4413

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

Merged
merged 5 commits into from
Mar 11, 2025
Merged

Conversation

Bi0T1N
Copy link
Contributor

@Bi0T1N Bi0T1N commented Feb 8, 2025

Description

This PR implements the functionality to export the GitVersion variables in the Dotenv format that can be consumed by your environment.

Related Issue

Closes #4174

Motivation and Context

Exporting the variables in the Dotenv format provides a simple mechanism to use the GitVersion variables in your shell environment. Additonallty some CI systems like GitHub or GitLab allow to pass around and expanding the jobs environment with artifacts in the Dotenv format.

It can be discussed how the output should look like e.g. wrap all values in '<value>', write nothing for empty lines or use '' (that's what I've decided for). The parsing rules for nodejs projects are here while the ones for GitLab CI can be found here.

How Has This Been Tested?

With the provided unit tests as well as with a Python script and a library for handling Dotenv files (see Examples section below).

Examples:

Running the following commands

git init --initial-branch=main
touch text.txt
git add test.txt 
git commit -m "test"
gitversion /output dotenv > gitversion.env

results in a gitversion.env file with the following content

GitVersion_AssemblySemFileVer=0.0.1.0
GitVersion_AssemblySemVer=0.0.1.0
GitVersion_BranchName=main
GitVersion_BuildMetaData=''
GitVersion_CommitDate=2025-02-08
GitVersion_CommitsSinceVersionSource=1
GitVersion_EscapedBranchName=main
GitVersion_FullBuildMetaData=Branch.main.Sha.2501b1440f9823abca9f9100f3776e42a3d730c3
GitVersion_FullSemVer=0.0.1-1
GitVersion_InformationalVersion=0.0.1-1+Branch.main.Sha.2501b1440f9823abca9f9100f3776e42a3d730c3
GitVersion_Major=0
GitVersion_MajorMinorPatch=0.0.1
GitVersion_Minor=0
GitVersion_Patch=1
GitVersion_PreReleaseLabel=''
GitVersion_PreReleaseLabelWithDash=''
GitVersion_PreReleaseNumber=1
GitVersion_PreReleaseTag=1
GitVersion_PreReleaseTagWithDash=-1
GitVersion_SemVer=0.0.1-1
GitVersion_Sha=2501b1440f9823abca9f9100f3776e42a3d730c3
GitVersion_ShortSha=2501b14
GitVersion_UncommittedChanges=1519
GitVersion_VersionSourceSha=''
GitVersion_WeightedPreReleaseNumber=55001

Loading that file in Python (pip install python-dotenv) with the following script

import dotenv
import os

dotenv_file = dotenv.find_dotenv('gitversion.env')
dotenv.load_dotenv(dotenv_file)

print("-- output loaded values as example --")
print(os.environ["GitVersion_MajorMinorPatch"])  # 0.0.1
print(os.environ['GitVersion_VersionSourceSha'])  # empty

print("-- manipulate a loaded value --")
os.environ["GitVersion_MajorMinorPatch"] = ""
print(os.environ['GitVersion_MajorMinorPatch'])  # empty

print("-- write changes to env file --")
dotenv.set_key("python_gitversion.env", "GitVersion_Major", os.environ["GitVersion_Major"])
dotenv.set_key("python_gitversion.env", "GitVersion_FullSemVer", os.environ["GitVersion_FullSemVer"])
dotenv.set_key("python_gitversion.env", "GitVersion_MajorMinorPatch", os.environ["GitVersion_MajorMinorPatch"])
dotenv.set_key("python_gitversion.env", "GitVersion_VersionSourceSha", os.environ["GitVersion_VersionSourceSha"])

writes

-- output loaded values as example --
0.0.1

-- manipulate a loaded value --

-- write changes to env file --

to the console (and thus proves it can load/understand the generated Dotenv format from GitVersion) and results in a python_gitversion.env file with the content

GitVersion_MajorMinorPatch=''
GitVersion_VersionSourceSha=''
GitVersion_Major='0'
GitVersion_FullSemVer='0.0.1-1'

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Sorry, something went wrong.

@arturcic arturcic self-requested a review February 8, 2025 13:42
@arturcic
Copy link
Member

@asbjornu we probably should rebase this branch onto main instead of merge main into this branch

@asbjornu
Copy link
Member

@asbjornu we probably should rebase this branch onto main instead of merge main into this branch

Indeed. I wish that was possible in the GitHub UI!

@Bi0T1N Bi0T1N force-pushed the dotenv_support branch 3 times, most recently from c02db41 to 8f34dbd Compare February 12, 2025 15:57
@Bi0T1N
Copy link
Contributor Author

Bi0T1N commented Feb 12, 2025

I rebased my changes on top of main and thus also removed the merge ;-)

@Bi0T1N
Copy link
Contributor Author

Bi0T1N commented Feb 12, 2025

I assume the Windows tests fail because of the \n at the end of output.ShouldContain("GitVersion_" + variableName + "=" + expectedValue + "\n");.
Can someone with access to a Windows machine confirm this please? The fix would probably be Environment.NewLine.

@arturcic
Copy link
Member

@Bi0T1N I will take this one for a spin before merging

@arturcic
Copy link
Member

@Bi0T1N please rebase this one on main (no merge)

@Bi0T1N
Copy link
Contributor Author

Bi0T1N commented Mar 7, 2025

I decided to wrap all values into single quotes thus it should be more future proof as it might not break
on values that contain whitespaces, etc.
Output looks like that now:

GitVersion_AssemblySemFileVer='0.0.1.0'
GitVersion_AssemblySemVer='0.0.1.0'
GitVersion_BranchName='main'
GitVersion_BuildMetaData=''
GitVersion_CommitDate='2025-03-07'
GitVersion_CommitsSinceVersionSource='1'
GitVersion_EscapedBranchName='main'
GitVersion_FullBuildMetaData='Branch.main.Sha.8d1cb464bbb0ee504774b6f6ef61d2c0dc419af8'
GitVersion_FullSemVer='0.0.1-1'
GitVersion_InformationalVersion='0.0.1-1+Branch.main.Sha.8d1cb464bbb0ee504774b6f6ef61d2c0dc419af8'
GitVersion_Major='0'
GitVersion_MajorMinorPatch='0.0.1'
GitVersion_Minor='0'
GitVersion_Patch='1'
GitVersion_PreReleaseLabel=''
GitVersion_PreReleaseLabelWithDash=''
GitVersion_PreReleaseNumber='1'
GitVersion_PreReleaseTag='1'
GitVersion_PreReleaseTagWithDash='-1'
GitVersion_SemVer='0.0.1-1'
GitVersion_Sha='8d1cb464bbb0ee504774b6f6ef61d2c0dc419af8'
GitVersion_ShortSha='8d1cb46'
GitVersion_UncommittedChanges='0'
GitVersion_VersionSourceSha=''
GitVersion_WeightedPreReleaseNumber='55001'

The python_gitversion.env file that is generated by the script from above still contains the same content:

GitVersion_Major='0'
GitVersion_FullSemVer='0.0.1-1'
GitVersion_MajorMinorPatch=''
GitVersion_VersionSourceSha=''

@arturcic
Copy link
Member

arturcic commented Mar 7, 2025

Next week I have a bit of time, will review the PR

@arturcic
Copy link
Member

@mergify rebase

Copy link
Contributor

mergify bot commented Mar 11, 2025

rebase

✅ Nothing to do for rebase action

Copy link
Member

@arturcic arturcic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!, Great job

@arturcic arturcic merged commit e2e6b60 into GitTools:main Mar 11, 2025
96 checks passed
Copy link
Contributor

mergify bot commented Mar 11, 2025

Thank you @Bi0T1N for your contribution!

@Bi0T1N Bi0T1N deleted the dotenv_support branch March 14, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI: New dotenv output format
3 participants