github.com/core-coin/go-core/v2@v2.1.9/internal/build/env.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package build
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"regexp"
    24  	"strconv"
    25  	"strings"
    26  	"time"
    27  )
    28  
    29  var (
    30  	// These flags override values in build env.
    31  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    32  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    33  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    34  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    35  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    36  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    37  )
    38  
    39  // Environment contains metadata provided by the build environment.
    40  type Environment struct {
    41  	Name                      string // name of the environment
    42  	Repo                      string // name of GitHub repo
    43  	Commit, Date, Branch, Tag string // Git info
    44  	Type                      string
    45  	Buildnum                  string
    46  	IsPullRequest             bool
    47  	IsCronJob                 bool
    48  }
    49  
    50  func (env Environment) String() string {
    51  	return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
    52  		env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    53  }
    54  
    55  // Env returns metadata about the current CI environment, falling back to LocalEnv
    56  // if not running on CI.
    57  func Env() Environment {
    58  	if os.Getenv("CI") == "true" && os.Getenv("GITHUB_ACTIONS") == "true" {
    59  		commit := os.Getenv("GITHUB_SHA")
    60  		return Environment{
    61  			Name:          "github actions",
    62  			Repo:          os.Getenv("GITHUB_REPOSITORY"),
    63  			Commit:        os.Getenv("GITHUB_SHA"),
    64  			Date:          getDate(commit),
    65  			Branch:        os.Getenv("GITHUB_REF_NAME"),
    66  			Tag:           os.Getenv("GITHUB_REF_NAME"),
    67  			Buildnum:      os.Getenv("GITHUB_RUN_ID"),
    68  			Type:          os.Getenv("GITHUB_REF_TYPE"),
    69  			IsPullRequest: false,
    70  			IsCronJob:     false,
    71  		}
    72  	} else {
    73  		return LocalEnv()
    74  	}
    75  }
    76  
    77  // LocalEnv returns build environment metadata gathered from git.
    78  func LocalEnv() Environment {
    79  	env := applyEnvFlags(Environment{Name: "local", Repo: "core-coin/go-core"})
    80  
    81  	head := readGitFile("HEAD")
    82  	if fields := strings.Fields(head); len(fields) == 2 {
    83  		head = fields[1]
    84  	} else {
    85  		// In this case we are in "detached head" state
    86  		// see: https://git-scm.com/docs/git-checkout#_detached_head
    87  		// Additional check required to verify, that file contains commit hash
    88  		commitRe, _ := regexp.Compile("^([0-9a-f]{40})$")
    89  		if commit := commitRe.FindString(head); commit != "" && env.Commit == "" {
    90  			env.Commit = commit
    91  		}
    92  	}
    93  	if env.Commit == "" {
    94  		env.Commit = readGitFile(head)
    95  	}
    96  	env.Date = getDate(env.Commit)
    97  
    98  	if env.Branch == "" {
    99  		if head != "HEAD" {
   100  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
   101  		}
   102  	}
   103  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
   104  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
   105  	}
   106  
   107  	return env
   108  }
   109  
   110  func firstLine(s string) string {
   111  	return strings.Split(s, "\n")[0]
   112  }
   113  
   114  func getDate(commit string) string {
   115  	if commit == "" {
   116  		return ""
   117  	}
   118  	out := RunGit("show", "-s", "--format=%ct", commit)
   119  	if out == "" {
   120  		return ""
   121  	}
   122  	date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
   123  	if err != nil {
   124  		panic(fmt.Sprintf("failed to parse git commit date: %v", err))
   125  	}
   126  	return time.Unix(date, 0).Format("20060102")
   127  }
   128  
   129  func applyEnvFlags(env Environment) Environment {
   130  	if !flag.Parsed() {
   131  		panic("you need to call flag.Parse before Env or LocalEnv")
   132  	}
   133  	if *GitCommitFlag != "" {
   134  		env.Commit = *GitCommitFlag
   135  	}
   136  	if *GitBranchFlag != "" {
   137  		env.Branch = *GitBranchFlag
   138  	}
   139  	if *GitTagFlag != "" {
   140  		env.Tag = *GitTagFlag
   141  	}
   142  	if *BuildnumFlag != "" {
   143  		env.Buildnum = *BuildnumFlag
   144  	}
   145  	if *PullRequestFlag {
   146  		env.IsPullRequest = true
   147  	}
   148  	if *CronJobFlag {
   149  		env.IsCronJob = true
   150  	}
   151  	return env
   152  }