关于gitlab-ci-runner:gitlab-CICD配置中遇到的问题

先来说一下配置CI/CD的目标,咱们在提交代码后即提交push申请后gitLab会主动生成一个pipeLine工作,如果咱们想要在每次提交代码之后都跑一次单元测试就须要对其进行编辑,此外咱们还能够在此运行咱们自定义的脚本来实现更多需要,例如向钉钉收回推送。 上面这就是对应的配置文件——.gitlab-ci.yml咱们在此文件中次要用到的一共就只有上面几个关键词: stages:设置运行步骤variables:定义变量tags:设置运行环境script:设置要执行的命令对于stages: stages: -step1 //步骤一 -step2 //步骤二step_one: stage: step1 //申明是属于步骤一 . . .step_two: stage: step1 //申明是属于步骤一. . .step_three stage: step2 //申明是属于步骤二. . .其中step_one和step_two是并行执行的。step_two在其后执行。 对于variables须要揭示的是gitLab有本人内置的变量,咱们能够间接进行应用,只须要在scrpts: 执行-env,就能够查看总共有那些变量。 对于scrpts:其作用就相当咱们本地的在我的项目根目录下的终端,输出相应指令就能够在运行相应步骤时执行。 对于tags:申明运行时所用的环境 第一个问题—在远端执行时呈现格局报错咱们自定义的脚本在本地运行没有问题后就提交到了gitlab中通过gitlab CI/CD调用时却产生了格局谬误,例如:存在预期之外的“(”等报错。 在网上查找后所失去的问题出处都是呈现在脚本解释器的阐明上即脚本结尾的 #!/bin/bash 然而通过测试——新建test.sh在远端运行通过bash解释器运行发现并没有问题,不会产生报错; 于是我又认真地看了一边shell教程,发现这下面对于函数的阐明是这样的:没有用到function关键字,于是我尝试着去掉它之后发现的确不报错了,但之后在远端执行时还遇到等等其余的格局问题就不在此一一赘述了。 一步一步跟着远端运行的报错解决完格局问题后的确能够在远端残缺运行咱们自定义的.sh文件,然而为什么在本地就没有这些格局问题在远端就呈现问题了过后还是没有解决,过后只是把它归纳于可能是版本不一样。 所以这样的解决办法就带来了上面第二个问题: 问题二—获取pipeLine运行工夫时遇到的问题在gitlab给出的pipeLine自带的变量中只有pipeLine的创立工夫:CI_PIPELINE_CREATED_AT 一开始是想可不可以间接在gitLab CI 运行时取得以后工夫,间接在gitLab配置文件中计算出其时间差,然而找了很久都没有找到能够间接在gitLab CI中获取以后工夫的办法,所以只能转变思路——把CI_PIPELINE_CREATED_AT传给咱们的shell脚本,在脚本中实现这些操作。 currentTime=`date "+%Y-%m-%d %H:%M:%S"` //获取工夫while [ -n "$1" ]; do //shell脚本是以字符串为单位承受的变量case "$1" in. . .-M) PIPE="$2" //把$2即传过来的工夫赋给PIPE CURRENT_STAMP=$(date -d "$currentTime" +%s) //转化为工夫戳 PIPE_STAMP=$(date -d "$PIPE" +%s) //转化为工夫戳 TIME_STRING=$((CURRENT_STAMP-PIPE_STAMP)) //计算工夫戳差值 CONTENT=$CONTENT$TIME_STRING"秒" shift 2 //将数据前移2项,即$1=$3,$2=$4... ;;done在本地执行之后能够失常运行,在远端运行时发现最初计算出来的差值始终为零,起初认为是pipe_line的开始工夫有误,然而查看后与预期统一,于是就尝试着在脚本中输入转化后的起始工夫戳和以后工夫戳然而发现其打印后果都为空。 ...

April 15, 2022 · 1 min · jiezi

关于gitlab-ci-runner:gitlabrunner-x509-certificate-signed-by-unknown-authority

