CI/CD - 环境搭建(4) - 自动更新Next Release Version

前面已经实现最基本的当有Code Commit,会自动运行Jenkins Job,并把Artifact Upload到Artifactory上去。这中间的Version是手动来更改的,最好也跟着一并做成全自动化的。

Goal

Build成功后,自动更新Verion Number在Pom文件,并上传至Github。

Implementation

  1. 通过Shell Script对Pipeline做了一些修改,增加获得Version,更改Version,上传Github

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    node {
    def mvnHome

    stage('Checkout and Preparation') { // for display purposes
    // Get some code from a GitHub repository
    git credentialsId: 'ee7a726b-e672-424d-9b78-e8015668d55a', url: 'git@github.com:yongfeiuall/artifactory-maven-example.git'
    // Get the Maven tool.
    // ** NOTE: This 'M3' Maven tool must be configured
    // ** in the global configuration.
    mvnHome = tool 'M3'
    }

    stage('Get current version') {
    sh "'${mvnHome}/bin/mvn' help:evaluate -Dexpression=project.version | grep -e '^[^\\[]' > version.out"
    version = readFile("version.out").trim()
    echo "Version is ${version}"
    sh 'cat version.out |awk -F "-" \'{print $1}\' > release.out'
    release = readFile("release.out").trim()
    echo "Release version is ${release}"

    sh 'cat release.out|awk \'{n=split($1,A,".");i=2;major=A[1];while(i<n) major = major "." A[i++];print major}\' > major.out'
    sh 'cat release.out|awk \'{n=split($1,A,".");print A[n]}\' > minor.out'
    major = readFile("major.out").trim()
    minor = readFile("minor.out").trim() as Integer
    }

    stage('Build') {
    // Run the maven build
    sh "'${mvnHome}/bin/mvn' clean install -DskipTests"
    }

    stage('Results') {
    archive 'target/*.jar'
    }

    stage('Update pom with next development version') {
    sh "'${mvnHome}/bin/mvn' versions:set -DnewVersion=${major}.${minor+1}-SNAPSHOT -DoldVersion=* -DgroupId=* -DartifactId=* -DgenerateBackupPoms=false"
    sh script:'git status'
    modifiedFiles = sh(returnStdout: true, script: "git ls-files -m").trim()
    echo modifiedFiles
    if(modifiedFiles) {
    echo "commit required"
    fileAdded = sh(returnStdout: true, script: "git add .")
    sh(script: "git commit -m 'RELEASE_BUILD: changing to next development'")
    sh(script: "git push --set-upstream origin master")

    echo "commit completed"
    } else {
    echo "commit not required"
    }
    }
    }
  2. 运行Jenkins Job

  3. Github验证
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!