#passing credentials to build private go modules in dagger

1 messages · Page 1 of 1 (latest)

fossil rivet
#
# put the contents of the private key argument into id_rsa inside our ssh folder and give permissions
# Then download our modules and private modules, finally delete the ssh key.
RUN echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa && \
go mod download && rm -rf /root/.ssh

Hi folks

I am currently doing the above to pass github credentials to my docker build file. i don't particularly like passing ssh credentials like this.i would prefer something like a github token of some sort. is there a better way to build private go modules in a container that i can use in a dagger module ? how do people do it ?

fossil rivet
half minnow
#

you can also use access tokens and change the git config like:

git config --global url."https://${GITHUB_ACCESS_TOKEN}:@github.com/".insteadOf "https://github.com/"

fossil rivet
#

i am going to to see if ican make that work first

half minnow
#

without a Dockerfile should be easier and simpler

fossil rivet
#

no dockerfile at all .. using go sdk only

half minnow
#

this works:

func (m *Lala) Test(src *dagger.Directory, token *dagger.Secret) *dagger.Container {
    return dag.
        Container().
        From("golang:1.23").
        WithDirectory("/app", src).
        WithWorkdir("/app").
        WithSecretVariable("GITHUB_TOKEN", token).
        WithExec([]string{"bash", "-c", `git config --global url."https://${GITHUB_TOKEN}:@github.com/marcosnils/containers".insteadOf https://github.com/marcosnils/containers`}).
        WithEnvVariable("GOPRIVATE", "github.com/marcosnils/containers").
        WithExec([]string{"go", "build", "."})
}
fossil rivet
#

i am using the github fine grained tokens and would appreciate if you know what permissions does it take to clone a repo ..

half minnow
#

When using fine-grained tokens you have to prefix the token with oauth2: user.
like this:

#

You need to grant Permissions Commit statuses, Contents, Pull requests and Metadata as Read to be able to Clone repositories.

That's according to SO

fossil rivet
#

oh cooool lemme try that

fossil rivet
half minnow
fossil rivet
#

oh you had an extra / in there thanks !!!

half minnow
#

@fossil rivet in the computer now

#

just verified and it works

#

these are the only permissions my fine-grained token needs:

#

and here's my example function:

func (m *Lala) Test(src *dagger.Directory, token *dagger.Secret) *dagger.Container {
    return dag.
        Container().
        From("golang:1.23").
        WithDirectory("/app", src).
        WithWorkdir("/app").
        WithSecretVariable("GITHUB_TOKEN", token).
        WithExec([]string{"bash", "-c", `git config --global url."https://oauth2:${GITHUB_TOKEN}@github.com/marcosnils/containers".insteadOf https://github.com/marcosnils/containers`}).
        WithEnvVariable("GOPRIVATE", "github.com/marcosnils/containers").
        WithExec([]string{"go", "build", "."})
}

fossil rivet
#

finally got it to work !!! thank youuuuuu !!!!

jovial rose
#

hey everyone, I stumbeled upon this thread from GH trying to solve a similar issue. I pretty much copied the example you have here, but I am getting an error: invalid key: when trying to pass my token. Let me know if you want to continue here, or if we should start a new thread.