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 reposi…


This content originally appeared on DEV Community and was authored by Javad Rajabzade

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
)

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 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"]

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


This content originally appeared on DEV Community and was authored by Javad Rajabzade


Print Share Comment Cite Upload Translate Updates
APA

Javad Rajabzade | Sciencx (2021-06-18T08:32:12+00:00) How to use Go Modules with Private Git repository. Retrieved from https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/

MLA
" » How to use Go Modules with Private Git repository." Javad Rajabzade | Sciencx - Friday June 18, 2021, https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/
HARVARD
Javad Rajabzade | Sciencx Friday June 18, 2021 » How to use Go Modules with Private Git repository., viewed ,<https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/>
VANCOUVER
Javad Rajabzade | Sciencx - » How to use Go Modules with Private Git repository. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/
CHICAGO
" » How to use Go Modules with Private Git repository." Javad Rajabzade | Sciencx - Accessed . https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/
IEEE
" » How to use Go Modules with Private Git repository." Javad Rajabzade | Sciencx [Online]. Available: https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/. [Accessed: ]
rf:citation
» How to use Go Modules with Private Git repository | Javad Rajabzade | Sciencx | https://www.scien.cx/2021/06/18/how-to-use-go-modules-with-private-git-repository/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.