aboutsummaryrefslogtreecommitdiffstats
path: root/internal/build/env.go
blob: b7aa0f1228eb94549a3a46d84c60359281f753fb (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package build

import (
    "flag"
    "fmt"
    "os"
    "strconv"
    "strings"
    "time"
)

var (
    // These flags override values in build env.
    GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
)

// Environment contains metadata provided by the build environment.
type Environment struct {
    Name                      string // name of the environment
    Repo                      string // name of GitHub repo
    Commit, Date, Branch, Tag string // Git info
    Buildnum                  string
    IsPullRequest             bool
    IsCronJob                 bool
}

func (env Environment) String() string {
    return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
        env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
}

// Env returns metadata about the current CI environment, falling back to LocalEnv
// if not running on CI.
func Env() Environment {
    switch {
    case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
        commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
        if commit == "" {
            os.Getenv("TRAVIS_COMMIT")
        }
        return Environment{
            Name:          "travis",
            Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
            Commit:        commit,
            Date:          getDate(commit),
            Branch:        os.Getenv("TRAVIS_BRANCH"),
            Tag:           os.Getenv("TRAVIS_TAG"),
            Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
            IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
            IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
        }
    case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
        commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
        if commit == "" {
            os.Getenv("APPVEYOR_REPO_COMMIT")
        }
        return Environment{
            Name:          "appveyor",
            Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
            Commit:        commit,
            Date:          getDate(commit),
            Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
            Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
            Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
            IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
            IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
        }
    default:
        return LocalEnv()
    }
}

// LocalEnv returns build environment metadata gathered from git.
func LocalEnv() Environment {
    env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})

    head := readGitFile("HEAD")
    if fields := strings.Fields(head); len(fields) == 2 {
        head = fields[1]
    } else {
        return env
    }
    if env.Commit == "" {
        env.Commit = readGitFile(head)
    }
    env.Date = getDate(env.Commit)
    if env.Branch == "" {
        if head != "HEAD" {
            env.Branch = strings.TrimPrefix(head, "refs/heads/")
        }
    }
    if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
        env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
    }
    return env
}

func firstLine(s string) string {
    return strings.Split(s, "\n")[0]
}

func getDate(commit string) string {
    if commit == "" {
        return ""
    }
    out := RunGit("show", "-s", "--format=%ct", commit)
    if out == "" {
        return ""
    }
    date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
    if err != nil {
        panic(fmt.Sprintf("failed to parse git commit date: %v", err))
    }
    return time.Unix(date, 0).Format("20060102")
}

func applyEnvFlags(env Environment) Environment {
    if !flag.Parsed() {
        panic("you need to call flag.Parse before Env or LocalEnv")
    }
    if *GitCommitFlag != "" {
        env.Commit = *GitCommitFlag
    }
    if *GitBranchFlag != "" {
        env.Branch = *GitBranchFlag
    }
    if *GitTagFlag != "" {
        env.Tag = *GitTagFlag
    }
    if *BuildnumFlag != "" {
        env.Buildnum = *BuildnumFlag
    }
    if *PullRequestFlag {
        env.IsPullRequest = true
    }
    if *CronJobFlag {
        env.IsCronJob = true
    }
    return env
}