前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >源码编译 GitLab Runner

源码编译 GitLab Runner

作者头像
soulteary
发布2019-09-28 18:25:14
1.5K0
发布2019-09-28 18:25:14
举报
文章被收录于专栏:为了不折腾而去折腾的那些事

本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 署名 4.0 国际 (CC BY 4.0)

本文作者: 苏洋

创建时间: 2019年08月04日 统计字数: 10252字 阅读时间: 21分钟阅读 本文链接: https://soulteary.com/2019/08/04/source-code-compilation-gitlab-runner.html

源码编译 GitLab Runner

从源码安装 GitLab 你或许听说过,但是从源码安装 GitLab Runner ,或许这将是你听到的第一篇相关博客。

最近遇到一个问题,需要手动编译构建 GitLab Runner,而官方文档陈旧、命令过时,如果按照官方错误的指引搞下去,难免会浪费很多时间,而且得不到你想要的结果。

如果你也有类似需求,跟随本篇文章,大概十分钟左右就能折腾出一个属于你自己的 GitLab Runner。

前置准备

这次的前置准备真的不多,大概就需要两样:

  • 一台虚拟机,我选择的操作系统是 Ubuntu18.0464位
  • 一颗耐心

在编译 GitLab Runner 之前,我们需要制作编译工具,而编译工具依赖定制的系统环境,所以第一步就是简单折腾一个系统环境。

准备系统环境

安装基础环境的第一步,无非是升级系统环境、安装必要依赖。

这里推荐安装 git 依赖,而非官方文档中注明的 git-core ,详细原因见这篇文章。

代码语言:javascript
复制
apt update && apt upgrade -yapt install software-properties-common git mercurial unzip vim binfmt-support qemu-user-static -y

与其他软件不同的是,这里必须安装 Docker ,因为后面的构建过程中会使用到 Docker (哪怕你编译的不是 Runner 的 Docker 镜像)。

截止文章发布,使用 Dockerversion19.03.1 并无任何异常。如果你使用的是阿里云,可以使用下面的命令进行安装,更详细的内容,可以参考之前的文章。

代码语言:javascript
复制
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"apt install -y docker-ce

由于软件是使用 Go 语言编写,所以安装 Go 语言运行时必不可少。但可惜的是,我们无法使用最新的 Go1.12,必须老老实实使用 2017 年推出的 1.8.7

代码语言:javascript
复制
wget https://storage.googleapis.com/golang/go1.8.7.linux-amd64.tar.gzsudo tar -C /usr/local -xzf go*-*.tar.gzecho "export GOPATH=$HOME/Go" >> ~/.profileecho "export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin" >> ~/.profilesource ~/.profile

接下来需要做的事情是获取软件包源码、以及开发需要使用的依赖包。

代码语言:javascript
复制
go get gitlab.com/gitlab-org/gitlab-runnercd $GOPATH/src/gitlab.com/gitlab-org/gitlab-runner/go get -u github.com/jteeuwen/go-bindata/...

准备编译工具

在上述命令都执行完毕,且没有报错的情况下,继续执行下面的语句,就能获得可用的编译工具啦。

代码语言:javascript
复制
make helper-build helper-docker

如果你比较顺利,将会看到类似下面的日志:

