Skip to content

Commit

Permalink
gitlab: write the .env to $CI_PROJECT_DIR/.env instead of CWD
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Jan 14, 2025
1 parent 6f789be commit 3011985
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion atlasaction/gitlab_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -44,7 +46,15 @@ func (a *gitlabCI) GetInput(name string) string {

// SetOutput implements the Action interface.
func (a *gitlabCI) SetOutput(name, value string) {
err := fprintln(".env", toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value))
dotEnv := ".env"
if dir := a.getenv("CI_PROJECT_DIR"); dir != "" {
if err := os.MkdirAll(dir, 0700); err != nil {
a.Errorf("failed to create output directory %s: %v", dir, err)
return
}
dotEnv = filepath.Join(dir, dotEnv)
}
err := fprintln(dotEnv, toOutputVar(a.getenv("ATLAS_ACTION_COMMAND"), name, value))
if err != nil {
a.Errorf("failed to write output to file .env: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions atlasaction/gitlab_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestGitlabCI(t *testing.T) {
e.Defer(srv.Close)
e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh"))
e.Setenv("CI_API_V4_URL", srv.URL)
e.Setenv("CI_PROJECT_DIR", filepath.Join(e.WorkDir, "project"))
e.Setenv("CI_PROJECT_ID", "1")
e.Setenv("GITLAB_CI", "true")
e.Setenv("GITLAB_TOKEN", "token")
Expand All @@ -36,7 +37,7 @@ func TestGitlabCI(t *testing.T) {
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"output": func(ts *testscript.TestScript, neg bool, args []string) {
if len(args) == 0 {
_, err := os.Stat(ts.MkAbs(".env"))
_, err := os.Stat(ts.MkAbs("./project/.env"))
if neg {
if !os.IsNotExist(err) {
ts.Fatalf("expected no output, but got some")
Expand All @@ -49,7 +50,7 @@ func TestGitlabCI(t *testing.T) {
}
return
}
cmpFiles(ts, neg, args[0], ".env")
cmpFiles(ts, neg, args[0], "./project/.env")
},
},
})
Expand Down

0 comments on commit 3011985

Please sign in to comment.