github.com/pslzym/go-ethereum@v1.8.17-0.20180926104442-4b6824e07b1b/internal/build/env.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package build
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  )
    25  
    26  var (
    27  	// These flags override values in build env.
    28  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    29  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    30  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    31  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    32  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    33  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    34  )
    35  
    36  // Environment contains metadata provided by the build environment.
    37  type Environment struct {
    38  	Name                string // name of the environment
    39  	Repo                string // name of GitHub repo
    40  	Commit, Branch, Tag string // Git info
    41  	Buildnum            string
    42  	IsPullRequest       bool
    43  	IsCronJob           bool
    44  }
    45  
    46  func (env Environment) String() string {
    47  	return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)",
    48  		env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    49  }
    50  
    51  // Env returns metadata about the current CI environment, falling back to LocalEnv
    52  // if not running on CI.
    53  func Env() Environment {
    54  	switch {
    55  	case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
    56  		return Environment{
    57  			Name:          "travis",
    58  			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
    59  			Commit:        os.Getenv("TRAVIS_COMMIT"),
    60  			Branch:        os.Getenv("TRAVIS_BRANCH"),
    61  			Tag:           os.Getenv("TRAVIS_TAG"),
    62  			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
    63  			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
    64  			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
    65  		}
    66  	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
    67  		return Environment{
    68  			Name:          "appveyor",
    69  			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
    70  			Commit:        os.Getenv("APPVEYOR_REPO_COMMIT"),
    71  			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
    72  			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
    73  			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
    74  			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
    75  			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
    76  		}
    77  	default:
    78  		return LocalEnv()
    79  	}
    80  }
    81  
    82  // LocalEnv returns build environment metadata gathered from git.
    83  func LocalEnv() Environment {
    84  	env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
    85  
    86  	head := readGitFile("HEAD")
    87  	if splits := strings.Split(head, " "); len(splits) == 2 {
    88  		head = splits[1]
    89  	} else {
    90  		return env
    91  	}
    92  	if env.Commit == "" {
    93  		env.Commit = readGitFile(head)
    94  	}
    95  	if env.Branch == "" {
    96  		if head != "HEAD" {
    97  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
    98  		}
    99  	}
   100  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
   101  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
   102  	}
   103  	return env
   104  }
   105  
   106  func firstLine(s string) string {
   107  	return strings.Split(s, "\n")[0]
   108  }
   109  
   110  func applyEnvFlags(env Environment) Environment {
   111  	if !flag.Parsed() {
   112  		panic("you need to call flag.Parse before Env or LocalEnv")
   113  	}
   114  	if *GitCommitFlag != "" {
   115  		env.Commit = *GitCommitFlag
   116  	}
   117  	if *GitBranchFlag != "" {
   118  		env.Branch = *GitBranchFlag
   119  	}
   120  	if *GitTagFlag != "" {
   121  		env.Tag = *GitTagFlag
   122  	}
   123  	if *BuildnumFlag != "" {
   124  		env.Buildnum = *BuildnumFlag
   125  	}
   126  	if *PullRequestFlag {
   127  		env.IsPullRequest = true
   128  	}
   129  	if *CronJobFlag {
   130  		env.IsCronJob = true
   131  	}
   132  	return env
   133  }