Public/Get-BuildVariables.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function Get-BuildVariables {
    <#
    .SYNOPSIS
        Normalize build system variables

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        Normalize build system variables

        Each build system exposes common variables it's own unique way, if at all.
        This function was written to enable more portable builds, and
            to avoid tightly coupling your build scripts with your build system

            Gathers from:
                AppVeyor
                GitLab CI
                Jenkins

            Produces:
                ProjectPath: Project root for cloned repo
                BranchName: git branch for this build
                CommitMessage: git commit message for this build

    .PARAMETER Path
        Path to project root. Defaults to the current working path

    .NOTES
        We assume you are in the project root, for several of the fallback options

    .EXAMPLE
        Get-BuildVariables

    #>

    [cmdletbinding()]
    param(
        $Path = $PWD.Path
    )

$Environment = Get-Item ENV:
$IsGitRepo = Test-Path $( Join-Path $Path .git )

# Find the build folder based on build system
    $BuildRoot = switch ($Environment.Name)
    {
        'APPVEYOR_BUILD_FOLDER' { (Get-Item -Path "ENV:$_").Value; break } # AppVeyor
        'CI_PROJECT_DIR'        { (Get-Item -Path "ENV:$_").Value; break } # GitLab CI
        'WORKSPACE'             { (Get-Item -Path "ENV:$_").Value; break } # Jenkins Jenkins... seems generic.
    }
    if(-not $BuildRoot)
    {
        # Assumption: this function is defined in a file at the root of the build folder
        $BuildRoot = $PSScriptRoot
    }

# Find the git branch
    $BuildBranch = switch ($Environment.Name)
    {
        'APPVEYOR_REPO_BRANCH'  { (Get-Item -Path "ENV:$_").Value; break } # AppVeyor
        'CI_BUILD_REF_NAME'     { (Get-Item -Path "ENV:$_").Value; break } # GitLab CI
        'GIT_BRANCH'            { (Get-Item -Path "ENV:$_").Value; break } # Jenkins
    }
    if(-not $BuildBranch)
    {
        if($IsGitRepo)
        {
            # Using older than 1.6.3 in your build system? Yuck
            # Thanks to earl: http://stackoverflow.com/a/1418022/3067642
            $BuildBranch = git rev-parse --abbrev-ref HEAD
        }
    }

# Find the git commit message
    $CommitMessage = switch ($Environment.Name)
    {
        'APPVEYOR_REPO_COMMIT_MESSAGE' {
            "$env:APPVEYOR_REPO_COMMIT_MESSAGE$env:APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED"
            break
        }
        'CI_BUILD_REF' {
                if($IsGitRepo)
                {
                    git log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )
                    break
                } # Gitlab - thanks to mipadi http://stackoverflow.com/a/3357357/3067642
        }
        'GIT_COMMIT' {
                if($IsGitRepo)
                {
                    git log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )
                    break
                } # Jenkins - thanks to mipadi http://stackoverflow.com/a/3357357/3067642
        }
    }
    if(-not $CommitMessage)
    {
        if($IsGitRepo)
        {
            $CommitMessage = git log --format=%B -n 1
        }
    }

# Determine the build system:
    $BuildSystem = switch ($Environment.Name)
    {
        'APPVEYOR_BUILD_FOLDER' { 'AppVeyor'; break }
        'GITLAB_CI'             { 'GitLab CI' ; break }
        'JENKINS_URL'           { 'Jenkins'; break }
    }
    if(-not $BuildSystem)
    {
        $BuildSystem = 'Unknown'
    }

    [pscustomobject]@{
        BuildSystem = $BuildSystem
        ProjectPath = $BuildRoot
        BranchName = $BuildBranch
        CommitMessage = $CommitMessage
    }
}