github.com/bshelton229/agent@v3.5.4+incompatible/clicommand/bootstrap.go (about)

     1  package clicommand
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  
     7  	"github.com/buildkite/agent/bootstrap"
     8  	"github.com/buildkite/agent/cliconfig"
     9  	"github.com/buildkite/agent/logger"
    10  	"github.com/urfave/cli"
    11  )
    12  
    13  var BootstrapHelpDescription = `Usage:
    14  
    15     buildkite-agent bootstrap [arguments...]
    16  
    17  Description:
    18  
    19     The bootstrap command executes a buildkite job locally.
    20  
    21     Generally the bootstrap command is run as a sub-process of the buildkite-agent to execute
    22     a given job sent from buildkite.com, but you can also invoke the bootstrap manually.
    23  
    24     Execution is broken down into phases. By default, the bootstrap runs a plugin phase which
    25     sets up any plugins specified, then a checkout phase which pulls down your code and then a
    26     command phase that executes the specified command in the created environment.
    27  
    28     You can run only specific phases with the --phases flag.
    29  
    30     The bootstrap is also responsible for executing hooks around the phases.
    31     See https://buildkite.com/docs/agent/v3/hooks for more details.
    32  
    33  Example:
    34  
    35     $ eval $(curl -s -H "Authorization: Bearer xxx" \
    36       "https://api.buildkite.com/v2/organizations/[org]/pipelines/[proj]/builds/[build]/jobs/[job]/env.txt" | sed 's/^/export /')
    37     $ buildkite-agent bootstrap --build-path builds`
    38  
    39  type BootstrapConfig struct {
    40  	Command                      string   `cli:"command"`
    41  	JobID                        string   `cli:"job" validate:"required"`
    42  	Repository                   string   `cli:"repository" validate:"required"`
    43  	Commit                       string   `cli:"commit" validate:"required"`
    44  	Branch                       string   `cli:"branch" validate:"required"`
    45  	Tag                          string   `cli:"tag"`
    46  	RefSpec                      string   `cli:"refspec"`
    47  	Plugins                      string   `cli:"plugins"`
    48  	PullRequest                  string   `cli:"pullrequest"`
    49  	GitSubmodules                bool     `cli:"git-submodules"`
    50  	SSHKeyscan                   bool     `cli:"ssh-keyscan"`
    51  	AgentName                    string   `cli:"agent" validate:"required"`
    52  	OrganizationSlug             string   `cli:"organization" validate:"required"`
    53  	PipelineSlug                 string   `cli:"pipeline" validate:"required"`
    54  	PipelineProvider             string   `cli:"pipeline-provider" validate:"required"`
    55  	AutomaticArtifactUploadPaths string   `cli:"artifact-upload-paths"`
    56  	ArtifactUploadDestination    string   `cli:"artifact-upload-destination"`
    57  	CleanCheckout                bool     `cli:"clean-checkout"`
    58  	GitCloneFlags                string   `cli:"git-clone-flags"`
    59  	GitCleanFlags                string   `cli:"git-clean-flags"`
    60  	BinPath                      string   `cli:"bin-path" normalize:"filepath"`
    61  	BuildPath                    string   `cli:"build-path" normalize:"filepath"`
    62  	HooksPath                    string   `cli:"hooks-path" normalize:"filepath"`
    63  	PluginsPath                  string   `cli:"plugins-path" normalize:"filepath"`
    64  	CommandEval                  bool     `cli:"command-eval"`
    65  	PluginsEnabled               bool     `cli:"plugins-enabled"`
    66  	PluginValidation             bool     `cli:"plugin-validation"`
    67  	LocalHooksEnabled            bool     `cli:"local-hooks-enabled"`
    68  	PTY                          bool     `cli:"pty"`
    69  	Debug                        bool     `cli:"debug"`
    70  	Shell                        string   `cli:"shell"`
    71  	Phases                       []string `cli:"phases" normalize:"list"`
    72  }
    73  
    74  var BootstrapCommand = cli.Command{
    75  	Name:        "bootstrap",
    76  	Usage:       "Run a Buildkite job locally",
    77  	Description: BootstrapHelpDescription,
    78  	Flags: []cli.Flag{
    79  		cli.StringFlag{
    80  			Name:   "command",
    81  			Value:  "",
    82  			Usage:  "The command to run",
    83  			EnvVar: "BUILDKITE_COMMAND",
    84  		},
    85  		cli.StringFlag{
    86  			Name:   "job",
    87  			Value:  "",
    88  			Usage:  "The ID of the job being run",
    89  			EnvVar: "BUILDKITE_JOB_ID",
    90  		},
    91  		cli.StringFlag{
    92  			Name:   "repository",
    93  			Value:  "",
    94  			Usage:  "The repository to clone and run the job from",
    95  			EnvVar: "BUILDKITE_REPO",
    96  		},
    97  		cli.StringFlag{
    98  			Name:   "commit",
    99  			Value:  "",
   100  			Usage:  "The commit to checkout in the repository",
   101  			EnvVar: "BUILDKITE_COMMIT",
   102  		},
   103  		cli.StringFlag{
   104  			Name:   "branch",
   105  			Value:  "",
   106  			Usage:  "The branch the commit is in",
   107  			EnvVar: "BUILDKITE_BRANCH",
   108  		},
   109  		cli.StringFlag{
   110  			Name:   "tag",
   111  			Value:  "",
   112  			Usage:  "The tag the commit",
   113  			EnvVar: "BUILDKITE_TAG",
   114  		},
   115  		cli.StringFlag{
   116  			Name:   "refspec",
   117  			Value:  "",
   118  			Usage:  "Optional refspec to override git fetch",
   119  			EnvVar: "BUILDKITE_REFSPEC",
   120  		},
   121  		cli.StringFlag{
   122  			Name:   "plugins",
   123  			Value:  "",
   124  			Usage:  "The plugins for the job",
   125  			EnvVar: "BUILDKITE_PLUGINS",
   126  		},
   127  		cli.StringFlag{
   128  			Name:   "pullrequest",
   129  			Value:  "",
   130  			Usage:  "The number/id of the pull request this commit belonged to",
   131  			EnvVar: "BUILDKITE_PULL_REQUEST",
   132  		},
   133  		cli.StringFlag{
   134  			Name:   "agent",
   135  			Value:  "",
   136  			Usage:  "The name of the agent running the job",
   137  			EnvVar: "BUILDKITE_AGENT_NAME",
   138  		},
   139  		cli.StringFlag{
   140  			Name:   "organization",
   141  			Value:  "",
   142  			Usage:  "The slug of the organization that the job is a part of",
   143  			EnvVar: "BUILDKITE_ORGANIZATION_SLUG",
   144  		},
   145  		cli.StringFlag{
   146  			Name:   "pipeline",
   147  			Value:  "",
   148  			Usage:  "The slug of the pipeline that the job is a part of",
   149  			EnvVar: "BUILDKITE_PIPELINE_SLUG",
   150  		},
   151  		cli.StringFlag{
   152  			Name:   "pipeline-provider",
   153  			Value:  "",
   154  			Usage:  "The id of the SCM provider that the repository is hosted on",
   155  			EnvVar: "BUILDKITE_PIPELINE_PROVIDER",
   156  		},
   157  		cli.StringFlag{
   158  			Name:   "artifact-upload-paths",
   159  			Value:  "",
   160  			Usage:  "Paths to files to automatically upload at the end of a job",
   161  			EnvVar: "BUILDKITE_ARTIFACT_PATHS",
   162  		},
   163  		cli.StringFlag{
   164  			Name:   "artifact-upload-destination",
   165  			Value:  "",
   166  			Usage:  "A custom location to upload artifact paths to (i.e. s3://my-custom-bucket)",
   167  			EnvVar: "BUILDKITE_ARTIFACT_UPLOAD_DESTINATION",
   168  		},
   169  		cli.BoolFlag{
   170  			Name:   "clean-checkout",
   171  			Usage:  "Whether or not the bootstrap should remove the existing repository before running the command",
   172  			EnvVar: "BUILDKITE_CLEAN_CHECKOUT",
   173  		},
   174  		cli.StringFlag{
   175  			Name:   "git-clone-flags",
   176  			Value:  "-v",
   177  			Usage:  "Flags to pass to \"git clone\" command",
   178  			EnvVar: "BUILDKITE_GIT_CLONE_FLAGS",
   179  		},
   180  		cli.StringFlag{
   181  			Name:   "git-clean-flags",
   182  			Value:  "-fxdq",
   183  			Usage:  "Flags to pass to \"git clean\" command",
   184  			EnvVar: "BUILDKITE_GIT_CLEAN_FLAGS",
   185  		},
   186  		cli.StringFlag{
   187  			Name:   "bin-path",
   188  			Value:  "",
   189  			Usage:  "Directory where the buildkite-agent binary lives",
   190  			EnvVar: "BUILDKITE_BIN_PATH",
   191  		},
   192  		cli.StringFlag{
   193  			Name:   "build-path",
   194  			Value:  "",
   195  			Usage:  "Directory where builds will be created",
   196  			EnvVar: "BUILDKITE_BUILD_PATH",
   197  		},
   198  		cli.StringFlag{
   199  			Name:   "hooks-path",
   200  			Value:  "",
   201  			Usage:  "Directory where the hook scripts are found",
   202  			EnvVar: "BUILDKITE_HOOKS_PATH",
   203  		},
   204  		cli.StringFlag{
   205  			Name:   "plugins-path",
   206  			Value:  "",
   207  			Usage:  "Directory where the plugins are saved to",
   208  			EnvVar: "BUILDKITE_PLUGINS_PATH",
   209  		},
   210  		cli.BoolTFlag{
   211  			Name:   "command-eval",
   212  			Usage:  "Allow running of arbitary commands",
   213  			EnvVar: "BUILDKITE_COMMAND_EVAL",
   214  		},
   215  		cli.BoolTFlag{
   216  			Name:   "plugins-enabled",
   217  			Usage:  "Allow plugins to be run",
   218  			EnvVar: "BUILDKITE_PLUGINS_ENABLED",
   219  		},
   220  		cli.BoolFlag{
   221  			Name:   "plugin-validation",
   222  			Usage:  "Validate plugin configuration",
   223  			EnvVar: "BUILDKITE_PLUGIN_VALIDATION",
   224  		},
   225  		cli.BoolTFlag{
   226  			Name:   "local-hooks-enabled",
   227  			Usage:  "Allow local hooks to be run",
   228  			EnvVar: "BUILDKITE_LOCAL_HOOKS_ENABLED",
   229  		},
   230  		cli.BoolTFlag{
   231  			Name:   "ssh-keyscan",
   232  			Usage:  "Automatically run ssh-keyscan before checkout",
   233  			EnvVar: "BUILDKITE_SSH_KEYSCAN",
   234  		},
   235  		cli.BoolTFlag{
   236  			Name:   "git-submodules",
   237  			Usage:  "Enable git submodules",
   238  			EnvVar: "BUILDKITE_GIT_SUBMODULES",
   239  		},
   240  		cli.BoolTFlag{
   241  			Name:   "pty",
   242  			Usage:  "Run jobs within a pseudo terminal",
   243  			EnvVar: "BUILDKITE_PTY",
   244  		},
   245  		cli.StringFlag{
   246  			Name:   "shell",
   247  			Usage:  "The shell to use to interpret build commands",
   248  			EnvVar: "BUILDKITE_SHELL",
   249  			Value:  DefaultShell(),
   250  		},
   251  		cli.StringSliceFlag{
   252  			Name:   "phases",
   253  			Usage:  "The specific phases to execute. The order they're defined is is irrelevant.",
   254  			EnvVar: "BUILDKITE_BOOTSTRAP_PHASES",
   255  		},
   256  		DebugFlag,
   257  	},
   258  	Action: func(c *cli.Context) {
   259  		// The configuration will be loaded into this struct
   260  		cfg := BootstrapConfig{}
   261  
   262  		// Load the configuration
   263  		if err := cliconfig.Load(c, &cfg); err != nil {
   264  			logger.Fatal("%s", err)
   265  		}
   266  
   267  		// Turn of PTY support if we're on Windows
   268  		runInPty := cfg.PTY
   269  		if runtime.GOOS == "windows" {
   270  			runInPty = false
   271  		}
   272  
   273  		// Validate phases
   274  		for _, phase := range cfg.Phases {
   275  			switch phase {
   276  			case "plugin", "checkout", "command":
   277  				// Valid phase
   278  			default:
   279  				logger.Fatal("Invalid phase %q", phase)
   280  			}
   281  		}
   282  
   283  		// Configure the bootstraper
   284  		bootstrap := &bootstrap.Bootstrap{
   285  			Phases: cfg.Phases,
   286  			Config: bootstrap.Config{
   287  				Command:                      cfg.Command,
   288  				JobID:                        cfg.JobID,
   289  				Repository:                   cfg.Repository,
   290  				Commit:                       cfg.Commit,
   291  				Branch:                       cfg.Branch,
   292  				Tag:                          cfg.Tag,
   293  				RefSpec:                      cfg.RefSpec,
   294  				Plugins:                      cfg.Plugins,
   295  				GitSubmodules:                cfg.GitSubmodules,
   296  				PullRequest:                  cfg.PullRequest,
   297  				GitCloneFlags:                cfg.GitCloneFlags,
   298  				GitCleanFlags:                cfg.GitCleanFlags,
   299  				AgentName:                    cfg.AgentName,
   300  				PipelineProvider:             cfg.PipelineProvider,
   301  				PipelineSlug:                 cfg.PipelineSlug,
   302  				OrganizationSlug:             cfg.OrganizationSlug,
   303  				AutomaticArtifactUploadPaths: cfg.AutomaticArtifactUploadPaths,
   304  				ArtifactUploadDestination:    cfg.ArtifactUploadDestination,
   305  				CleanCheckout:                cfg.CleanCheckout,
   306  				BuildPath:                    cfg.BuildPath,
   307  				BinPath:                      cfg.BinPath,
   308  				HooksPath:                    cfg.HooksPath,
   309  				PluginsPath:                  cfg.PluginsPath,
   310  				PluginValidation:             cfg.PluginValidation,
   311  				Debug:                        cfg.Debug,
   312  				RunInPty:                     runInPty,
   313  				CommandEval:                  cfg.CommandEval,
   314  				PluginsEnabled:               cfg.PluginsEnabled,
   315  				LocalHooksEnabled:            cfg.LocalHooksEnabled,
   316  				SSHKeyscan:                   cfg.SSHKeyscan,
   317  				Shell:                        cfg.Shell,
   318  			},
   319  		}
   320  
   321  		// Run the bootstrap and exit with whatever it returns
   322  		os.Exit(bootstrap.Start())
   323  	},
   324  }