Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

  • A+
所属分类:Linux  自动化运维
摘要

简单介绍一下如何使用 GitLab-CI 来自动创建 Docker 镜像, 并上传到仓库.

环境介绍:

说明 节点 ip 系统
Gitlab Server git.ds.com 10.0.1.179 CentOS 7.5.1804
Gitlab Runner 10.0.1.178 CentOS 7.5.1804
Docker Registry Harbor harbor.ds.com 10.0.1.183 CentOS 7.5.1804

关于 gitlab server和Harbor的安装这里就表述了。

一、gitlab runner的安装 (gitlab 10版本的)

1、根据具体的系统环境下载对应的二进制执行文件

# Linux x86-64
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Linux x86
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

# Linux arm
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm

通过下面的地址你可以找到左右版本的二进制文件

https://docs.gitlab.com/runner/install/bleeding-edge.html#download-any-other-tagged-release

2、给刚下载的二进制执行文件添加执行权限

sudo chmod +x /usr/local/bin/gitlab-runner

3、安装docker

curl -sSL https://get.docker.com/ | sh

4、创建一个gitlab ci用户

useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

5、将gitlab ci用户添加到docker运行用户的组里面

usermod -aG root gitlab-runner

由于我的docker是用root用户运行的所以我这边将gitlab-runner用户加到root的组里面了。
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

6、开始运行GitLab-Runner

gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start

二、注册Runners

1、打开gitlab页面找到runners的注册令牌
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

2、去gitlab runner的节点上做注册动作

gitlab-runner register
[root@DS-VM-Node_10_0_1_178 ~]$ gitlab-runner register
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://git.ds.com
Please enter the gitlab-ci token for this runner:
Fx1jQzBW5ZJoxz157KEU
Please enter the gitlab-ci description for this runner:
[DS-VM-Node_10_0_1_178.dwhd.org]:
Please enter the gitlab-ci tags for this runner (comma separated):
myrunner_10_0_1_178_2
Whether to run untagged builds [true/false]:
[false]: true
Whether to lock the Runner to current project [true/false]:
[true]: true
Registering runner... succeeded                     runner=Fx1jQzBW
Please enter the executor: docker-ssh+machine, kubernetes, docker-ssh, shell, virtualbox, docker+machine, docker, parallels, ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@DS-VM-Node_10_0_1_178 ~]$

Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

3、重启一下runner使之生效

gitlab-runner restart

4、去gitlab上检查
能发现到一个刚注册的runner
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

5、将这个runner指定到指定项目上去
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

三、测试

为了写文章我这次新建了一个gitlab项目。

[root@DS-VM-Node_10_0_1_178 /data]$ git clone http://git.ds.com/lookback/docker-nginx.git
正克隆到 'docker-nginx'...
Username for 'http://git.ds.com': lookback
Password for 'http://lookback@git.ds.com':
warning: 您似乎克隆了一个空版本库。
[root@DS-VM-Node_10_0_1_178 /data]$ mv docker-nginx1/* docker-nginx/
[root@DS-VM-Node_10_0_1_178 /data]$ ls -lh docker-nginx/
总用量 52K
-rw-r--r-- 1 root root 2.7K 6月   9 12:19 Block_Injections.conf
-rw-r--r-- 1 root root 6.1K 6月   9 12:19 conf.tar
-rw-r--r-- 1 root root 2.0K 6月   9 12:19 docker-compose-nginx-php-mariadb-redis.yml
-rw-r--r-- 1 root root 1.2K 6月   9 12:19 docker-compose-nginx-php-mysql.yml
-rw-r--r-- 1 root root  718 6月   9 12:19 docker-compose-nginx-php.yml
-rw-r--r-- 1 root root  517 6月   9 12:19 docker-compose-stream.yml
-rw-r--r-- 1 root root  282 6月   9 12:19 docker-compose.yml
-rw-r--r-- 1 root root 4.3K 6月   9 12:19 Dockerfile
-rwxr-xr-x 1 root root 3.6K 6月   9 12:19 entrypoint.sh
drwxr-xr-x 5 root root   96 6月   9 12:19 etc
-rw-r--r-- 1 root root 4.4K 6月   9 12:19 README.md
[root@DS-VM-Node_10_0_1_178 /data]$ cd docker-nginx/
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git add .
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git commit -m 'Frist update'
[master(根提交) d7c6a42] Frist update
 23 files changed, 856 insertions(+)
 create mode 100644 Block_Injections.conf
 create mode 100644 Dockerfile
 create mode 100644 README.md
 create mode 100644 conf.tar
 create mode 100644 docker-compose-nginx-php-mariadb-redis.yml
 create mode 100644 docker-compose-nginx-php-mysql.yml
 create mode 100644 docker-compose-nginx-php.yml
 create mode 100644 docker-compose-stream.yml
 create mode 100644 docker-compose.yml
 create mode 100755 entrypoint.sh
 create mode 100644 etc/logrotate.d/supervisord
 create mode 100644 etc/nginx-conf-example/block_injections.conf
 create mode 100644 etc/nginx-conf-example/nginx.conf
 create mode 100644 etc/nginx-conf-example/rewrite/discuz.conf
 create mode 100644 etc/nginx-conf-example/rewrite/drupal.conf
 create mode 100644 etc/nginx-conf-example/rewrite/ecshop.conf
 create mode 100644 etc/nginx-conf-example/rewrite/joomla.conf
 create mode 100644 etc/nginx-conf-example/rewrite/opencart.conf
 create mode 100644 etc/nginx-conf-example/rewrite/typecho.conf
 create mode 100644 etc/nginx-conf-example/rewrite/wordpress.conf
 create mode 100644 etc/supervisord.conf
 create mode 100644 etc/supervisord.d/crond.ini
 create mode 100644 etc/supervisord.d/nginx.ini
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git push
Username for 'http://git.ds.com': lookback
Password for 'http://lookback@git.ds.com':
fatal: Authentication failed for 'http://git.ds.com/lookback/docker-nginx.git/'
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git push
Username for 'http://git.ds.com': lookback
Password for 'http://lookback@git.ds.com':
Counting objects: 30, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (30/30), 18.65 KiB | 0 bytes/s, done.
Total 30 (delta 2), reused 0 (delta 0)
To http://git.ds.com/lookback/docker-nginx.git
 * [new branch]      master -> master
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$

Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

2、配置项目runner的变量
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

3、配置.gitlab-ci.yml文件

[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ cat .gitlab-ci.yml
# This file is a template, and might need editing before it works on your project.
# Official docker image.

stages:
  - build_image
  - push_image

before_script:
  - docker info
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

build_image:
  stage: build_image
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .

push_image:
  stage: push_image
  script:
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
    - docker rmi "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git add .
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git commit -m 'Add .gitlab-ci.yml'
[master e79c41f] Add .gitlab-ci.yml
 1 file changed, 22 insertions(+)
 create mode 100644 .gitlab-ci.yml
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$ git push
Username for 'http://git.ds.com': lookback
Password for 'http://lookback@git.ds.com':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 527 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://git.ds.com/lookback/docker-nginx.git
   d7c6a42..e79c41f  master -> master
[root@DS-VM-Node_10_0_1_178 /data/docker-nginx]$

Video_2018-06-09_123016

Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里
Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry里

lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: