美文网首页
jenkins 修改 并提交代码

jenkins 修改 并提交代码

作者: 星星326 | 来源:发表于2023-06-27 13:21 被阅读0次

在使用 jenkins 过程中, 有时候需要使用命令修改 代码,或者配置文件,并提交
典型的应用为 把策划方案 excel 转换为json 文件 供程序来使用.
下面我实现了一种

pipeline {
    
    agent {
      label 'mac170'
    }
    
    stages {
        
        stage('Git clone repo') {
            steps {
                //cleanWs()
                git credentialsId: 'xxxx-xxxx-xxxx-xxxx-xxxx', url: 'http://xxxx/xxxx.git'
                sh '''
                
                tempBranch=${branch}
                IFS='/'
                read -ra strarr <<< "${tempBranch}"
                IFS=''

                #git branch --delete --force ${strarr[1]}
                git checkout --track -B ${strarr[1]} ${branch}
                git clean -df
                #git checkout --force ${strarr[1]}
                git switch ${strarr[1]}
                git reset --hard ${branch}
                #git pull origin ${strarr[1]}:${strarr[1]}
                
                printenv
                '''
            }
        }

        stage('Change code stage'){
            steps {
                sh '''
        tempBranch=${branch}
        IFS='/'
        read -ra strarr <<< "${tempBranch}"
        IFS=''
                
        echo "导出config"
        #balabala  做事情,或者调用其他脚本做事情
        
        git add xxxxx.json
        git add xxxxx.json

        #获取修改的文件数量
        lineCnt=`git status -s | wc -l`
        lineCnt=`eval echo $lineCnt`
        if [[ $lineCnt != 0 ]] ; then
            echo "有$lineCnt个文件需要提交" 
            git commit -m "update by jenkins"
            git push origin ${strarr[1]}
        else
            echo "没有任何更新,不需要提交!"
        fi
                '''
            }
        }
    }

}

其中 branch 变量为git paramter 插件的选择结果

git paramter 插件 git paramter 设置

相关文章

网友评论

      本文标题:jenkins 修改 并提交代码

      本文链接:https://www.haomeiwen.com/subject/nshnydtx.html