一、需求来源
二、相关概念
2.1 Pipeline
2.2 Stages
2.3 Jobs
2.4 GitLab Runner
2.4.1 简介
2.4.2 安装
# For Debian/Ubuntu
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
$ sudo apt-get install gitlab-ci-multi-runner
# For CentOS
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
$ sudo yum install gitlab-ci-multi-runner
2.4.3 注册 Runner
运行 sudo gitlab-ci-multi-runner register
输入 CI URL
输入 Token
输入 Runner 的名字
选择 Runner 的类型,简单起见还是选 Shell 吧
完成
gitlab-runner register --name A --url http://B/ --registration-token C
WARNING: Running in user-mode.
WARNING: The user-mode requires you to manually start builds processing:
WARNING: $ gitlab-runner run
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
[http://B/]:
Please enter the gitlab-ci token for this runner:
[C]:
Please enter the gitlab-ci description for this runner:
[A]:
Please enter the gitlab-ci tags for this runner (comma separated):
A
Whether to run untagged builds [true/false]:
[false]:
Whether to lock Runner to current project [true/false]:
[false]:
Registering runner... succeeded runner=S
Please enter the executor: shell, virtualbox, docker+machine, docker-ssh+machine, kubernetes, docker, docker-ssh, parallels, ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
$ sudo gitlab-runner list
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
my-runner Executor=shell Token=cd1cd7cf243afb47094677855aacd3 URL=http://mygitlab.com/ci
$ sudo gitlab-runner verify
Running in system-mode.
Verifying runner... is alive runner=5_tVnBmx
三、编写.gitlab-ci.yml
3.1 基本写法
# 定义 stages
stages:
- build
- test
# 定义 job
job1:
stage: test
script:
- echo "I am job1"
- echo "I am in test stage"
# 定义 job
job2:
stage: build
script:
- echo "I am job2"
- echo "I am in build stage"
I am job2
I am in build stage
I am job1
I am in test stage
3.2 实战:实现代码同步
# .gitlab-ci.yml 内容如下
stages:
- update
update:
stage: update
script:
- echo "Now, CI is begin"
- /A/update.sh
- echo "Now, CI is end!"
only:
- dev
tags:
- update
#!/bin/bash
cd /A/
git pull --rebase;
cp -rf B/* /C/
insufficient permission for adding an object to repository database .git/objects
四、配置SSH实现免密登录
4.1 添加变量
SSH_USER = root
SSH_HOST = 服务器ip
SSH_KNOWN_HOSTS = 文件 ~/.ssh/known_host 有你服务器ip的一行
SSH_PRIVATE_KEY = 文件 ~/.ssh/id_rsa 里的内容
4.2 编辑.gitlab-ci.yml
stage:
- Deploy
Deplpy:
stage: Deploy
only:
- master
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
when: manual #手动确认之后才能构建
script:
- pwd
- ls -l
#主要同步代码的命令,可以在这里排除一些文件,同步权限,配置服务器的项目路径等。重点参考rsync命令的用法。
- rsync -aztp --exclude ".gitlab-ci.yml" ./ $SSH_USER@$SSH_HOST:/xxx
微信号 | sagewang666
微信公众号 | 小王学长的笔记
文章转载自小王学长的笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。