项目作者: go-training

项目描述 :
Go Modules with Private GIT Repository
高级语言: Dockerfile
项目地址: git://github.com/go-training/golang-private-module.git
创建时间: 2020-02-23T02:31:46Z
项目社区:https://github.com/go-training/golang-private-module

开源协议:

下载


golang-private-module

Go Modules with Private GIT Repository

Local Machine

  1. go env -w GOPRIVATE=github.com/appleboy
  2. git config --global url."https://$USERNAME:$ACCESS_TOKEN@github.com".insteadOf "https://github.com"

try the private repo:

  1. go get github.com/appleboy/golang-private

Drone CI

  1. steps:
  2. - name: build
  3. image: golang:1.13
  4. environment:
  5. USERNAME:
  6. from_secret: username
  7. ACCESS_TOKEN:
  8. from_secret: access_token
  9. commands:
  10. - go env -w GOPRIVATE=github.com/$USERNAME
  11. - git config --global url."https://$USERNAME:$ACCESS_TOKEN@github.com".insteadOf "https://github.com"
  12. - go mod tidy
  13. - go build -o main .

Build Docker Image

See the following:

  1. # Start from the latest golang base image
  2. FROM golang:1.14 as Builder
  3. RUN GOCACHE=OFF
  4. RUN go env -w GOPRIVATE=github.com/appleboy
  5. # Set the Current Working Directory inside the container
  6. WORKDIR /app
  7. # Copy everything from the current directory to the Working Directory inside the container
  8. COPY . .
  9. ARG ACCESS_TOKEN
  10. ENV ACCESS_TOKEN=$ACCESS_TOKEN
  11. RUN git config --global url."https://appleboy:${ACCESS_TOKEN}@github.com".insteadOf "https://github.com"
  12. # Build the Go app
  13. RUN go build -o main .
  14. FROM scratch
  15. COPY --from=Builder /app/main /
  16. CMD ["/main"]

build image using Drone and get docker access token in security section of setting.

  1. - name: build-image
  2. image: plugins/docker
  3. environment:
  4. ACCESS_TOKEN:
  5. from_secret: access_token
  6. settings:
  7. username:
  8. from_secret: username
  9. password:
  10. from_secret: password
  11. repo: appleboy/golang-module-private
  12. build_args_from_env:
  13. - ACCESS_TOKEN