代码语言:javascript
复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* # make helper-build helper-dockergo get github.com/mitchellh/goxgox -osarch=windows/amd64 -ldflags "-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=12.2.0~beta.1803.g41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2019-08-01T16:59:49+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH=master -s -w" -output=dockerfiles/build/binaries/gitlab-runner-helper.x86_64-windows gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helperNumber of parallel builds: 7-->   windows/amd64: gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helpergox -osarch=linux/amd64 -ldflags "-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=12.2.0~beta.1803.g41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2019-08-01T16:59:49+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH=master -s -w" -output=dockerfiles/build/binaries/gitlab-runner-helper.x86_64 gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helperNumber of parallel builds: 7-->     linux/amd64: gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helpergox -osarch=linux/arm -ldflags "-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=12.2.0~beta.1803.g41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2019-08-01T16:59:49+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH=master -s -w" -output=dockerfiles/build/binaries/gitlab-runner-helper.arm gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helperNumber of parallel builds: 7-->       linux/arm: gitlab.com/gitlab-org/gitlab-runner/apps/gitlab-runner-helperdocker build -t gitlab/gitlab-runner-helper:x86_64-41d5c6ad -f dockerfiles/build/Dockerfile.x86_64 dockerfiles/buildSending build context to Docker daemon  40.32MBStep 1/6 : FROM alpine:3.93.9: Pulling from library/alpinee7c96db7181b: Pull completeDigest: sha256:7746df395af22f04212cd25a92c1d6dbc5a06a0ca9579a229ef43008d4d1302aStatus: Downloaded newer image for alpine:3.9 ---> 055936d39205Step 2/6 : RUN apk add --no-cache bash ca-certificates git git-lfs miniperl     && ln -s miniperl /usr/bin/perl ---> Running in 93e1c5a12b93fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz(1/14) Installing ncurses-terminfo-base (6.1_p20190105-r0)(2/14) Installing ncurses-terminfo (6.1_p20190105-r0)(3/14) Installing ncurses-libs (6.1_p20190105-r0)(4/14) Installing readline (7.0.003-r1)(5/14) Installing bash (4.4.19-r1)Executing bash-4.4.19-r1.post-install(6/14) Installing ca-certificates (20190108-r0)(7/14) Installing nghttp2-libs (1.35.1-r0)(8/14) Installing libssh2 (1.8.2-r0)(9/14) Installing libcurl (7.64.0-r2)(10/14) Installing expat (2.2.7-r0)(11/14) Installing pcre2 (10.32-r1)(12/14) Installing git (2.20.1-r0)(13/14) Installing git-lfs (2.5.1-r2)Executing git-lfs-2.5.1-r2.post-installGit LFS initialized.(14/14) Installing miniperl (5.26.3-r0)Executing busybox-1.29.3-r10.triggerExecuting ca-certificates-20190108-r0.triggerOK: 42 MiB in 28 packagesRemoving intermediate container 93e1c5a12b93 ---> cf8c23304de7Step 3/6 : RUN git lfs install --skip-repo ---> Running in cdd487902490Git LFS initialized.Removing intermediate container cdd487902490 ---> 995fbdc82d24Step 4/6 : COPY ./scripts/ /usr/bin ---> d1a8aabbcd3cStep 5/6 : COPY ./binaries/gitlab-runner-helper.x86_64 /usr/bin/gitlab-runner-helper ---> fcb3bbe621aaStep 6/6 : RUN echo 'hosts: files dns' >> /etc/nsswitch.conf ---> Running in d5a9a71bbe2fRemoving intermediate container d5a9a71bbe2f ---> 73b181e7c05aSuccessfully built 73b181e7c05aSuccessfully tagged gitlab/gitlab-runner-helper:x86_64-41d5c6addocker rm -f gitlab-runner-prebuilt-x86_64-41d5c6adError: No such container: gitlab-runner-prebuilt-x86_64-41d5c6adMakefile.runner_helper.mk:55: recipe for target 'out/helper-images/prebuilt-x86_64.tar' failedmake: [out/helper-images/prebuilt-x86_64.tar] Error 1 (ignored)docker create --name=gitlab-runner-prebuilt-x86_64-41d5c6ad gitlab/gitlab-runner-helper:x86_64-41d5c6ad /bin/shbe14d18aa9153578b35e7c58fce754093b89d6b50228c02baeaa440c753d723adocker export -o out/helper-images/prebuilt-x86_64.tar gitlab-runner-prebuilt-x86_64-41d5c6addocker rm -f gitlab-runner-prebuilt-x86_64-41d5c6adgitlab-runner-prebuilt-x86_64-41d5c6adxz -f -9 out/helper-images/prebuilt-x86_64.tardocker build -t gitlab/gitlab-runner-helper:arm-41d5c6ad -f dockerfiles/build/Dockerfile.arm dockerfiles/buildSending build context to Docker daemon  40.32MBStep 1/6 : FROM multiarch/alpine:armhf-v3.9armhf-v3.9: Pulling from multiarch/alpined2a76f42393b: Pull complete0e48aa23384e: Pull completeDigest: sha256:138eab1a3a8b31f4d0df75a752b5fdda95612505522de9e9f8d2f96d3d97abd9Status: Downloaded newer image for multiarch/alpine:armhf-v3.9 ---> daef464a9e14Step 2/6 : RUN apk add --no-cache bash ca-certificates git git-lfs miniperl     && ln -s miniperl /usr/bin/perl ---> Running in a87dfb239af0fetch https://uk.alpinelinux.org/alpine/v3.9/main/armhf/APKINDEX.tar.gzfetch https://uk.alpinelinux.org/alpine/v3.9/community/armhf/APKINDEX.tar.gz(1/15) Installing ncurses-terminfo-base (6.1_p20190105-r0)(2/15) Installing ncurses-terminfo (6.1_p20190105-r0)(3/15) Installing ncurses-libs (6.1_p20190105-r0)(4/15) Installing readline (7.0.003-r1)(5/15) Installing bash (4.4.19-r1)Executing bash-4.4.19-r1.post-install(6/15) Installing ca-certificates (20190108-r0)(7/15) Installing nghttp2-libs (1.35.1-r0)(8/15) Installing libssh2 (1.8.2-r0)(9/15) Installing libcurl (7.64.0-r2)(10/15) Installing libgcc (8.3.0-r0)(11/15) Installing expat (2.2.7-r0)(12/15) Installing pcre2 (10.32-r1)(13/15) Installing git (2.20.1-r0)(14/15) Installing git-lfs (2.5.1-r2)Executing git-lfs-2.5.1-r2.post-installGit LFS initialized.(15/15) Installing miniperl (5.26.3-r0)Executing busybox-1.29.3-r10.triggerExecuting ca-certificates-20190108-r0.triggerOK: 40 MiB in 34 packagesRemoving intermediate container a87dfb239af0 ---> 919719cb8063Step 3/6 : RUN git lfs install --skip-repo ---> Running in 292da8a82fffGit LFS initialized.Removing intermediate container 292da8a82fff ---> da40dc15d497Step 4/6 : COPY ./scripts/ /usr/bin ---> 65df18912961Step 5/6 : COPY ./binaries/gitlab-runner-helper.arm /usr/bin/gitlab-runner-helper ---> eb4e1e5c8f94Step 6/6 : RUN echo 'hosts: files dns' >> /etc/nsswitch.conf ---> Running in 6478fa57668aRemoving intermediate container 6478fa57668a ---> 9dc701e1cac4Successfully built 9dc701e1cac4Successfully tagged gitlab/gitlab-runner-helper:arm-41d5c6addocker rm -f gitlab-runner-prebuilt-arm-41d5c6adError: No such container: gitlab-runner-prebuilt-arm-41d5c6adMakefile.runner_helper.mk:55: recipe for target 'out/helper-images/prebuilt-arm.tar' failedmake: [out/helper-images/prebuilt-arm.tar] Error 1 (ignored)docker create --name=gitlab-runner-prebuilt-arm-41d5c6ad gitlab/gitlab-runner-helper:arm-41d5c6ad /bin/shd6f6155e534039619d616944528205ec3e388c9eda51787cbae329fcec10ec03docker export -o out/helper-images/prebuilt-arm.tar gitlab-runner-prebuilt-arm-41d5c6addocker rm -f gitlab-runner-prebuilt-arm-41d5c6adgitlab-runner-prebuilt-arm-41d5c6adxz -f -9 out/helper-images/prebuilt-arm.tar
*/