gitlab的单元测试依赖于gitlab-runner,gitlab-runner的作用便是根本gitlab仓库相干的配置来运行单元测试(蕴含但不限于)并把单元测试的后果反馈给gitlab。 所以如果咱们跑单元测试的环境是macos,则须要在一台macos主机上安装gitlab-runner;如果咱们跑单元测试的环境是ubuntu,则须要在一台ubuntu的主机上安装gitlab-runner。 gitlab-runner装置实现后,须要将其注册到gitlab中,这样gitlab才会对其发动近程的调用。注册命令如下: sudo gitlab-runner register --url https://yourGitLab.ServerDomainName.com --registration-token yourGitLabToken但出于某些平安方面的起因,如果咱们的gitlab服务器的证书并不在拟运行单元测试主机的无效验证范畴,则会报一个如下谬误: This solves the x509: certificate signed by unknown authority problem when registering a runner.此时则须要咱们在运行注册命令时,手动的为其指定一个无效的证书。 解决方案gitlab官网专门对这个问题进行了阐明。大略就是说咱们须要手动的为每个预注册的站点来指定相干的证书。 在https的拜访过程中,浏览器会主动的为咱们下载证书并应用 CA 证书进行验证,但gitlab-runner并不会如此,所以咱们须要手动的帮忙一下它。 尽管gitlab的官网给出了几种解决方案,但试验证实将下载到的证书注册到操作系统上是最简略的计划。 步骤如下: 获取证书如果跑gitlab服务的网站的证书就是你申请的,那么你自身就领有一个crt证书,能够略过此步,持续往下看。 如果咱们并不把握以后站点的证书,则能够应用openssl来将获取服务器的crt证书,该操作能够在运行gitlab-runner的测试机上进行,也能够在本人的电脑上进行: openssl s_client -showcerts -connect gitlab.example.com:443(批改为你本人的域名及端口) < /dev/null 2>/dev/null | openssl x509 -outform PEM > ~/gitlab.example.com.crt(批改为本人的名字)总之呢,咱们须要的是一个在gitlab站点上装置的crt证书。 注册证书假如咱们按上一步的操作拿到了相干站点的证书,并且上传到了运行gitlab-runner的机器上。上面,咱们以ubuntu操作系统为例,介绍如何把这个证书全局的装置到操作系统。 yunzhi@yunzhi-virtual-machine:/etc/ca-certificates$ sudo apt-get install -y ca-certificates[sudo] password for yunzhi: Reading package lists... DoneBuilding dependency tree Reading state information... Doneca-certificates is already the newest version (20210119~20.04.2).0 upgraded, 0 newly installed, 0 to remove and 160 not upgraded.yunzhi@yunzhi-virtual-machine:~$ sudo cp gitlab.example.com.crt /usr/local/share/ca-certificates/yunzhi@yunzhi-virtual-machine:/usr/local/share/ca-certificates$ sudo update-ca-certificatesUpdating certificates in /etc/ssl/certs...rehash: warning: skipping DigiCert-Global-Root-CA.pem,it does not contain exactly one certificate or CRL1 added, 0 removed; done.Running hooks in /etc/ca-certificates/update.d...done.是要害,示意胜利增加了1个证书。 ...

April 4, 2022 · 1 min · jiezi

关于gitlab-ci-runner:一行行解读gitlabciyml

【日常】.gitlab-ci.yml解读# .job: 定义暗藏的工作Job,该工作不会被Gitlab cicd执行# &template 定义锚点# 暗藏工作联合锚点,能够提供模板给工作Job复用 —— 不可跨文件应用,跨文件思考`!reference`API# .job: &template定义可复用的内容.script_package_install-deploy: &script_package_install-deploy | yarn global add @custom/deploy-tool# docker 镜像image: node:latest# 钩子函数,脚本执行之前须要执行的命令before_script: - npm config set registry https://registry.npm.taobao.org/ - npm config set cache-folder .cache - *script_package_install-deploy# 定义不同的周期阶段,属于同一周期的工作Job并行执行stages: - build - deploy_staging - deploy_preview - deploy_production# .job: &template定义可复用的内容.script_set_env: &script_set_env | if [ "$CI_COMMIT_REF_NAME" == "production" ]; then if [ "$CI_JOB_STAGE" == "deploy_production" ]; then export DEPLOY_ENVIRONMENT=prod else export DEPLOY_ENVIRONMENT=preview fi else [ "$CI_COMMIT_REF_NAME" == "staging" ] export DEPLOY_ENVIRONMENT=staging fi# .job: &template定义可复用的内容# 通过*template应用锚点定义的内容.deploy: &deploy_common script: - *script_set_env - deploy-tool deploy# 定义工作buildbuild: stage: build # 所属周期阶段,同周期的并行执行 cache: # 须要缓存文件 key: $CI_PROJECT_NAME paths: - .cache/**/* - node_modules/**/* script: # 该工作要执行的脚本 - npm i - *script_set_env # 通过*template应用锚点定义的内容 - deploy-tool build only: # 执行机会:staging、production分支push后主动执行 - staging - production# 定义工作deploy_stagingdeploy_staging: stage: deploy_staging # 所属周期阶段,同周期的并行执行 <<: *deploy_common # 通过<<: *template合并锚点定义的内容 environment: # 定义要部署的环境 name: staging only: # 执行机会:staging分支push后主动执行 - staging# 定义工作deploy_previewdeploy_preview: stage: deploy_preview # 所属周期阶段,同周期的并行执行 <<: *deploy_common # 通过<<: *template合并锚点定义的内容 environment: # 定义要部署的环境 name: preview only: # 执行机会:production分支push后主动执行 - production# 定义工作deploy_previewdeploy_production: stage: deploy_production # 所属周期阶段,同周期的并行执行 <<: *deploy_common # 通过<<: *template合并锚点定义的内容 environment: # 定义要部署的环境 name: production when: manual # 手动执行 only: # 因为when的存在,不主动执行了,when默认值on_success - production*template vs. <<: *template*template:复用的只是工作脚本的其中一个指令<<: *template:复用的是整个工作脚本 ...

