From 14c8c52c03fbfa3369acaa897163529ba0700c88 Mon Sep 17 00:00:00 2001 From: giuli007 <39272329+giuli007@users.noreply.github.com> Date: Thu, 24 Jun 2021 19:16:47 +0200 Subject: [PATCH] Always use the default workspace directory when building commands context (#1620) When pre_workflow_hooks is being used to generate atlantis.yaml and projects are set with specific workspaces different than "default", a different directory is created for each project-workspace pair, but all those that are not on the "default" workspace will not have an atlantis.yaml file at their root. This change ensures that when atlantis is building the projects commands for either: - a project-specific plan, triggered via (atlantis plan -d foo -w bar) - a project-specific apply, triggered vi (atlantis apply -d foo -w bar) - or applies for all the plans it can find (atlantis apply) it uses the default workspace to find the atlantis.yaml config. Co-authored-by: giuli007 --- server/events/project_command_builder.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/server/events/project_command_builder.go b/server/events/project_command_builder.go index 10631eeb2..d93fa0732 100644 --- a/server/events/project_command_builder.go +++ b/server/events/project_command_builder.go @@ -351,7 +351,7 @@ func (p *DefaultProjectCommandBuilder) buildProjectPlanCommand(ctx *CommandConte defer unlockFn() ctx.Log.Debug("cloning repository") - repoDir, _, err := p.WorkingDir.Clone(ctx.Log, ctx.HeadRepo, ctx.Pull, workspace) + _, _, err = p.WorkingDir.Clone(ctx.Log, ctx.HeadRepo, ctx.Pull, workspace) if err != nil { return pcc, err } @@ -361,12 +361,19 @@ func (p *DefaultProjectCommandBuilder) buildProjectPlanCommand(ctx *CommandConte repoRelDir = cmd.RepoRelDir } + // use the default repository workspace because it is the only one guaranteed to have an atlantis.yaml, + // other workspaces will not have the file if they are using pre_workflow_hooks to generate it dynamically + defaultRepoDir, err := p.WorkingDir.GetWorkingDir(ctx.Pull.BaseRepo, ctx.Pull, DefaultWorkspace) + if err != nil { + return pcc, err + } + return p.buildProjectCommandCtx( ctx, models.PlanCommand, cmd.ProjectName, cmd.Flags, - repoDir, + defaultRepoDir, repoRelDir, workspace, cmd.Verbose, @@ -447,9 +454,16 @@ func (p *DefaultProjectCommandBuilder) buildAllProjectCommands(ctx *CommandConte return nil, err } + // use the default repository workspace because it is the only one guaranteed to have an atlantis.yaml, + // other workspaces will not have the file if they are using pre_workflow_hooks to generate it dynamically + defaultRepoDir, err := p.WorkingDir.GetWorkingDir(ctx.Pull.BaseRepo, ctx.Pull, DefaultWorkspace) + if err != nil { + return nil, err + } + var cmds []models.ProjectCommandContext for _, plan := range plans { - commentCmds, err := p.buildProjectCommandCtx(ctx, commentCmd.CommandName(), plan.ProjectName, commentCmd.Flags, plan.RepoDir, plan.RepoRelDir, plan.Workspace, commentCmd.Verbose, commentCmd.ForceApply) + commentCmds, err := p.buildProjectCommandCtx(ctx, commentCmd.CommandName(), plan.ProjectName, commentCmd.Flags, defaultRepoDir, plan.RepoRelDir, plan.Workspace, commentCmd.Verbose, commentCmd.ForceApply) if err != nil { return nil, errors.Wrapf(err, "building command for dir %q", plan.RepoRelDir) } @@ -473,7 +487,9 @@ func (p *DefaultProjectCommandBuilder) buildProjectApplyCommand(ctx *CommandCont } defer unlockFn() - repoDir, err := p.WorkingDir.GetWorkingDir(ctx.Pull.BaseRepo, ctx.Pull, workspace) + // use the default repository workspace because it is the only one guaranteed to have an atlantis.yaml, + // other workspaces will not have the file if they are using pre_workflow_hooks to generate it dynamically + repoDir, err := p.WorkingDir.GetWorkingDir(ctx.Pull.BaseRepo, ctx.Pull, DefaultWorkspace) if os.IsNotExist(errors.Cause(err)) { return projCtx, errors.New("no working directory found–did you run plan?") } else if err != nil {