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

Set default GitBranch from CI env variables #365

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Set default GitBranch from CI env variables
Virtually all CI systems provide environment variables containing the branch from the underlying source control.

This is already in use in:
https://github.com/dotnet/reproducible-builds/pull/
devlooped/oss#2
devlooped/nugetizer#57
devlooped/ThisAssembly#69

We should also bring in the same defaults here. Hopefully this will one day be part of SourceLink.
kzu committed Oct 29, 2024
commit 19b99bf9e7e3500892f2a8cecef4d8ce77c2d320
36 changes: 36 additions & 0 deletions src/GitInfo/build/GitInfo.CI.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup Condition="'$(GitBranch)' == ''">
<!-- GitHub Actions: https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(GITHUB_REF)' != '' and $(GITHUB_REF.Contains('refs/pull/'))">pr$(GITHUB_REF.Replace('refs/pull/', '').Replace('/merge', ''))</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(GITHUB_REF)' != ''">$(GITHUB_REF.Replace('refs/heads/', '').Replace('refs/tags/', ''))</GitBranch>
<!-- Azure DevOps: https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(BUILD_SOURCEBRANCH)' != ''">$(BUILD_SOURCEBRANCH.Replace('refs/heads/', '').Replace('refs/tags/', ''))</GitBranch>
<!-- AppVeyor: https://www.appveyor.com/docs/environment-variables/ -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(APPVEYOR_PULL_REQUEST_NUMBER)' != ''">pr$(APPVEYOR_PULL_REQUEST_NUMBER)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(APPVEYOR_REPO_TAG_NAME)' != ''">$(APPVEYOR_REPO_TAG_NAME)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(APPVEYOR_REPO_BRANCH)' != ''">$(APPVEYOR_REPO_BRANCH)</GitBranch>
<!-- TeamCity: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Branch-Related+Parameters -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(TEAMCITY_BUILD_BRANCH)' != ''">$(TEAMCITY_BUILD_BRANCH)</GitBranch>
<!--TravisCI: https://docs.travis-ci.com/user/environment-variables/ -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(TRAVIS_PULL_REQUEST)' != '' and '$(TRAVIS_PULL_REQUEST)' != 'false'">pr$(TRAVIS_PULL_REQUEST)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(TRAVIS_BRANCH)' != ''">$(TRAVIS_BRANCH)</GitBranch>
<!-- CircleCI: https://circleci.com/docs/2.0/env-vars/ -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(CIRCLE_PR_NUMBER)' != ''">pr$(CIRCLE_PR_NUMBER)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(CIRCLE_TAG)' != ''">$(CIRCLE_TAG)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(CIRCLE_BRANCH)' != ''">$(CIRCLE_BRANCH)</GitBranch>
<!-- GitLab: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(CI_COMMIT_TAG)' != ''">$(CI_COMMIT_TAG)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(CI_MERGE_REQUEST_IID)' != ''">pr$(CI_MERGE_REQUEST_IID)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(CI_EXTERNAL_PULL_REQUEST_IID)' != ''">pr$(CI_EXTERNAL_PULL_REQUEST_IID)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(CI_COMMIT_BRANCH)' != ''">$(CI_COMMIT_BRANCH)</GitBranch>
<!-- Buddy: https://buddy.works/docs/pipelines/environment-variables#default-environment-variables -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(BUDDY_EXECUTION_PULL_REQUEST_NO)' != ''">pr$(BUDDY_EXECUTION_PULL_REQUEST_NO)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(BUDDY_EXECUTION_TAG)' != ''">$(BUDDY_EXECUTION_TAG)</GitBranch>
<GitBranch Condition="'$(GitBranch)' == '' and '$(BUDDY_EXECUTION_BRANCH)' != ''">$(BUDDY_EXECUTION_BRANCH)</GitBranch>
<!-- Jenkins: https://plugins.jenkins.io/git/#plugin-content-environment-variables -->
<GitBranch Condition="'$(GitBranch)' == '' and '$(GIT_LOCAL_BRANCH)' != ''">$(GIT_LOCAL_BRANCH)</GitBranch>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions src/GitInfo/build/GitInfo.targets
Original file line number Diff line number Diff line change
@@ -104,6 +104,20 @@
<_GitIsDirtyFile>$(GitCachePath)GitIsDirty.cache</_GitIsDirtyFile>
</PropertyGroup>

<PropertyGroup Label="CI" Condition="'$(CI)' == ''">
<CI>false</CI>
<!-- GH, CircleCI, GitLab and BitBucket already use CI -->
<CI Condition="'$(TF_BUILD)' == 'true' or
'$(TEAMCITY_VERSION)' != '' or
'$(APPVEYOR)' != '' or
'$(BuildRunner)' == 'MyGet' or
'$(JENKINS_URL)' != '' or
'$(TRAVIS)' == 'true' or
'$(BUDDY)' == 'true'">true</CI>
</PropertyGroup>

<Import Project="GitInfo.CI.targets" Condition="$(CI)" />

<Target Name="GitInfoReport" DependsOnTargets="GitVersion">
<Message Importance="$(GitInfoReportImportance)" Text="Git Info:
GitInfoBaseDir: $(GitInfoBaseDir)