1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-30 10:11:23 +02:00

Set 'HOME' during local pipeline step (#1686)

close #1685

---------

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Bruno BELANYI 2023-04-02 15:47:22 +01:00 committed by GitHub
parent 6bf72cbeaf
commit ea95d5aa76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/alessio/shellescape"
@ -30,6 +31,11 @@ import (
"github.com/woodpecker-ci/woodpecker/shared/constant"
)
const (
workingSubDir = "work"
homeSubDir = "home"
)
// notAllowedEnvVarOverwrites are all env vars that can not be overwritten by step config
var notAllowedEnvVarOverwrites = []string{
"CI_NETRC_MACHINE",
@ -42,9 +48,9 @@ var notAllowedEnvVarOverwrites = []string{
type local struct {
// TODO: make cmd a cmd list to iterate over, the hard part is to have a common ReadCloser
cmd *exec.Cmd
output io.ReadCloser
workingdir string
cmd *exec.Cmd
output io.ReadCloser
workflowBaseDir string
}
// New returns a new local Engine.
@ -62,7 +68,7 @@ func (e *local) IsAvailable(context.Context) bool {
func (e *local) Load(context.Context) error {
dir, err := os.MkdirTemp("", "woodpecker-local-*")
e.workingdir = dir
e.workflowBaseDir = dir
return err
}
@ -82,12 +88,15 @@ func (e *local) Exec(ctx context.Context, step *types.Step) error {
}
}
// Set HOME
env = append(env, "HOME="+filepath.Join(e.workflowBaseDir, homeSubDir))
var command []string
if step.Image == constant.DefaultCloneImage {
// Default clone step
// TODO: creat tmp HOME and insert netrc
// TODO: use tmp HOME and insert netrc and delete it after clone
// TODO: download plugin-git binary if not exist
env = append(env, "CI_WORKSPACE="+e.workingdir+"/"+step.Environment["CI_REPO"])
env = append(env, "CI_WORKSPACE="+filepath.Join(e.workflowBaseDir, workingSubDir, step.Environment["CI_REPO"]))
command = append(command, "plugin-git")
} else {
// Use "image name" as run command
@ -111,9 +120,9 @@ func (e *local) Exec(ctx context.Context, step *types.Step) error {
// Prepare working directory
if step.Image == constant.DefaultCloneImage {
e.cmd.Dir = e.workingdir + "/" + step.Environment["CI_REPO_OWNER"]
e.cmd.Dir = filepath.Join(e.workflowBaseDir, workingSubDir, step.Environment["CI_REPO_OWNER"])
} else {
e.cmd.Dir = e.workingdir + "/" + step.Environment["CI_REPO"]
e.cmd.Dir = filepath.Join(e.workflowBaseDir, workingSubDir, step.Environment["CI_REPO"])
}
err := os.MkdirAll(e.cmd.Dir, 0o700)
if err != nil {
@ -152,5 +161,5 @@ func (e *local) Tail(context.Context, *types.Step) (io.ReadCloser, error) {
// Destroy the pipeline environment.
func (e *local) Destroy(context.Context, *types.Config) error {
return os.RemoveAll(e.cmd.Dir)
return os.RemoveAll(e.workflowBaseDir)
}