首先服务器要安装gitlab
gitlab服务器如果低于9.0版本,gitlab-runner要选低版本
这里gitlab 版本 8.8.0, gitlab-runner版本 v1.11.1
gitlab安装不多做叙述,安装gitlab-runner
这里有个重要的,如果是docker部署的gitlab,宿主机端口如果映射的不是80,则需更改环境external_url=域名,否则会报以下错误
端口已经改了不是80了
fatal: unable to access 'http://gitlab-ci-token:[email protected]/dlh/citest.git/': Failed to connect to 47.94.156.123 port 80: Connection refused
创建工作目录 /usr/local/docker/runner
创建构建目录 /usr/local/docker/runner/environment
下载 jdk-8u11-linux-x64.tar.gz 并复制到 /usr/local/docker/runner/environment
下载 apache-maven-3.6.1-bin.tar.gz 并复制到 /usr/local/docker/runner/environment
在 /usr/local/docker/runner/environment 目录下创建 Dockerfile,安装jdk还有maven到容器里
这里使用的gitlab版本为8.8.0社区版,gitlab-runner只能使用2.0以下的版本
FROM gitlab/gitlab-runner:v1.11.1
MAINTAINER dlh [email protected]
# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk-8u11-linux-x64.tar.gz /usr/local/java
RUN tar -zxvf jdk-8u11-linux-x64.tar.gz && \
rm -fr jdk-8u11-linux-x64.tar.gz
# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
# RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz
COPY apache-maven-3.6.1-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.6.1-bin.tar.gz && \
rm -fr apache-maven-3.6.1-bin.tar.gz
# COPY settings.xml /usr/local/maven/apache-maven-3.6.1/conf/settings.xml
# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_11
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.6.1
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
WORKDIR /
在 /usr/local/docker/runner 目录下创建 docker-compose.yml
version: '3.1'
services:
gitlab-runner:
build: environment
restart: always
container_name: gitlab-runner
privileged: true
volumes:
- ./config:/etc/gitlab-runner
# 映射docker命令
- /var/run/docker.sock:/var/run/docker.sock
- /bin/docker:/bin/docker
执行docker-compose -f docker-compose.yml up -d
查询正在运行的容器
docker ps
进入到容器里
docker exec -it 0be3ea7795b1 bash
设置maven变量到系统配置
vi /etc/profile
加上
export PATH=$PATH:$MAVEN_HOME/bin
然后 source /etc/profile
这一步很重要
// 进入容器 进行gitlab-runner 注册
root@0be3ea7795b1:/# gitlab-runner register
执行完后会有提示,输入几个内容
这个链接在 gitlab设置里的runner里有
步骤
输入Gitlab CI地址 http://47.94.156.123:8099
输入项目Gitlab CI token HMgHedsyRsd9LySEDQjH
输入Gitlab Runner描述 ci-test
输入Gitlab Runner标签 test-tag
输入Gitlab Runner执行的语言 shell
root@44914cf556e1:/# gitlab-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://47.94.156.123:8099
Please enter the gitlab-ci token for this runner:
HMgHedsyRsd9LySEDQjH
Please enter the gitlab-ci description for this runner:
[44914cf556e1]: gitlab-rn^Hunner-test2
Please enter the gitlab-ci tags for this runner (comma separated):
git-test1
Whether to run untagged builds [true/false]:
[false]: true
Registering runner... succeeded runner=HMgHedsy
Please enter the executor: docker, docker-ssh, parallels, shell, docker-ssh+machine, kubernetes, ssh, virtualbox, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
.gitlab-ci.yml文件放在项目根目录下
在gitlab上,gitlab-runner执行ci报错fatal: unable to access 'http://gitlab-ci-token:xxxxxx@ip/xxx/citest.git
gitlab为8.x版本的会这样,如果是docker部署的gitlab,宿主机端口如果映射的不是80,则需更改环境external_url=域名,否则会报以下错误
fatal: unable to access 'http://gitlab-ci-token:xxxxxx@ip/xxx/citest.git/': Failed to connect to xx.xx.xx.xxx port 80: Connection refused
gitlab为8.x以上的版本,在创建runner的时候,用交互式去创建
gitlab-runner register -n \
--url http://xxxxxx/ci/ \
--registration-token xxxx \
--executor docker \
--description "runner1" \
--docker-image "docker:stable" \
--clone-url http://xxxxx
加上clone-url参数
或者在/etc/gitlab-runner/config.toml文件里
[[runners]]
name = "test11"
url = "http://xxxx"
token = "xxxx"
executor = "docker"
#加上这个clone_url 参数,指定 url
clone_url = "xxx"
[runners.docker]
tls_verify = false
image = "docker:stable"
privileged = false
disable_cache = false
volumes = ["/cache"]
[runners.cache]
参考文章
作者:撸帝
链接:https://www.jianshu.com/p/2a0f1e109c71
来源:简书