March 26, 2022 · 2 min · jiezi

关于gitlab-ci-runner:简述gitlabci入门篇

GitLab CI 是什么?GitLab CI 是GitLab内置的进行继续集成的工具,只须要在仓库根目录下创立.gitlab-ci.yml 文件,并配置GitLab Runner;每次提交的时候,gitlab将自动识别到.gitlab-ci.yml文件,并且应用Gitlab Runner执行该脚本。 Gitlab Runner简介GitLab-Runner就是一个用来执行.gitlab-ci.yml 脚本的工具。Runner就像认真工作的工人,GitLab-CI就是治理工人的核心,所有工人都要在GitLab-CI外面注册。当相应的我的项目发生变化时,GitLab-CI就会告诉相应的工人执行对应的脚本。 GitLab-Runner个别都是配合GitLab-CI应用的,在GitLab外面定义一个属于这个工程的软件集成脚本,用来自动化地实现一些软件集成工作。GitLab-Runner执行状况如下: 本地代码改变变动代码推送到GitLab上GitLab 将这个变动告诉GitLab-CIGitLab-CI找出这个工程相关联的gitlab-runnergitlab-runner把代码更新到本地依据预设置的条件配置好环境依据预约义的脚本(个别是.gitlab-ci.yml)执行把执行后果告诉给GitLabGitLab显示最终执行的后果Runner类型gitLab-Runner能够分类两种类型:Shared Runner(共享型)和Specific Runner(指定型)。 Shared Runner:所有工程都可能用的,且只有系统管理员可能创立。Specific Runner:只有特定的我的项目能够应用。Runner搭建1. Runner 装置 (Install GitLab Runner 官网)GitLab Runner 能够在 GNU/Linux、macOS、FreeBSD 和 Windows 上装置和应用。你能够装置它: 在一个容器中。通过手动下载二进制文件。通过应用 rpm/deb 包的存储库。以Ubuntu为例(Install GitLab Runner using the official GitLab repositories): 1. Add the official GitLab repository:# For Debian/Ubuntucurl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash2. Install the latest version of GitLab Runner, or skip to the next step to install a specific version:# For Debian/Ubuntu/Mintsudo apt-get install gitlab-runner2. 获取Runner注册Token装置好Runner之后,须要向Gitlab进行注册,注册Runner须要GitLab-CI的url和token。可依据需要注册抉择所需类型Runner。 ...

October 12, 2021 · 3 min · jiezi

关于gitlab-ci-runner:gitlabrunner-部署及关联gitlab

