#Fetch private Go repo using personal access token

1 messages · Page 1 of 1 (latest)

upper dove
#

Hello, I need to be able to fetch a private Go repo and would like to know if it is possible using a personal access token?

I've also seen the cookbook example https://docs.dagger.io/7442989/cookbook#access-private-git-repository which uses SSHAuthSocket but need an Access Token alternative.

In the Github action we currently use the following:

run: git config --global url.https://$GH_ACCESS_TOKEN@github.com/.insteadOf https://github.com/.

I've tried running this after setting the env var e.g. WithWorkdir("/src").WithExec([]string{"git", "config", "--global", "url.https://$GH_ACCESS_TOKEN@github.com/.insteadOf", "https://github.com/"}) but it didn't work.

Filesystem

silent copper
#

@upper dove are you setting the $GH_ACCESS_TOKEN env with WithSecretVariable?

#

take into account that WithExec doesn't execute in the context of a shell so the $GH_ACCESS_TOKEN variable doesn't get expanded. You need to use WithExec([]string{"sh", "-c", "git config --global...."}) for that to work

upper dove
#

Thanks @silent copper - I didn't realise that WithExec doesn't execute in the context of a shell. I'm setting the token with WithSecretVariable. Here's a more complete example

ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
    panic(err)
}
defer client.Close()

secret := client.SetSecret("GH_ACCESS_TOKEN", os.Getenv("GH_ACCESS_TOKEN"))
source := client.Container().
    From(GoImageVersion).
    WithDirectory("/src", client.Host().
        Directory("."), dagger.ContainerWithDirectoryOpts{
        Exclude: []string{"vendor/"},
    })
source = source.WithEnvVariable("GOPRIVATE", os.Getenv("GOPRIVATE"))
source = source.WithSecretVariable("GH_ACCESS_TOKEN", secret)
source.WithWorkdir("/src").WithExec([]string{"sh", "-c", "git", "config", "--global", "url.https://$GH_ACCESS_TOKEN@github.com/.insteadOf", "https://github.com/"})

out, err := source.WithWorkdir("/src").WithExec([]string{"go", "test", "./..."}).Stdout(ctx)
if err != nil {
    panic(err)
}
fmt.Println(out)

I'm getting the error exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled

silent copper
#

👋 checking really quick

silent copper
#

here's the fixed version @upper dove

    ctx := context.Background()
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    secret := client.SetSecret("GH_ACCESS_TOKEN", os.Getenv("GH_ACCESS_TOKEN"))
    source := client.Container().
        From("golang").
        WithDirectory("/src", client.Host().
            Directory("."), dagger.ContainerWithDirectoryOpts{
            Exclude: []string{"vendor/"},
        })
    source = source.WithEnvVariable("GOPRIVATE", os.Getenv("GOPRIVATE"))
    source = source.WithSecretVariable("GH_ACCESS_TOKEN", secret)
    source = source.WithWorkdir("/src").WithExec([]string{"sh", "-c", "git config --global url.\"https://$GH_ACCESS_TOKEN@github.com/.insteadOf\" https://github.com/"})

    out, err := source.WithWorkdir("/src").WithExec([]string{"go", "get", "-v", "./..."}).Stdout(ctx)
    if err != nil {
        panic(err)
    }
    fmt.Println(out)

fixed the "sh -c" WithExec statements which had arguments scrambled and also added the source = source. assignment since it was also missing

#

that works for me

#

also changed the go test to go get.. forgot to switch that one back

upper dove
#

Thank you @silent copper - really appreciate your help 😃