DEV Community

Javad Rajabzadeh for Gopher

Posted on

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/project

go 1.16

require (
    github.com/pkg/errors
    github.com/spf13/cobra
    github.com/spf13/viper
)
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

BitBucket

git config --global url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com".insteadOf "https://privatebitbucket.com"
Enter fullscreen mode Exit fullscreen mode

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 builder

COPY . /app

# Add the keys
ARG github_user
ENV github_user=$github_user
ARG github_personal_token
ENV github_personal_token=$github_personal_token

WORKDIR /app/cmd/webapp

RUN 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.8
COPY --from=builder /app/cmd/webapp/myapp /app/myapp

WORKDIR /app

ENTRYPOINT ["/myapp"]
Enter fullscreen mode Exit fullscreen mode

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...
Enter fullscreen mode Exit fullscreen mode

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."git@github.com".insteadOf "https://github.com"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)