Jenkins: How to use a variable from a pre-build shell in the Maven “Goals and options”

后端 未结 6 1538
别跟我提以往
别跟我提以往 2020-12-24 12:20

I have a Maven job in Jenkins. Before the actual build step I have an \"Execute shell\" pre-build step. In that shell I set a variable:

REVISION=$(cat .build         


        
6条回答
  •  温柔的废话
    2020-12-24 12:26

    I needed to resolve the variables before the injection was done so I put this in script content:

    Example: (note it doesn't seem possible to simply export variables here so I wrote to files and the help section in jenkins seems to indicate this is expected)

    git ls-tree --name-only -r ${sha1} | grep -v -c "*\.md" > diff.bak
    
    git diff origin/master --shortstat | grep "1 files changed" && echo 1 > count.bak || echo 0 > count.bak
    
    

    I then added this in the groovy script, using the output files I can create a map:

    def procDiff = "cat $WORKSPACE/diff.bak".execute()
    def procCount = "cat $WORKSPACE/count.bak".execute()
    def diff = procDiff.text
    def count = procCount.text
    
    print "string val = $diff and count = $count "
    
    if ("0".equals(diff) || !"1".equals(count)){
    def map = ["GOAL": "clean verify"]
    return map
    } else {
    def map = ["GOAL": "clean"]
    return map
    }
    

    Then I could reference $GOAL in my maven build to conditionally trigger a "clean" or a "clean verify" based on the type of PR raised.

提交回复
热议问题