Jenkins的Git parameter参数化构建

1,384次阅读
没有评论

共计 3548 个字符,预计需要花费 9 分钟才能阅读完成。

Jenkins的Git parameter参数化构建

Git Parameter可以让你通过以参数的形式来拉取指定branch,tag,pull request或revision number

对于参数的类型可以选择:

  • PT_TAG
  • PT_BRANCH
  • PT_BRANCH_TAG
  • PT_REVISION
  • PT_PULL_REQUEST

参数化branch

看一简单的流水线如下

pipeline {
  agent any
  options { timestamps() }
  parameters {
    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
  }
  stages {
    stage('Example') {
      steps {
        // url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
        git branch: "${params.BRANCH}", url: 'http://gitlab.xadocker.cn/devops/git-parameter-plugin.git', credentialsId: 'gitlab-user-jenkins'
      }
    }
  }
}

初次运行时,没有任何的参数选项,需要点击立即构建进行初始化获取仓库信息后才有

Jenkins的Git parameter参数化构建

console中的日志,因为我们在pipeline配置参数defaultValue: ‘master’,所以第一此拉取了master分支

Started by user cicd-admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline研究/project-git-01
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example)
[Pipeline] git
17:31:55  The recommended git tool is: NONE
17:31:55  using credential gitlab-user-jenkins
17:31:55   > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/pipeline研究/project-git-01/.git # timeout=10
17:31:55  Fetching changes from the remote Git repository
17:31:55   > git config remote.origin.url http://gitlab.xadocker.cn/devops/git-parameter-plugin.git # timeout=10
17:31:55  Fetching upstream changes from http://gitlab.xadocker.cn/devops/git-parameter-plugin.git
17:31:55   > git --version # timeout=10
17:31:55   > git --version # 'git version 1.8.3.1'
17:31:55  using GIT_ASKPASS to set credentials gitlab-user-jenkins
17:31:55   > git fetch --tags --progress http://gitlab.xadocker.cn/devops/git-parameter-plugin.git +refs/heads/*:refs/remotes/origin/* # timeout=10
17:31:55   > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
17:31:55  Checking out Revision 20baddbb710a7e8eb515c66e9fa4eec9f67f3324 (refs/remotes/origin/master)
17:31:55   > git config core.sparsecheckout # timeout=10
17:31:55   > git checkout -f 20baddbb710a7e8eb515c66e9fa4eec9f67f3324 # timeout=10
17:31:55   > git branch -a -v --no-abbrev # timeout=10
17:31:55   > git branch -D master # timeout=10
17:31:55   > git checkout -b master 20baddbb710a7e8eb515c66e9fa4eec9f67f3324 # timeout=10
17:31:55  Commit message: "[maven-release-plugin] prepare for next development iteration"
17:31:55  First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

选择其他分支构建测试

Jenkins的Git parameter参数化构建

参数化tag

Jenkins的Git parameter参数化构建
pipeline {
    agent any
    parameters {
        gitParameter name: 'TAG',
                     type: 'PT_TAG',
                     defaultValue: 'master'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.TAG}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'http://gitlab.xadocker.cn/devops/git-parameter-plugin.git',credentialsId: 'gitlab-user-jenkins']]
                        ])
            }
        }
    }
}

参数化revision

Jenkins的Git parameter参数化构建
pipeline {
    agent any
    parameters {
        gitParameter name: 'REVISION',
                     type: 'PT_REVISION',
                     defaultValue: 'master'
    }
    stages {
        stage('Example') {
            steps {
                checkout([$class: 'GitSCM',
                          branches: [[name: "${params.REVISION}"]],
                          doGenerateSubmoduleConfigurations: false,
                          extensions: [],
                          gitTool: 'Default',
                          submoduleCfg: [],
                          userRemoteConfigs: [[url: 'http://gitlab.xadocker.cn/devops/git-parameter-plugin.git',credentialsId: 'gitlab-user-jenkins']]
                        ])
            }
        }
    }
}

parameter还有个排序选项

sortMode: 'NONE' or 'ASCENDING_SMART' or 'DESCENDING_SMART' or 'ASCENDING' or 'DESCENDING'

智能倒序

pipeline {
  agent any
  options { timestamps() }
  parameters {
    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH', sortMode: 'DESCENDING_SMART'
  }
  stages {
    stage('Example') {
      steps {
        // url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
        git branch: "${params.BRANCH}", url: 'http://gitlab.xadocker.cn/devops/git-parameter-plugin.git', credentialsId: 'gitlab-user-jenkins'
      }
    }
  }
}

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2019-08-17发表,共计3548字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)