Docker环境下: Gitlab + Gitlab-runner 构建 Gitlab CI/CD

一.Docker安装gitlab:
https://docs.gitlab.com/omnibus/docker/
步骤:
a) export GITLAB_HOME=/srv/gitlab
b)完成此步骤就已经运行,后面几步是注意事项

sudo docker run --detach \
  --hostname gitlab.example.com \
  --publish 443:443 --publish 80:80 --publish 22:222 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  gitlab/gitlab-ee:latest

c) 注意:映射关系
The GitLab container uses host mounted volumes to store persistent data:

Local location			Container location		Usage
$GITLAB_HOME/data		/var/opt/gitlab			For storing application data.
$GITLAB_HOME/logs		/var/log/gitlab			For storing logs.
$GITLAB_HOME/config		/etc/gitlab				For storing the GitLab configuration files.

d) 修改docker内gitlab配置:首先 docker exec -it gitlab bash , 修改:/etc/gitlab/gitlab.rb
e) 如果页面一直502,建议等几分钟或内存分配大一点(8G试试)
f) 启动后:gitlab.example.com (or http://192.168.59.103(宿主机ip),上面设置的). 第一次访问设置管理员密码后,用root登录。

二.Docker安装gitlab-runner:
https://docs.gitlab.com/runner/install/docker.html
安装步骤:
a)

docker run -d --name gitlab-runner --restart always \
     -v /srv/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:alpine

b) 查看gitlab自己项目的【Settings > CI/CD】页面,此处有密钥和路径以及安装方法
注册步骤:https://docs.gitlab.com/runner/register/
c) 第一种注册:首先 docker exec -it gitlab-runner bash , 然后:gitlab-runner register,最后根据提示输入。
d) 第二种注册:

docker run --rm -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
  --non-interactive \
  --executor "docker" \
  --docker-image alpine:latest \
  --url "https://gitlab.com/" \
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --description "docker-runner" \
  --tag-list "docker" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected"

e) 在docker容器中查看是否激活已注册:gitlab-runner verify(d没搞定,就用c)
f) 最后还是没搞定,注意下你的配置文件,需要使用root权限的容器改**–privileged=true**,容器镜像可参考**–docker-image: ruby:2.6**等等

三.至此, gitlab CI/CD 基本环境有了

[root@localhost docker]# docker images
REPOSITORY                    TAG               IMAGE ID       CREATED        SIZE
gitlab/gitlab-runner-helper   x86_64-7a6612da   1fe5eabc2f9a   18 hours ago   70.3MB
gitlab/gitlab-ee              latest            cede2a703552   6 days ago     2.44GB
gitlab/gitlab-runner          latest            6965be4d8032   11 days ago    819MB
ruby                          2.6               7e85f351a741   2 weeks ago    840MB

四.准备【.gitlab-ci.yml】文件
https://docs.gitlab.com/ee/ci/quick_start/
下面是yml内容,相对于官方示例,我加了 【tags: -docker】,上述设置的【–tag-list “docker”】

build-job:
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"
  tags:
    - docker

test-job1:
  stage: test
  script:
    - echo "This job tests something"
  tags:
    - docker

test-job2:
  stage: test
  script:
    - echo "This job tests something, but takes more time than test-job1."
    - echo "After the echo commands complete, it runs the sleep command for 20 seconds"
    - echo "which simulates a test that runs 20 seconds longer than test-job1"
    - sleep 20
  tags:
    - docker

deploy-prod:
  stage: deploy
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."#
  tags:
    - docker

五.在自己gitlab项目中【CI/CD->Pipelines】查看效果
Docker环境下: Gitlab + Gitlab-runner 构建 Gitlab CI/CD_第1张图片

六.最后研究【.gitlab-ci.yml】语法配置吧:
https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html

你可能感兴趣的:(Git,docker,gitlab,ci/cd,centos)