开始构建软件

当上面的步骤都就绪后,就可以正式开始编译构建 GitLab Runner 啦。

代码语言:javascript
复制
source ci/touch_make_dependenciesmake build BUILD_PLATFORMS="-osarch='linux/amd64'"

-osarch 参数支持同时填写多个平台,来达到输出多个平台的可执行文件,比如填写 linux/amd64 darwin/amd64,将输出64位的Linux、OSx 系统的应用软件。

目前支持的平台有:

  • darwin/386 / darwin/amd64
  • freebsd/386 / freebsd/amd64 / freebsd/arm
  • linux/386 / linux/amd64 / linux/arm
  • windows/386 / windows/amd64

如果我们只构建 Linux 64 位系统下的应用,将看到类似下面的日志输出:

代码语言:javascript
复制
# make build BUILD_PLATFORMS="-osarch='linux/amd64'"go get github.com/mitchellh/gox# Building gitlab-runner in version 12.2.0~beta.1803.g41d5c6ad for -osarch='linux/amd64'gox -osarch='linux/amd64' \    -ldflags "-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=12.2.0~beta.1803.g41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=41d5c6ad -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2019-08-01T17:04:48+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH=master -s -w" \    -output="out/binaries/gitlab-runner-{{.OS}}-{{.Arch}}" \    gitlab.com/gitlab-org/gitlab-runnerNumber of parallel builds: 7-->     linux/amd64: gitlab.com/gitlab-org/gitlab-runner

命令执行完毕后,我们可以在 ./out/binaries/ 目录内看到我们想要的二进制程序。试着运行一下吧:

代码语言:javascript
复制
# ./out/binaries/gitlab-runner-linux-amd64 -vVersion:      12.2.0~beta.1803.g41d5c6adGit revision: 41d5c6adGit branch:   masterGO version:   go1.8.7Built:        2019-08-01T17:04:48+0000OS/Arch:      linux/amd64

如果你也能看到类似的输出,那么源码编译 GitLab Runner 的任务,就这么愉快的结束啦。

上述问题解决方案来自项目 .gitlab-ci.yml 持续集成配置文件,感兴趣的同学可以了解下。

最后

《编程匠艺》曾提过不应把过时错误的信息提供给你的伙伴,要维护良好的文档。然而现实中充满了过时错误的信息,就像本例中一样,作为一款开源软件,这些错误的信息难免会浇灭外部贡献者的热情。

下一篇文章,我将讲讲我为什么要编译 GitLab。

—EOF

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 折腾技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 源码编译 GitLab Runner
    • 前置准备
      • 准备系统环境
        • 准备编译工具
          • 开始构建软件
            • 最后
            相关产品与服务
            容器镜像服务
            容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档