An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
How to use Go Modules with Private Git repository
The Go module system has greatly improved the way dependencies are managed. If you are new to Go Modules.
Public Repositories
After configuring everything correctly, it is relatively straightforward to include Go packages from public repositories. Typical starting points for me are as follows:
module github.com/Ja7ad/projectgo 1.16require ( github.com/pkg/errors github.com/spf13/cobra github.com/spf13/viper)
Private Repositories
Go uses Git to pull the versions of the dependencies you specify. To access any private repositories, the git configuration for wherever Go is running (e.g. a Docker container or your laptop) needs to be configured with the appropriate credentials.
GitHub
git config --global url."https://${user}:${personal_access_token}@github.com".insteadOf "https://github.com"
GitLab
git config --global url."https://oauth2:${personal_access_token}@privategitlab.com".insteadOf "https://privategitlab.com"# or git config --global url."https://${user}:${personal_access_token}@privategitlab.com".insteadOf "https://privategitlab.com"
BitBucket
git config --global url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com".insteadOf "https://privatebitbucket.com"
CI/CD Pipeline
Dockerfile can be added via this method and add private repository to the image:
# ---------------------------------------------------------------------# The first stage container, for building the application# ---------------------------------------------------------------------FROM golang:1.16-stretch as builderCOPY . /app# Add the keysARG github_userENV github_user=$github_userARG github_personal_tokenENV github_personal_token=$github_personal_tokenWORKDIR /app/cmd/webappRUN git config \ --global \ url."https://${github_user}:${github_personal_token}@@github.com".insteadOf \ "https://github.com"RUN GIT_TERMINAL_PROMPT=1 \ GOARCH=amd64 \ GOOS=linux \ CGO_ENABLED=0 \ go build -v --installsuffix cgo --ldflags="-s" -o myapp# ---------------------------------------------------------------------# The second stage container, for running the application# ---------------------------------------------------------------------FROM alpine:3.8COPY --from=builder /app/cmd/webapp/myapp /app/myappWORKDIR /appENTRYPOINT ["/myapp"]
You can use the following method for docker-compose.yml:
version: '3.6'services: app: container_name: my_go_app_container build: # context can/may/will be different per-project setup context: ../ dockerfile: GitDockerfile args: - github_user=private_user - github_personal_token=private_token image: my_go_app_image # other configs...
SSH
Another way to do this would be to use your SSH key every time you connect, and to set .gitconfig like the following to ensure SSH is used each time:
git config --global url."[email protected]".insteadOf "https://github.com"
Original Link: https://dev.to/gopher/how-to-use-go-modules-with-private-git-repository-53b4
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To