Merge pull request #8629 from geekpi/master

translated
This commit is contained in:
geekpi 2018-04-28 08:39:36 +08:00 committed by GitHub
commit e90ebd7f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 155 additions and 157 deletions

View File

@ -1,157 +0,0 @@
translating----geekpi
BUILDING GO PROJECTS WITH DOCKER ON GITLAB CI
===============================================
### Intro
This post is a summary of my research on building Go projects in a Docker container on CI (Gitlab, specifically). I found solving private dependencies quite hard (coming from a Node/.NET background) so that is the main reason I wrote this up. Please feel free to reach out if there are any issues or a submit pull request on the Docker image.
### Dep
As dep is the best option for managing Go dependencies right now, the build will need to run `dep ensure` before building.
Note: I personally do not commit my `vendor/` folder into source control, if you do, Im not sure if this step can be skipped or not.
The best way to do this with Docker builds is to use `dep ensure -vendor-only`. [See here][1].
### Docker Build Image
I first tried to use `golang:1.10` but this image doesnt have:
* curl
* git
* make
* dep
* golint
I have created my own Docker image for builds ([github][2] / [dockerhub][3]) which I will keep up to date - but I offer no guarantees so you should probably create and manage your own.
### Internal Dependencies
Were quite capable of building any project that has publicly accessible dependencies so far. But what about if your project depends on another private gitlab repository?
Running `dep ensure` locally should work with your git setup, but once on CI this doesnt apply and builds will fail.
### Gitlab Permissions Model
This was [added in Gitlab 8.12][4] and the most useful feature we care about is the `CI_JOB_TOKEN` environment variable made available during builds.
This basically means we can clone [dependent repositories][5] like so
```
git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/myuser/mydependentrepo
```
However we do want to make this a bit more user friendly as dep will not magically add credentials when trying to pull code.
We will add this line to the `before_script` section of the `.gitlab-ci.yml`.
```
before_script:
- echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
```
Using the `.netrc` file allows you to specify which credentials to use for which server. This method allows you to avoid entering a username and password every time you pull (or push) from Git. The password is stored in plaintext so you shouldnt do this on your own machine. This is actually for `cURL` which Git uses behind the scenes. [Read more here][6].
Project Files
============================================================
### Makefile
While this is optional, I have found it makes things easier.
Configuring these steps below means in the CI script (and locally) we can run `make lint`, `make build` etc without repeating steps each time.
```
GOFILES = $(shell find . -name '*.go' -not -path './vendor/*')
GOPACKAGES = $(shell go list ./... | grep -v /vendor/)
default: build
workdir:
mkdir -p workdir
build: workdir/scraper
workdir/scraper: $(GOFILES)
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o workdir/scraper .
test: test-all
test-all:
@go test -v $(GOPACKAGES)
lint: lint-all
lint-all:
@golint -set_exit_status $(GOPACKAGES)
```
### .gitlab-ci.yml
This is where the Gitlab CI magic happens. You may want to swap out the image for your own.
```
image: sjdweb/go-docker-build:1.10
stages:
- test
- build
before_script:
- cd $GOPATH/src
- mkdir -p gitlab.com/$CI_PROJECT_NAMESPACE
- cd gitlab.com/$CI_PROJECT_NAMESPACE
- ln -s $CI_PROJECT_DIR
- cd $CI_PROJECT_NAME
- echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
- dep ensure -vendor-only
lint_code:
stage: test
script:
- make lint
unit_tests:
stage: test
script:
- make test
build:
stage: build
script:
- make
```
### What This Is Missing
I would usually be building a Docker image with my binary and pushing that to the Gitlab Container Registry.
You can see Im building the binary and exiting, you would at least want to store that binary somewhere (such as a build artifact).
--------------------------------------------------------------------------------
via: https://seandrumm.co.uk/blog/building-go-projects-with-docker-on-gitlab-ci/
作者:[ SEAN DRUMM][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://seandrumm.co.uk/
[1]:https://github.com/golang/dep/blob/master/docs/FAQ.md#how-do-i-use-dep-with-docker
[2]:https://github.com/sjdweb/go-docker-build/blob/master/Dockerfile
[3]:https://hub.docker.com/r/sjdweb/go-docker-build/
[4]:https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html
[5]:https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html#dependent-repositories
[6]:https://github.com/bagder/everything-curl/blob/master/usingcurl-netrc.md

View File

@ -0,0 +1,155 @@
在 GITLAB CI 中使用 DOCKER 构建 GO 项目
===============================================
### 介绍
这篇文章是我在 CI 的 Docker 容器中构建 Go 项目的研究总结(特别是在 Gitlab 中)。我发现很难解决私有依赖问题(来自 Node/.NET 背景),因此这是我写这篇文章的主要原因。如果 Docker 镜像上存在任何问题或提交请求,请随时与我们联系。
### Dep
由于 dep 是现在管理 Go 依赖关系的最佳选择,因此在构建前之前运行 `dep ensure`
注意:我个人不会将我的 `vendor/`  文件夹提交到源码控制,如果你这样做,我不确定这个步骤是否可以跳过。
使用 Docker 构建的最好方法是使用 `dep ensure -vendor-only`。 [见这里][1]。
### Docker 构建镜像
我第一次尝试使用  `golang:1.10`,但这个镜像没有:
* curl
* git
* make
* dep
* golint
我已经为我将不断更新的构建创建好了镜像([github][2] / [dockerhub][3] - 但我不提供任何保证,因此你应该创建并管理自己的 Dockerhub。
### 内部依赖关系
我们完全有能力创建一个有公共依赖关系的项目。但是如果你的项目依赖于另一个私人 gitlab 仓库呢?
在本地运行 `dep ensure` 应该可以和你的 git 设置一起工作,但是一旦在 CI 上不适用,构建就会失败。
### Gitlab 权限模型
这是在[ Gitlab 8.12 中添加的][4],我们关心的最有用的功能是在构建期提供的 `CI_JOB_TOKEN` 环境变量。
这基本上意味着我们可以像这样克隆[依赖仓库][5]
```
git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/myuser/mydependentrepo
```
然而,我们希望使这更友好一点,因为 dep 在试图拉取代码时不会奇迹般地添加凭据。
我们将把这一行添加到 `.gitlab-ci.yml``before_script` 部分。
```
before_script:
- echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
```
使用 `.netrc` 文件可以指定哪个凭证用于哪个服务器。这种方法可以避免每次从 Git 中拉取(或推送)时输入用户名和密码。密码以明文形式存储,因此你不应在自己的计算机上执行此操作。这实际用于 Git 在背后使用  `cURL`。 [在这里阅读更多][6]。
项目文件
============================================================
### Makefile
虽然这是可选的,但我发现它使事情变得更容易。
配置这些步骤意味着在 CI 脚本(和本地)中,我们可以运行 `make lint`、`make build` 等,而无需每次重复步骤。
```
GOFILES = $(shell find . -name '*.go' -not -path './vendor/*')
GOPACKAGES = $(shell go list ./... | grep -v /vendor/)
default: build
workdir:
mkdir -p workdir
build: workdir/scraper
workdir/scraper: $(GOFILES)
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o workdir/scraper .
test: test-all
test-all:
@go test -v $(GOPACKAGES)
lint: lint-all
lint-all:
@golint -set_exit_status $(GOPACKAGES)
```
### .gitlab-ci.yml
这是 Gitlab CI 魔术发生的地方。你可能想使用自己的镜像。
```
image: sjdweb/go-docker-build:1.10
stages:
- test
- build
before_script:
- cd $GOPATH/src
- mkdir -p gitlab.com/$CI_PROJECT_NAMESPACE
- cd gitlab.com/$CI_PROJECT_NAMESPACE
- ln -s $CI_PROJECT_DIR
- cd $CI_PROJECT_NAME
- echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
- dep ensure -vendor-only
lint_code:
stage: test
script:
- make lint
unit_tests:
stage: test
script:
- make test
build:
stage: build
script:
- make
```
### 缺少了什么
我通常会用我的二进制文件构建 Docker 镜像,并将其推送到 Gitlab 容器注册器中。
你可以看到我正在构建二进制文件并退出,你至少需要将该二进制文件(例如生成文件)存储在某处。
--------------------------------------------------------------------------------
via: https://seandrumm.co.uk/blog/building-go-projects-with-docker-on-gitlab-ci/
作者:[ SEAN DRUMM][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://seandrumm.co.uk/
[1]:https://github.com/golang/dep/blob/master/docs/FAQ.md#how-do-i-use-dep-with-docker
[2]:https://github.com/sjdweb/go-docker-build/blob/master/Dockerfile
[3]:https://hub.docker.com/r/sjdweb/go-docker-build/
[4]:https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html
[5]:https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html#dependent-repositories
[6]:https://github.com/bagder/everything-curl/blob/master/usingcurl-netrc.md