简介GitLab-CI GitLab-CI就是一套配合GitLab应用的继续集成系统(当然,还有其它的继续集成系统,同样能够配合GitLab应用,比方Jenkins)。而且GitLab8.0当前的版本是默认集成了GitLab-CI并且默认启用的。 GitLab-Runner GitLab-Runner是配合GitLab-CI进行应用的。个别地,GitLab外面的每一个工程都会定义一个属于这个工程的软件集成脚本,用来自动化地实现一些软件集成工作。当这个工程的仓库代码产生变动时,比方有人push了代码,GitLab就会将这个变动告诉GitLab-CI。这时GitLab-CI会找出与这个工程相关联的Runner,并告诉这些Runner把代码更新到本地并执行预约义好的执行脚本。指标:代码提交到GitLab上,由GitLab的CI性能主动实现部署。原理:GitLab在接管到代码提交事件时,通过.gitlab-ci.yml的配置信息与对应节点上的runner进行交互。装置部署planA (内网可用)下载&&装置[root@gitlab ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm--2021-08-16 10:11:02-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.0.1-ce.0.el7.x86_64.rpmResolving mirrors.xxxxe.com (mirrors.xxxxe.com)... 10.130.104.43Connecting to mirrors.xxxxe.com (mirrors.xxxxe.com)|10.130.104.43|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 32189634 (31M) [application/x-redhat-package-manager]Saving to: ‘gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm’100%[=============================================================================================================================>] 32,189,634 2.23MB/s in 11s 2021-08-16 10:11:13 (2.80 MB/s) - ‘gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm’ saved [32189634/32189634][root@gitlab ~]# lsanaconda-ks.cfg gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm[root@gitlab ~]# rpm -ivh gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm warning: gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 880721d4: NOKEYerror: Failed dependencies: git is needed by gitlab-runner-12.0.1-1.x86_64[root@gitlab ~]# cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core) [root@gitlab ~]# rpm -ivh gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm --nodeps --forcewarning: gitlab-ce-12.0.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 880721d4: NOKEYPreparing... ################################# [100%]Updating / installing... 1:gitlab-runner-12.0.1-1 ################################# [100%]GitLab Runner: creating gitlab-runner...批改gitlab-runner 启动用户[root@gitlab ~]# ps -ef | grep runnerroot 3739 1 0 10:23 ? 00:00:00 /usr/lib/gitlab-runner/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runnerroot 3801 3780 0 10:49 pts/0 00:00:00 grep --color=auto runner[root@gitlab ~]# gitlab-runner uninstallRuntime platform arch=amd64 os=linux pid=3802 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner install --working-directory /root --user rootRuntime platform arch=amd64 os=linux pid=3821 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner restartRuntime platform arch=amd64 os=linux pid=3853 revision=0e5417a3 version=12.0.1[root@gitlab ~]# gitlab-runner statusRuntime platform arch=amd64 os=linux pid=3868 revision=0e5417a3 version=12.0.1gitlab-runner: Service is running![root@gitlab ~]# ps -ef | grep gitlab-runnerroot 3861 1 0 10:51 ? 00:00:00 /usr/lib/gitlab-runner/gitlab-runner run --working-directory /root --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user root注册[root@gitlab ~]# gitlab-ci-multi-runner registerRuntime platform arch=amd64 os=linux pid=3878 revision=0e5417a3 version=12.0.1Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):https://gitlab.xxxx.com #gitlab urlPlease enter the gitlab-ci token for this runner:Gxxxxxxxxxxxxxxxx7 #gitlab tokenPlease enter the gitlab-ci description for this runner:[gitlab]: wuhan-tongj-10.82.16.44Please enter the gitlab-ci tags for this runner (comma separated):wuhan-tongj-10.82.16.44Registering runner... succeeded runner=GLNByiz6Please enter the executor: shell, virtualbox, docker+machine, docker-ssh+machine, docker, parallels, kubernetes, docker-ssh, ssh:shell #脚本执行的语言用处等Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ...

August 16, 2021 · 3 min · jiezi

关于gitlab-ci-runner:微服务项目部署实践使用Gitlab-Runner实现微服务项目的持续集成持续交付和持续部署

概念服务治理遇到的问题 在微服务项目中每个服务都是独立运行的我的项目不可能对每个我的项目进行手动部署,波及到自动化运维的问题 继续集成继续集成(Continues Integration,简称CI)继续集成指的是,频繁(一天屡次)地将代码集成到骨干,长处有两个: 疾速发现错误: 每实现一点更新, 就集成到骨干,能够疾速发现错误,定位谬误避免分支大幅偏离主题: 如果不是常常集成,骨干又在不断更新,会导致当前集成难度变大,甚至难以集成继续集成强调:开发人员提交了新的代码之后,立刻进行构建,(单元)测试,依据测试后果,确定新代码和原有代码是否集成到一起与集成相干的概念还有继续交付和继续部署 应用GitLab继续集成GitLab8.0当前,GitLab CI就曾经集成在GitL中,只有在我的项目中增加一个 .gitlab-ci.yml文件,而后增加一个Runner,就能够进行继续集成Pipeline Pipeline: 管道 ,一次Pipeline相当于一次构建工作,能够蕴含多个流程:装置依赖,运行测试,编译,部署测试服务器,部署生产服务器等流程任何提交或者Merge Request的合并都能够触发PipelineStages Stages示意构建阶段,也就是下面的流程,能够在一次Pipeline中构建多个Stages,这些Stages的特点: 所有Stages会依照程序运行: 即当一个Stage实现后,下一个Stage才会开始只有当所有Stages实现后,该构建工作(Pipeline)才会胜利如果任何一个Stage失败,那么后续的Stages都不会执行,该构建工作(Pipeline)失败Jobs Jobs示意构建工作,示意某个Stage外面执行的工作,能够在Stages里定义多个Jobs,这些Jobs特点: 雷同Stage中的Jobs会并行执行雷同Stage中的Jobs都执行胜利时,该Stage才会执行胜利如果任何一个Job失败,那么该Stage失败,即构建工作(Pipeline)失败 继续交付继续交付(Continuous Delivery): 频繁地将软件的新版本,交付给品质团队或用户以供评审评审通过,代码就进入生产阶段继续交付是继续集成的下一步,强调的是:不管怎么更新,软件是随时随地能够交付的继续交付是在继续集成的根底上,将集成后的代码部署到更靠近实在运行环境的类生产环境(production-like environment)中 继续部署继续部署(Continuous Deployment)是继续交付的下一步,指的是代码通过评审后,主动部署到生产环境继续部署的指标: 代码在任何时刻都是可部署的,可进入生产阶段继续部署的前提: 自动化实现测试,构建,部署等步骤 GitLab RunnerGitLab CI一般来说,构建工作会占用很多的系统资源(编译代码时),因为GitLab CI是GitLab的一部分,由GitLab CI来运行构建工作的化,GitLab的性能会大大降落GitLab CI最大的作用: 是治理各个我的项目的构建状态 GitLab RunnerGitLab Runner能够装置到不同的机器上,在构建工作运行期间不会影响GitL的性能基于Docker装置GitLab Runner: 1.创立工作目录: /usr/local/docker/runner2.创立构建目录: /usr/local/docker/runner/environment3.下载jdk-8u152-linux-x64.tar.gz复制到/usr/local/docker/runner/environment4.下载apache-maven-3.5.3-bin.tar.gz复制到/usr/local/docker/runner/environmentdaemon.json1.在/usr/local/docker/runner/environment目录下创立daemon.json,用于配置加速器和仓库地址-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------{ "registry-mirrors":[ "https://registry.docker-cn.com" ], "insecure-registries":[ "127.0.0.1:5000" ]}----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Dockerfile1.在 /usr/local/docker/runner/environment目录下创立Dockerfile---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------FROM gitlab/gitlab-runnerMAINTAINER Lusifer <topsale@vip.qq.com># 批改软件源RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list && \ echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list && \ echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list && \ echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list && \ apt-get update -y && \ apt-get clean# 装置 DockerRUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \ curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - && \ add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" && \ apt-get update -y && \ apt-get install -y docker-ceCOPY daemon.json /etc/docker/daemon.json# 装置 Docker ComposeWORKDIR /usr/local/binRUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-composeRUN chmod +x docker-compose# 装置 JavaRUN mkdir -p /usr/local/javaWORKDIR /usr/local/javaCOPY jdk-8u152-linux-x64.tar.gz /usr/local/javaRUN tar -zxvf jdk-8u152-linux-x64.tar.gz && \ rm -fr jdk-8u152-linux-x64.tar.gz# 装置 MavenRUN mkdir -p /usr/local/mavenWORKDIR /usr/local/maven# RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gzCOPY apache-maven-3.5.3-bin.tar.gz /usr/local/mavenRUN tar -zxvf apache-maven-3.5.3-bin.tar.gz && \ rm -fr apache-maven-3.5.3-bin.tar.gz# COPY settings.xml /usr/local/maven/apache-maven-3.5.3/conf/settings.xml# 配置环境变量ENV JAVA_HOME /usr/local/java/jdk1.8.0_152ENV MAVEN_HOME /usr/local/maven/apache-maven-3.5.3ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/binWORKDIR /--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------docker-compose.yml在 /usr/local/docker/runner 目录下创立 docker-compose.yml---------------------------------------------------------------------------------------------------------------------------------------------------------------------------# 示意从 environment 目录下寻找 Dockerfile,即在Docker 里装 Dockerversion: '3.1'services:gitlab-runner: build: environment restart: always container_name: gitlab-runner privileged: true volumes: - ./config:/etc/gitlab-runner - /var/run/docker.sock:/var/run/docker.sock------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------构建镜像并启动 ...

May 19, 2021 · 3 min · jiezi