因目前测试环境docker版本较低,安装docker出现问题,所以目前gitlab-runner 采用shell模式运行
1. 安装Java
1.下载
2.安装
$rpm -ivh jdk-8u192-linux-x64.rpm
配置环境变量
$vi /etc/profile.d/java.sh
文件内容为:
#!/bin/bash
JAVA_HOME=/usr/java/jdk1.8.0_192-amd64/
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME
export CLASSPATH=.
环境变量生效:
$source /etc/profile.d/java.sh
最后用 java -version 命令查看jdk是否生效。
2. 安装 Maven
下载
解压缩
$mkdir /usr/local/maven
$tar -zxvf apache-maven-3.0.5-bin.tar.gz -C /usr/local/maven/
配置环境变量
$vi /etc/profile.d/maven.sh
文件内容为:
#!/bin/bash
MAVEN_HOME=/usr/local/maven/apache-maven-3.0.5
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME PATH
环境变量生效:
$source /etc/profile.d/maven.sh
最后用 mvn -version 命令查看maven是否生效。
配置settings.xml
因.gitlab-ci.yml脚本历史原因,配置了一个变量 MAVEN_CLI_OPTS: “-s /usr/share/maven/ref/settings.xml —batch-mode”,并在每次执行mvn命令时附加变量MAVEN_CLI_OPTS,所以需要在/usr/share/maven/ref/目录中增加settings.xml配置文件。
命令如下:
$vi /usr/share/maven/ref/settings.xml
内容如下,其中涉及密码部分隐藏:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 修改本地仓库地址到export目录 -->
<localRepository>/export/maven/repository</localRepository>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-releases</name>
<url>http://artifactory.jd.com/libs-releases</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>snapshots</id>
<name>libs-snapshots</name>
<url>http://artifactory.jd.com/libs-snapshots</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-releases</name>
<url>http://artifactory.jd.com/plugins-releases</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshots</name>
<url>http://artifactory.jd.com/plugins-snapshots</url>
</pluginRepository>
</pluginRepositories>
<properties>
<sonar.host.url>
http://192.168.179.114:9000/sonar
</sonar.host.url>
<sonar.login>
${SONAR_TOKEN 这里需要替换成sonar token}
</sonar.login>
</properties>
<id>artifactory</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>artifactory</activeProfile>
</activeProfiles>
<servers>
<server>
<id>jd-snapshots</id>
<username>xnqlarch</username>
<password>${xnqlarch用户公司私服token}</password>
</server>
<server>
<id>jd-central</id>
<username>xnqlarch</username>
<password>${xnqlarch用户公司私服token}</password>
</server>
<server>
<id>git.jd.com</id>
<username>xnqlarch</username>
<password>${xnqlarch用户gitlab token}</password>
</server>
</servers>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
</settings>
3. 安装 Git
下载安装,略(直接从其他容器copy过去即可,mysql镜像的容器自带git)
设置username,email
git config --global user.email "ql_arch@jd.com"
git config --global user.name "xnqlarch"
4. 安装gitlab-runner
下载
安装
$cp gitlab-runner-linux-amd64 /usr/local/bin/gitlab-runner
$sudo chmod +x /usr/local/bin/gitlab-runner
安装gitlab-runner服务
$mkdir /export/gitlab-runner
$gitlab-runner install --user=root --working-directory=/export/gitlab-runner
其他命令
gitlab-runner status 命令查看状态
gitlab-runner start 命令启动服务
gitlab-runner list 命令查看已注册系统列表
gitlab-runner vefify 命令校验已注册系统是否正常
配置文件为 /etc/gitlab-runner/config.toml,可进行编辑,配置文件如下:
concurrent = 1 # 设置最大并发执行job数量
check_interval = 0
[session_server]
session_timeout = 1800
5. 设定Cleanup定时器
因build文件过多会导致磁盘打满,所以需要执行定时任务来删除编译目录,创建文件:
$ vi /export/gitlab-runner/cleanup_gitlab_builds.sh
文件内容为:
#!/bin/bash
# cleanup_gitlab_builds: to remove old gitlab builds
WORK_DIR=/export/gitlab-runner/builds
LAST_MODIFY_MINS=60 # no of days the directory was modified
dir_array=()
space_cleared=0
skipped_dirs=()
QUIET=1
DRY_RUN=1
set -e # exit on errors
# Handle cmd line options
while getopts "d:nqh" opt
do
case "$opt" in
d) LAST_MODIFY_DAYS="$OPTARG";;
n) DRY_RUN=0;;
q) QUIET=0;;
*) ;;
esac
done
# https://stackoverflow.com/a/23357277/4005566
find "$WORK_DIR" -mindepth 4 -maxdepth 4 -type d -cmin +"${LAST_MODIFY_MINS}" -print0 > tempfile
while IFS= read -r -d $'\0'; do
dir_array+=("$REPLY")
done < tempfile
for dir in "${dir_array[@]}"; do
dir_size=$(du -s "$dir" | cut -f 1)
echo "$dir $dir_size"
if [ ! "$DRY_RUN" -eq 0 ]; then
rm -r "$dir"
space_cleared=$((space_cleared + dir_size))
fi
done
if [ ! "$QUIET" -eq 0 ]; then
echo
echo "$space_cleared kb cleared"
fi