You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins流水线报错:groovy.lang.MissingPropertyException: GIT_PASSWORD不存在

Jenkins流水线合并分支时GIT_PASSWORD属性缺失问题解决

已查阅所有同类问题并尝试各种方法,但仍无法解决当前问题。我的流水线代码用于构建Docker容器、启动Flask应用、运行Python脚本与测试,之后合并Master分支,代码如下:

pipeline {
    agent any

    stages {
        stage('Master merging'){
            steps{
                script{
                    passwordVariable = 'password'
                    usernameVariable = 'unsername' // 存在拼写错误
                    // Variables for input
                    if(env.BRANCH_NAME == 'features'||env.BRANCH_NAME == 'main'){
                        sh 'git checkout origin/features'
                        sh 'git pull'
                        sh 'git remote update'
                        sh 'git fetch'
                        sh 'git checkout origin/main'
                        sh 'git merge origin/features'
                        withCredentials([usernamePassword(credentialsId : 'GitHub', passwordVariable:'GIT_PASSWORD', usernameVariable:'GIT_USERNAME')]){
                            sh ""git push http://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/Username/Repo.git""
                        }
                    }
                }
            }
        }
    }
}

执行'Master merging'阶段时出现以下错误:

Masking supported pattern matches of $GIT_PASSWORD
[Pipeline] {
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (container shutdown)
Stage "container shutdown" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: GIT_PASSWORD for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)

问题原因及解决步骤

  1. 修正字符串转义错误
    原代码中sh "git push ..."使用了HTML实体转义的双引号,导致Groovy无法正确解析其中的${GIT_USERNAME}和${GIT_PASSWORD}变量,需替换为普通双引号:

    sh "git push http://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/Username/Repo.git"
    
  2. 修正拼写错误
    代码中usernameVariable = 'unsername'存在拼写错误,应改为usernameVariable = 'username'(虽非本次错误直接原因,但会引发后续潜在问题)。

  3. 优化Git操作流程
    原Git命令存在冗余且处于分离头指针状态,合并后推送易出问题,建议改为操作本地分支:

    • 切换到本地main分支并拉取最新代码
    • 拉取features分支最新代码并合并到main
    • 指定推送分支避免歧义

修正后的完整流水线代码

pipeline {
    agent any

    stages {
        stage('Master merging'){
            steps{
                script{
                    passwordVariable = 'password'
                    usernameVariable = 'username'
                    if(env.BRANCH_NAME in ['features', 'main']){
                        // 切换本地main分支并拉取最新代码
                        sh 'git checkout main'
                        sh 'git pull origin main'
                        // 拉取远程features分支到本地
                        sh 'git fetch origin features:features'
                        // 合并features分支到main
                        sh 'git merge features'
                        // 凭据认证后推送main分支
                        withCredentials([usernamePassword(credentialsId : 'GitHub', passwordVariable:'GIT_PASSWORD', usernameVariable:'GIT_USERNAME')]){
                            sh "git push http://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/Username/Repo.git main"
                        }
                    }
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者CenturyGhost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:18:17