如何比较 jenkins 管道中的 git 标签

问题描述 投票:0回答:0

我正在构建一个多作业管道并设置了 3 个管道:

  1. 主管道(触发 2 个子作业)
  2. 童工1
  3. 童工2

childjob1 的管道脚本包含比较 git 标签的步骤,并且仅在标签发生更改时才进入部署阶段。我已经测试了代码并且运行良好。下面提到的是代码:

#!/usr/bin/env groovy
pipeline {
  agent any

  stages {
    stage('Checkout') {
        steps {
            // uses the credentials built into the job on J`your text`enkins
            checkout scm`
        }
    } 

    stage('Check for Tag Changes') {
        steps {
            script {
            // Get the latest tag from the LOA repository
                def latestTagLOA = sh(
                    returnStdout: true,`
                    script: 'git for-each-ref --sort=-taggerdate --format \'%(refname:short)\' refs/tags --count=1'
                ).trim()
                echo "Latest tag in LOA repository: $latestTagLOA"

                // Read the previous tag from the file
                def previousTagLOA = readFile('previous_tagloa.txt').trim()
                echo "Previous tag in LOA repository: $previousTagLOA"

                // Check if the tags are different
                def tagChangedLOA = latestTagLOA != previousTagLOA
                if (tagChangedLOA) {
                    echo "Tag change detected. New tag found."
                } else {
                    echo "No tag change in LOA repository."
                }

                // Write the latest tag to the file for the next build
                writeFile(file: 'previous_tagloa.txt', text: latestTagLOA)

                // Set the environment variable to be used in the 'when' condition
                env.TAG_CHANGED_LOA = tagChangedLOA.toString()
                env.LATEST_TAG_LOA = latestTagLOA
                env.PREVIOUS_TAG_LOA = previousTagLOA
                    }
                }
            }

       stage('Build and Deploy LOA') {
           when {
            beforeAgent true
            expression { return env.TAG_CHANGED_LOA == 'true' }
            }
        
            steps {
            // This will only run when there is a change in the tag.
            echo "Building and deploying LOA with latest tag: ${env.LATEST_TAG_LOA}"
            sh 'chmod a+rx deployment.sh'
            sh './deployment.sh'
        }        
    }
}

    post {
        always {
        // Save the latest tag in a file for the next run
        writeFile file: "${env.WORKSPACE}/previous_tagloa.txt", text: "${env.LATEST_TAG_LOA}"
    }
    
    failure {
        echo 'Failed'
    }
    }
  }

当我对 childjob2 使用类似的代码时,它以某种方式从 childjob1 的存储库中读取标签,并且管道构建失败。 有没有更好的方法来比较标签并构建部署阶段?

childjob2 的代码如下:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
            checkout scm
        }
    }

        stage('Check for Tag Changes') {
            steps {
                script {
                def previousTagCP = readPreviousTag('previous_tagcp.txt')
                echo "Previous tag in CP repository: ${previousTagCP}"

                def latestTagCP = sh(
                    returnStdout: true,
                    script: 'git ls-remote --tags origin | grep -o "refs/tags/[^\\^]*$" | awk -F/ "{print \$3}" | sort -V | tail -1'
                ).trim()
                echo "Latest tag in CP repository: ${latestTagCP}"

                def tagChangedCP = latestTagCP != previousTagCP
                if (tagChangedCP) {
                    echo "Tag change detected. New tag found."
                } else {
                    echo "No tag change in CP repository."
                }

                env.TAG_CHANGED_CP = tagChangedCP.toString()
                env.LATEST_TAG_CP = latestTagCP
                env.PREVIOUS_TAG_CP = previousTagCP
            }
        }
     }

        stage('Build and Deploy CP') {
            when {
            expression { return env.TAG_CHANGED_CP == 'true' }
        }
            steps {
            echo "Building and deploying CP with latest tag: ${env.LATEST_TAG_CP}"
            sh 'chmod a+rx deployment.sh'
            sh './deployment.sh'

        }
    }
}

        post {
        always {
        // Save the latest tag in a file for the next run
        writePreviousTag('previous_tagcp.txt', env.LATEST_TAG_CP)
    }

        failure {
        echo 'Failed'
    }
}
}

// Custom function to read previous tag from the file
def readPreviousTag(fileName) {
    try {
    return readFile("${env.WORKSPACE}/${fileName}").trim()
}     catch (Exception e) {
        // If the file doesn't exist, set previousTagCP to a default value
        writeFile(file: "${env.WORKSPACE}/${fileName}", text: '0.0')
        return '0.0'
}
}

// Custom function to write the latest tag to the file
def writePreviousTag(fileName, tag) {
writeFile(file: "${env.WORKSPACE}/${fileName}", text: tag)
}
groovy jenkins-pipeline tags
© www.soinside.com 2019 - 2024. All rights reserved.