-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre workflow custom hooks to run scripts before workflow executio…
…n(plan, apply, etc) (#1255) * Updated runatlantis.io/docs to have `pre-workflow-hooks` use cases and examples
- Loading branch information
Showing
22 changed files
with
1,015 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Pre Workflow Hooks | ||
|
||
Pre workflow hooks can be defined to run scripts right before default or custom | ||
workflows are executed. | ||
|
||
[[toc]] | ||
|
||
## Usage | ||
Pre workflow hooks can only be specified in the Server-Side Repo Config under | ||
`repos` key. | ||
::: tip Note | ||
`pre-workflow-hooks` do not prevent Atlantis from executing its | ||
workflows(`plan`, `apply`) even if a `run` command exits with an error. | ||
::: | ||
|
||
## Use Cases | ||
### Dynamic Repo Config Generation | ||
If you want generate your `atlantis.yaml` before Atlantis can parse it. You | ||
can add a `run` command to `pre_workflow_hooks`. Your Repo config will be generated | ||
right before Atlantis can parse it. | ||
|
||
```yaml | ||
repos: | ||
- id: /.*/ | ||
pre_workflow_hooks: | ||
- run: ./repo-config-genarator.sh | ||
``` | ||
### Reference | ||
#### Custom `run` Command | ||
This is very similar to [custom workflow run | ||
command](custom-workflows.html#custom-run-command). | ||
```yaml | ||
- run: custom-command | ||
``` | ||
| Key | Type | Default | Required | Description | | ||
|-----|--------|---------|----------|----------------------| | ||
| run | string | none | no | Run a custom command | | ||
|
||
::: tip Notes | ||
* `run` commands are executed with the following environment variables: | ||
* `BASE_REPO_NAME` - Name of the repository that the pull request will be merged into, ex. `atlantis`. | ||
* `BASE_REPO_OWNER` - Owner of the repository that the pull request will be merged into, ex. `runatlantis`. | ||
* `HEAD_REPO_NAME` - Name of the repository that is getting merged into the base repository, ex. `atlantis`. | ||
* `HEAD_REPO_OWNER` - Owner of the repository that is getting merged into the base repository, ex. `acme-corp`. | ||
* `HEAD_BRANCH_NAME` - Name of the head branch of the pull request (the branch that is getting merged into the base) | ||
* `BASE_BRANCH_NAME` - Name of the base branch of the pull request (the branch that the pull request is getting merged into) | ||
* `PULL_NUM` - Pull request number or ID, ex. `2`. | ||
* `PULL_AUTHOR` - Username of the pull request author, ex. `acme-user`. | ||
* `DIR` - The absolute path to the root of the cloned repository. | ||
* `USER_NAME` - Username of the VCS user running command, ex. `acme-user`. During an autoplan, the user will be the Atlantis API user, ex. `atlantis`. | ||
::: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
server/events/mocks/mock_pre_workflows_hooks_command_runner.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/runatlantis/atlantis/server/events/models" | ||
"github.com/runatlantis/atlantis/server/events/runtime" | ||
"github.com/runatlantis/atlantis/server/events/vcs" | ||
"github.com/runatlantis/atlantis/server/events/yaml/valid" | ||
"github.com/runatlantis/atlantis/server/logging" | ||
"github.com/runatlantis/atlantis/server/recovery" | ||
) | ||
|
||
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_pre_workflows_hooks_command_runner.go PreWorkflowHooksCommandRunner | ||
|
||
type PreWorkflowHooksCommandRunner interface { | ||
RunPreHooks( | ||
baseRepo models.Repo, | ||
headRepo models.Repo, | ||
pull models.PullRequest, | ||
user models.User, | ||
) | ||
} | ||
|
||
// DefaultPreWorkflowHooksCommandRunner is the first step when processing a workflow hook commands. | ||
type DefaultPreWorkflowHooksCommandRunner struct { | ||
VCSClient vcs.Client | ||
Logger logging.SimpleLogging | ||
WorkingDirLocker WorkingDirLocker | ||
WorkingDir WorkingDir | ||
GlobalCfg valid.GlobalCfg | ||
Drainer *Drainer | ||
PreWorkflowHookRunner *runtime.PreWorkflowHookRunner | ||
} | ||
|
||
// RunPreHooks runs pre_workflow_hooks when PR is opened or updated. | ||
func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks( | ||
baseRepo models.Repo, | ||
headRepo models.Repo, | ||
pull models.PullRequest, | ||
user models.User, | ||
) { | ||
if opStarted := w.Drainer.StartOp(); !opStarted { | ||
if commentErr := w.VCSClient.CreateComment(baseRepo, pull.Num, ShutdownComment, "pre_workflow_hooks"); commentErr != nil { | ||
w.Logger.Log(logging.Error, "unable to comment that Atlantis is shutting down: %s", commentErr) | ||
} | ||
return | ||
} | ||
defer w.Drainer.OpDone() | ||
|
||
log := w.buildLogger(baseRepo.FullName, pull.Num) | ||
defer w.logPanics(baseRepo, pull.Num, log) | ||
|
||
log.Info("running pre hooks") | ||
|
||
unlockFn, err := w.WorkingDirLocker.TryLock(baseRepo.FullName, pull.Num, DefaultWorkspace) | ||
if err != nil { | ||
log.Warn("workspace is locked") | ||
return | ||
} | ||
log.Debug("got workspace lock") | ||
defer unlockFn() | ||
|
||
repoDir, _, err := w.WorkingDir.Clone(log, headRepo, pull, DefaultWorkspace) | ||
if err != nil { | ||
log.Err("unable to run pre workflow hooks: %s", err) | ||
return | ||
} | ||
|
||
preWorkflowHooks := make([]*valid.PreWorkflowHook, 0) | ||
for _, repo := range w.GlobalCfg.Repos { | ||
if repo.IDMatches(baseRepo.ID()) && len(repo.PreWorkflowHooks) > 0 { | ||
preWorkflowHooks = append(preWorkflowHooks, repo.PreWorkflowHooks...) | ||
} | ||
} | ||
|
||
ctx := models.PreWorkflowHookCommandContext{ | ||
BaseRepo: baseRepo, | ||
HeadRepo: headRepo, | ||
Log: log, | ||
Pull: pull, | ||
User: user, | ||
Verbose: false, | ||
} | ||
|
||
err = w.runHooks(ctx, preWorkflowHooks, repoDir) | ||
|
||
if err != nil { | ||
log.Err("pre workflow hook run error results: %s", err) | ||
} | ||
} | ||
|
||
func (w *DefaultPreWorkflowHooksCommandRunner) runHooks( | ||
ctx models.PreWorkflowHookCommandContext, | ||
preWorkflowHooks []*valid.PreWorkflowHook, | ||
repoDir string, | ||
) error { | ||
|
||
for _, hook := range preWorkflowHooks { | ||
_, err := w.PreWorkflowHookRunner.Run(ctx, hook.RunCommand, repoDir) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *DefaultPreWorkflowHooksCommandRunner) buildLogger(repoFullName string, pullNum int) *logging.SimpleLogger { | ||
src := fmt.Sprintf("%s#%d", repoFullName, pullNum) | ||
return w.Logger.NewLogger(src, true, w.Logger.GetLevel()) | ||
} | ||
|
||
// logPanics logs and creates a comment on the pull request for panics. | ||
func (w *DefaultPreWorkflowHooksCommandRunner) logPanics(baseRepo models.Repo, pullNum int, logger logging.SimpleLogging) { | ||
if err := recover(); err != nil { | ||
stack := recovery.Stack(3) | ||
logger.Err("PANIC: %s\n%s", err, stack) | ||
if commentErr := w.VCSClient.CreateComment( | ||
baseRepo, | ||
pullNum, | ||
fmt.Sprintf("**Error: goroutine panic. This is a bug.**\n```\n%s\n%s```", err, stack), | ||
"", | ||
); commentErr != nil { | ||
logger.Err("unable to comment: %s", commentErr) | ||
} | ||
} | ||
} |
Oops, something went wrong.