因目前测试环境docker版本较低,安装docker出现问题,所以目前gitlab-runner 采用shell模式运行

1. 安装Java

1.下载

2.安装

  1. $rpm -ivh jdk-8u192-linux-x64.rpm

配置环境变量

  1. $vi /etc/profile.d/java.sh

文件内容为:

  1. #!/bin/bash
  2. JAVA_HOME=/usr/java/jdk1.8.0_192-amd64/
  3. PATH=$JAVA_HOME/bin:$PATH
  4. export PATH JAVA_HOME
  5. export CLASSPATH=.

环境变量生效:

  1. $source /etc/profile.d/java.sh

最后用 java -version 命令查看jdk是否生效。

2. 安装 Maven

下载
解压缩

  1. $mkdir /usr/local/maven
  2. $tar -zxvf apache-maven-3.0.5-bin.tar.gz -C /usr/local/maven/

配置环境变量

  1. $vi /etc/profile.d/maven.sh

文件内容为:

  1. #!/bin/bash
  2. MAVEN_HOME=/usr/local/maven/apache-maven-3.0.5
  3. PATH=$MAVEN_HOME/bin:$PATH
  4. export MAVEN_HOME PATH

环境变量生效:

  1. $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配置文件。

命令如下:

  1. $vi /usr/share/maven/ref/settings.xml

内容如下,其中涉及密码部分隐藏:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. <!-- 修改本地仓库地址到export目录 -->
  6. <localRepository>/export/maven/repository</localRepository>
  7. <profiles>
  8. <profile>
  9. <repositories>
  10. <repository>
  11. <snapshots>
  12. <enabled>false</enabled>
  13. </snapshots>
  14. <id>central</id>
  15. <name>libs-releases</name>
  16. <url>http://artifactory.jd.com/libs-releases</url>
  17. </repository>
  18. <repository>
  19. <snapshots>
  20. <enabled>true</enabled>
  21. <updatePolicy>always</updatePolicy>
  22. <checksumPolicy>fail</checksumPolicy>
  23. </snapshots>
  24. <id>snapshots</id>
  25. <name>libs-snapshots</name>
  26. <url>http://artifactory.jd.com/libs-snapshots</url>
  27. </repository>
  28. </repositories>
  29. <pluginRepositories>
  30. <pluginRepository>
  31. <snapshots>
  32. <enabled>false</enabled>
  33. </snapshots>
  34. <id>central</id>
  35. <name>plugins-releases</name>
  36. <url>http://artifactory.jd.com/plugins-releases</url>
  37. </pluginRepository>
  38. <pluginRepository>
  39. <snapshots />
  40. <id>snapshots</id>
  41. <name>plugins-snapshots</name>
  42. <url>http://artifactory.jd.com/plugins-snapshots</url>
  43. </pluginRepository>
  44. </pluginRepositories>
  45. <properties>
  46. <sonar.host.url>
  47. http://192.168.179.114:9000/sonar
  48. </sonar.host.url>
  49. <sonar.login>
  50. ${SONAR_TOKEN 这里需要替换成sonar token}
  51. </sonar.login>
  52. </properties>
  53. <id>artifactory</id>
  54. </profile>
  55. </profiles>
  56. <activeProfiles>
  57. <activeProfile>artifactory</activeProfile>
  58. </activeProfiles>
  59. <servers>
  60. <server>
  61. <id>jd-snapshots</id>
  62. <username>xnqlarch</username>
  63. <password>${xnqlarch用户公司私服token}</password>
  64. </server>
  65. <server>
  66. <id>jd-central</id>
  67. <username>xnqlarch</username>
  68. <password>${xnqlarch用户公司私服token}</password>
  69. </server>
  70. <server>
  71. <id>git.jd.com</id>
  72. <username>xnqlarch</username>
  73. <password>${xnqlarch用户gitlab token}</password>
  74. </server>
  75. </servers>
  76. <pluginGroups>
  77. <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
  78. </pluginGroups>
  79. </settings>

3. 安装 Git

下载安装,略(直接从其他容器copy过去即可,mysql镜像的容器自带git)
设置username,email

  1. git config --global user.email "ql_arch@jd.com"
  2. git config --global user.name "xnqlarch"

4. 安装gitlab-runner

下载
安装

  1. $cp gitlab-runner-linux-amd64 /usr/local/bin/gitlab-runner
  2. $sudo chmod +x /usr/local/bin/gitlab-runner

安装gitlab-runner服务

  1. $mkdir /export/gitlab-runner
  2. $gitlab-runner install --user=root --working-directory=/export/gitlab-runner

其他命令

  1. gitlab-runner status 命令查看状态
  2. gitlab-runner start 命令启动服务
  3. gitlab-runner list 命令查看已注册系统列表
  4. gitlab-runner vefify 命令校验已注册系统是否正常

配置文件为 /etc/gitlab-runner/config.toml,可进行编辑,配置文件如下:

  1. concurrent = 1 # 设置最大并发执行job数量
  2. check_interval = 0
  3. [session_server]
  4. session_timeout = 1800

5. 设定Cleanup定时器

因build文件过多会导致磁盘打满,所以需要执行定时任务来删除编译目录,创建文件:

  1. $ vi /export/gitlab-runner/cleanup_gitlab_builds.sh

文件内容为:

  1. #!/bin/bash
  2. # cleanup_gitlab_builds: to remove old gitlab builds
  3. WORK_DIR=/export/gitlab-runner/builds
  4. LAST_MODIFY_MINS=60 # no of days the directory was modified
  5. dir_array=()
  6. space_cleared=0
  7. skipped_dirs=()
  8. QUIET=1
  9. DRY_RUN=1
  10. set -e # exit on errors
  11. # Handle cmd line options
  12. while getopts "d:nqh" opt
  13. do
  14. case "$opt" in
  15. d) LAST_MODIFY_DAYS="$OPTARG";;
  16. n) DRY_RUN=0;;
  17. q) QUIET=0;;
  18. *) ;;
  19. esac
  20. done
  21. # https://stackoverflow.com/a/23357277/4005566
  22. find "$WORK_DIR" -mindepth 4 -maxdepth 4 -type d -cmin +"${LAST_MODIFY_MINS}" -print0 > tempfile
  23. while IFS= read -r -d $'\0'; do
  24. dir_array+=("$REPLY")
  25. done < tempfile
  26. for dir in "${dir_array[@]}"; do
  27. dir_size=$(du -s "$dir" | cut -f 1)
  28. echo "$dir $dir_size"
  29. if [ ! "$DRY_RUN" -eq 0 ]; then
  30. rm -r "$dir"
  31. space_cleared=$((space_cleared + dir_size))
  32. fi
  33. done
  34. if [ ! "$QUIET" -eq 0 ]; then
  35. echo
  36. echo "$space_cleared kb cleared"
  37. fi