gitlab CI环境搭建

2019-01-27  本文已影响22人  奶爸撸代码

gitlab CI环境搭建

摘要:主要记录基于docker 的gitlabci环境搭建步骤和示例,顺带介绍相关的基本概念.

基本概念

From wiki

In software Pipeline
一次 Pipeline 其实相当于一次构建任务,里面可以包含多个流程,如安装依赖、运行测试、编译、部署测试服务器、部署生产服务器等流程。engineering, continuous integration (CI) is the practice of merging all developer working copies to a shared mainline several times a day.Each check-in is then verified by an automated build, allowing teams to detect problems early.

prerequisites

本文主要关注基于docker(k8s暂未搭建)搭建gitlab CI环境. 需要先搭建好docker(或k8s)相关环境:

Install gitlab

sudo docker pull gitlab/gitlab-ce:latest
sudo docker run --detach \
    --hostname localhost \
    --network=host \
    --publish 443:443 --publish 80:80 --publish 22:22 \
    --name gitlab \
    --restart always \
    --volume /tmp/gitlab/config:/etc/gitlab \
    --volume /tmp/gitlab/logs:/var/log/gitlab \
    --volume /tmp/gitlab/data:/var/opt/gitlab \
    gitlab/gitlab-ce:latest

# if error occurred: 
# Failed asserting that mode permissions on "/var/opt/gitlab/git-data/repositories" is 2770
mkdir -p /tmp/gitlab/data/git-data/repositories
sudo chmod g+s /tmp/gitlab/data/git-data/repositories
docker exec -it gitlab update-permissions

参数说明:
--hostname: 设置容器的hostname
--network: 设置容器运行的网络模式, 此处为host模式, 主机网络.
--publish-p: 端口映射转发规则(本地80:容器Http访问端口, 本地443: Https访问端口, 本地端口22: ssh)
--name:容器名称
--restart always:crash或机器启动后总要重启
--volume or -v: 文件目录或磁盘挂载, (/tmp/gitlab/data: app data,/tmp/gitlab/logs: 日志路径,/tmp/gitlab/config: 配置文件)
-e:配置环境变量

http://localhost:80
初次见面

创建项目

CI pipeline

see gitlabci

Starting from version 8.0, GitLab Continuous Integration (CI) is fully integrated into GitLab itself and is enabled by default on all projects.

配置 CI/CD

增加 .gitlab-ci.yml

image: alpine:latest
lint:
  script: "echo lint"
test:
  script: "echo testing"

extends, stage, services, image, before_script, after_script, artifacts, environment, variables, parellel等, 下篇准备对此做个总结, 其实gitlab官网说明挺详细的.

配置docker gitlab Runner

# https://docs.gitlab.com/runner/install/docker.html
mkdir -p /tmp/gitlab-runner/config
docker pull gitlab/gitlab-runner
docker run -d --network=host --name gitlab-runner --restart always \
  -v /tmp/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest
  
# register a runner interactively, 交互式注册
docker exec -it gitlab-runner /bin/bash
gitlab-runner register --help
gitlab-runner register

# 注册填写url/token/name/executer/tag等, 从项目settings拷贝url跟toker创建一个专有runner
copy url && token from http://gitlab.test.com/devops/citest/settings/ci_cd
# http://192.168.1.101/admin/runners
# docker中不要用localhost
# executor 这一步使用 docker

# The token can be found in /etc/gitlab-runner/config.toml
grep "token" /etc/gitlab-runner/config.toml

# stop
docker stop gitlab-runner && docker rm gitlab-runner
docker run --rm -t -i -v /tmp/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
  --non-interactive \
  --executor "docker" \
  --docker-image alpine:latest \
  --url "https://gitlab.test.com/" \
  --registration-token "PROJECT_REGISTRATION_TOKEN" \
  --description "docker-runner" \
  --tag-list "docker" \
  --run-untagged \
  --locked="false"``

注意: url与token来自这里:

settings->CI/CD->Specific Runners

# deployment.yml

# docker run --rm -t -i alpine sh -c "apk add --no-cache curl; curl -X POST -I http://gitlab.test.com/api/v4/runners"
#!/bin/bash -x
# docker run --rm -t -i -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
# One-line registration command
token="Y9xTAzfiGmvWjGjSP9SV"
if [ $# -gt 1 ];then
    token=$1
fi

docker run --rm -t -i --network=host -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
  --non-interactive \
  --url "http://gitlab.test.com/" \
  --registration-token ${token} \
  --description "docker-search-engine-runner" \
  --executor "docker" \
  --tag-list "sfd,tsfd,gpu" \
  --run-untagged \
  --locked="false" \
  --docker-image alpine:latest

tips

references

上一篇下一篇

猜你喜欢

热点阅读