Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitlab: write the .env to $CI_PROJECT_DIR/.env instead of CWD #301

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
giautm marked this conversation as resolved.
Show resolved Hide resolved
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
Loading