在之前编写过CI与Gitlab的整合应用,下来主要详细的介绍使用Gitlab工具的CI的可持续应用。搭建好Gitlab的环境好后,我们需要在Linux的环境安装Gitlab的插件gitlab-ci,安装命令为:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | bash
这些后,就会把该插件安装成功,输出信息主要为:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7012 100 7012 0 0 6080 0 0:00:01 0:00:01 --:--:-- 6081
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
runner_gitlab-ci-multi-runner-source/signature | 862 B 00:00:00
runner_gitlab-ci-multi-runner-source/signature | 951 B 00:00:00 !!!
updates | 2.9 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(2/2): epel/x86_64/primary_db | 6.9 MB 00:00:00
Package pygpgme-0.3-9.el7.x86_64 already installed and latest version
Nothing to do
Installing yum-utils...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
Generating yum cache for runner_gitlab-ci-multi-runner...
Generating yum cache for runner_gitlab-ci-multi-runner-source...
The repository is setup! You can now install packages.
下来再进行插件的安装,安装的命令为:
yum install gitlab-ci-multi-runner -y
执行后,输出的信息为:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
runner_gitlab-ci-multi-runner/x86_64/signature | 862 B 00:00:00
runner_gitlab-ci-multi-runner/x86_64/signature | 1.0 kB 00:00:00 !!!
runner_gitlab-ci-multi-runner-source/signature | 862 B 00:00:00
runner_gitlab-ci-multi-runner-source/signature | 951 B 00:00:00 !!!
Package gitlab-ci-multi-runner-9.5.1-1.x86_64 already installed and latest version
下来查看gitlab-ci-multi-runner是否可以正常的启动以及它的状态,具体如下:
[root@iz2ze4dcz1c36xtn6io522z ~]# gitlab-ci-multi-runner restart
[root@iz2ze4dcz1c36xtn6io522z ~]# gitlab-ci-multi-runner status
gitlab-runner: Service is running!
下来进行gitlab-ci的注册,注册需要获取到具体的URL和TOKEN的信息,步骤为:
下来在服务端操作CI的注册,操作命令为:
gitlab-ci-multi-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://47.95.142.233/
Please enter the gitlab-ci token for this runner:
9pzo1oicss-T6f7nVz_Q
Please enter the gitlab-ci description for this runner:
[iz2ze4dcz1c36xtn6io522z]:
Please enter the gitlab-ci tags for this runner (comma separated):
test
Whether to run untagged builds [true/false]:
[false]:
Whether to lock Runner to current project [true/false]:
[false]:
Registering runner... succeeded runner=9pzo1oic
Please enter the executor: docker, parallels, shell, ssh, docker+machine, docker-ssh, virtualbox, docker-ssh+machine, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
注册成功后,就可以在Gitlab里面进行基于shell的方式来进行CICD的交互了。下面来看是否注册成功,具体如下:
在Gitlab的CICD中也就能看到刚才注册成功的CI的信息了,截图如下:
下来在具体的项目里面增加一个.gitlab-ci.yml的文件来进行,文件内容为:
stages:
- build
- test
job1:
stage: test
script:
- echo "i am job1"
- echo "i am in test stage"
job2:
stage: build
script:
- echo "i am job2"
- echo "i am in build stage"
最后是对文件进行提交和保存,保存后的信息为:
最后再次到项目的CICD里面,就可以看到我们新增的CICD,具体如下:
点击pending可以看到阶段,如下所示:
我们对.gitlab-ci.yml进行完善,新增tags(也就是注册的时候填写的标签),完善后的yaml的文件内容为:
stages:
- test
job1:
stage: test
tags:
- test
script:
- python3 -m pytest -v -s test_login.py
保存后执行的信息为:
From http://47.95.***.***/wuya/saas
e866278..d07b359 master -> origin/master
Checking out d07b3595 as master...
Skipping Git submodules setup
$ python3 -m pytest -v -s test_login.py
============================= test session starts ==============================
platform linux -- Python 3.7.1, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Linux-3.10.0-1160.31.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core', 'Packages': {'pytest': '5.4.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.9.43', 'html': '2.0.1', 'forked': '1.1.3', 'xdist': '1.31.0', 'metadata': '1.8.0'}, 'CI': 'true', 'CI_COMMIT_REF_NAME': 'master', 'CI_COMMIT_REF_SLUG': 'master', 'CI_COMMIT_SHA': 'd07b3595dc8603d9bf483d1c34306827e20b2401', 'CI_JOB_ID': '145', 'CI_JOB_NAME': 'job1', 'CI_JOB_STAGE': 'test', 'CI_JOB_TOKEN': 'xxxxxxxxxxxxxxxxxxxx', 'CI_REPOSITORY_URL': 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@47.95.142.233/wuya/saas.git', 'CI_RUNNER_DESCRIPTION': 'iz2ze4dcz1c36xtn6io522z', 'CI_RUNNER_ID': '5', 'CI_RUNNER_TAGS': 'test', 'CI_PIPELINE_ID': '58', 'CI_PROJECT_DIR': '/home/gitlab-runner/builds/mizQHJ8x/0/wuya/saas', 'CI_PROJECT_ID': '14', 'CI_PROJECT_NAME': 'saas', 'CI_PROJECT_NAMESPACE': 'wuya', 'CI_PROJECT_PATH': 'wuya/saas', 'CI_PROJECT_URL': 'http://47.95.142.233/wuya/saas', 'CI_REGISTRY_PASSWORD': 'xxxxxxxxxxxxxxxxxxxx', 'CI_REGISTRY_USER': 'gitlab-ci-token', 'CI_SERVER': 'yes', 'CI_SERVER_NAME': 'GitLab', 'CI_SERVER_REVISION': '1a9fd38a4ca', 'CI_SERVER_VERSION': '12.0.2', 'GITLAB_CI': 'true', 'GITLAB_USER_ID': '25', 'GITLAB_USER_EMAIL': '2839168630@qq.com', 'JAVA_HOME': '/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252'}
rootdir: /home/gitlab-runner/builds/mizQHJ8x/0/wuya/saas
plugins: allure-pytest-2.9.43, html-2.0.1, forked-1.1.3, xdist-1.31.0, metadata-1.8.0
collecting ... collected 2 items
test_login.py::test_login_001 PASSED
test_login.py::test_login_002 PASSED
============================== 2 passed in 0.02s ===============================
Job succeeded