4.1.1、gitlab安装优化等

4.1.2、jenkins配置

4.1、jenkins集成gitlab - 图1

4.1.3、gitlab仓库创建

4.1、jenkins集成gitlab - 图2

4.1.3.1、创建共享库

4.1、jenkins集成gitlab - 图3

  1. 目录
  2. jenkinslib/src/org/devops/build.groovy
  3. =================================================================================================
  4. package org.devops
  5. //构建类型
  6. //传入参数构建类型,构建命令
  7. def Build(buildType,buildShell){
  8. //页面传入的buildType匹配mvn、ant、gradle、npm
  9. //m2、ant、Gradle、NPM为Global Tool Configuration配置变量名称
  10. def buildTools=["mvn":"m2","ant":"ant","gradle":"Gradle","npm":"NPM"]
  11. println("当前选择的构建类型为 ${buildType}")
  12. buildHome = tool buildTools[buildType]
  13. sh "${buildHome}/bin/${buildType} ${buildShell}"
  14. }

4.1.3.2、创建代码库

代码文件如下:

simple-java-maven-app-master.zip

4.1、jenkins集成gitlab - 图4

4.1.3.3、jenkins配置

  1. srcUrl
  2. http://39.105.58.61:9999/root/demo-maven-service.git

4.1、jenkins集成gitlab - 图5

  1. branchName
  2. master

4.1、jenkins集成gitlab - 图6

  1. buildType
  2. mvn

4.1、jenkins集成gitlab - 图7

  1. buildShell
  2. clean package

4.1、jenkins集成gitlab - 图8

4.1.3.4、jenkinsfile配置

4.1.3.4.1、gitlab共享库jenkinsfile

4.1、jenkins集成gitlab - 图9

4.1.3.4.2、jenkins流水线语法脚本生产

  1. http://39.105.58.61:9999/root/jenkinslib.git
  2. 凭证认证
  3. */master
  4. Jenkinsfile

4.1、jenkins集成gitlab - 图10

4.1.3.4.3、变量替换

  1. checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: 'http://39.105.58.61:9999/root/demo-maven-service.git']]])
  2. 替换
  3. checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])

4.1.3.4.4、jenkinsfile脚本

  1. #!groovy
  2. //引用Library库
  3. @Library('jenkinslib@master') _
  4. //引用构建共享库
  5. def build = new org.devops.build()
  6. //引入发布共享库
  7. def deploy = new org.devops.deploy()
  8. //引入参数
  9. String srcUrl = "${env.srcUrl}" //引用参数
  10. String branchName = "${env.branchName}" //引用参数
  11. String buildType = "${env.buildType}" //引用参数
  12. String buildShell = "${env.buildShell}" //引用参数
  13. pipeline {
  14. agent {
  15. node {
  16. label "master" //构建主机
  17. }
  18. }
  19. stages {
  20. //下载代码
  21. stage("CheckOut") {
  22. steps {
  23. script {
  24. checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlabpwd', url: "${srcUrl}"]]])
  25. }
  26. }
  27. }
  28. stage("Build") {
  29. steps {
  30. script {
  31. //引入构建共享库内函数
  32. build.Build(buildType,buildShell)
  33. //引用发布共享库内函数
  34. }
  35. }
  36. }
  37. }
  38. }

4.1、jenkins集成gitlab - 图11

4.1.3.5、maven更改阿里云镜像源

  1. http://dblab.xmu.edu.cn/blog/2758/
  2. cd /usr/local/maven/conf
  3. vim settings.xml
  4. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
  7. http://maven.apache.org/xsd/settings-1.0.0.xsd">
  8. <mirrors>
  9. <mirror>
  10. <id>aliyunmaven</id>
  11. <mirrorOf>*</mirrorOf>
  12. <name>阿里云公共仓库</name>
  13. <url>https://maven.aliyun.com/repository/public</url>
  14. </mirror>
  15. <mirror>
  16. <id>aliyunmaven</id>
  17. <mirrorOf>*</mirrorOf>
  18. <name>阿里云谷歌仓库</name>
  19. <url>https://maven.aliyun.com/repository/google</url>
  20. </mirror>
  21. <mirror>
  22. <id>aliyunmaven</id>
  23. <mirrorOf>*</mirrorOf>
  24. <name>阿里云阿帕奇仓库</name>
  25. <url>https://maven.aliyun.com/repository/apache-snapshots</url>
  26. </mirror>
  27. <mirror>
  28. <id>aliyunmaven</id>
  29. <mirrorOf>*</mirrorOf>
  30. <name>阿里云spring仓库</name>
  31. <url>https://maven.aliyun.com/repository/spring</url>
  32. </mirror>
  33. <mirror>
  34. <id>aliyunmaven</id>
  35. <mirrorOf>*</mirrorOf>
  36. <name>阿里云spring插件仓库</name>
  37. <url>https://maven.aliyun.com/repository/spring-plugin</url>
  38. </mirror>
  39. </mirrors>
  40. </settings>

4.1.3.6、jenkins构建

4.1、jenkins集成gitlab - 图12

4.1、jenkins集成gitlab - 图13

4.1、jenkins集成gitlab - 图14

4.1、jenkins集成gitlab - 图15

4.1.3.7、经典之Hello World!

4.1、jenkins集成gitlab - 图16