github.com/bloxroute-labs/bor@v0.1.4/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  	"strconv"
    24  	"strings"
    25  	"time"
    26  )
    27  
    28  var (
    29  	// These flags override values in build env.
    30  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    31  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    32  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    33  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    34  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    35  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    36  )
    37  
    38  // Environment contains metadata provided by the build environment.
    39  type Environment struct {
    40  	Name                      string // name of the environment
    41  	Repo                      string // name of GitHub repo
    42  	Commit, Date, Branch, Tag string // Git info
    43  	Buildnum                  string
    44  	IsPullRequest             bool
    45  	IsCronJob                 bool
    46  }
    47  
    48  func (env Environment) String() string {
    49  	return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
    50  		env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    51  }
    52  
    53  // Env returns metadata about the current CI environment, falling back to LocalEnv
    54  // if not running on CI.
    55  func Env() Environment {
    56  	switch {
    57  	case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
    58  		commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
    59  		if commit == "" {
    60  			commit = os.Getenv("TRAVIS_COMMIT")
    61  		}
    62  		return Environment{
    63  			Name:          "travis",
    64  			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
    65  			Commit:        commit,
    66  			Date:          getDate(commit),
    67  			Branch:        os.Getenv("TRAVIS_BRANCH"),
    68  			Tag:           os.Getenv("TRAVIS_TAG"),
    69  			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
    70  			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
    71  			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
    72  		}
    73  	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
    74  		commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
    75  		if commit == "" {
    76  			commit = os.Getenv("APPVEYOR_REPO_COMMIT")
    77  		}
    78  		return Environment{
    79  			Name:          "appveyor",
    80  			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
    81  			Commit:        commit,
    82  			Date:          getDate(commit),
    83  			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
    84  			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
    85  			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
    86  			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
    87  			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
    88  		}
    89  	default:
    90  		return LocalEnv()
    91  	}
    92  }
    93  
    94  // LocalEnv returns build environment metadata gathered from git.
    95  func LocalEnv() Environment {
    96  	env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
    97  
    98  	head := readGitFile("HEAD")
    99  	if fields := strings.Fields(head); len(fields) == 2 {
   100  		head = fields[1]
   101  	} else {
   102  		return env
   103  	}
   104  	if env.Commit == "" {
   105  		env.Commit = readGitFile(head)
   106  	}
   107  	env.Date = getDate(env.Commit)
   108  	if env.Branch == "" {
   109  		if head != "HEAD" {
   110  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
   111  		}
   112  	}
   113  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
   114  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
   115  	}
   116  	return env
   117  }
   118  
   119  func firstLine(s string) string {
   120  	return strings.Split(s, "\n")[0]
   121  }
   122  
   123  func getDate(commit string) string {
   124  	if commit == "" {
   125  		return ""
   126  	}
   127  	out := RunGit("show", "-s", "--format=%ct", commit)
   128  	if out == "" {
   129  		return ""
   130  	}
   131  	date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
   132  	if err != nil {
   133  		panic(fmt.Sprintf("failed to parse git commit date: %v", err))
   134  	}
   135  	return time.Unix(date, 0).Format("20060102")
   136  }
   137  
   138  func applyEnvFlags(env Environment) Environment {
   139  	if !flag.Parsed() {
   140  		panic("you need to call flag.Parse before Env or LocalEnv")
   141  	}
   142  	if *GitCommitFlag != "" {
   143  		env.Commit = *GitCommitFlag
   144  	}
   145  	if *GitBranchFlag != "" {
   146  		env.Branch = *GitBranchFlag
   147  	}
   148  	if *GitTagFlag != "" {
   149  		env.Tag = *GitTagFlag
   150  	}
   151  	if *BuildnumFlag != "" {
   152  		env.Buildnum = *BuildnumFlag
   153  	}
   154  	if *PullRequestFlag {
   155  		env.IsPullRequest = true
   156  	}
   157  	if *CronJobFlag {
   158  		env.IsCronJob = true
   159  	}
   160  	return env
   161  }