Jenkins 致命问题:无法访问:URL 使用错误/非法格式或缺少 URL

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

我的问题出在 Jenkins Pipeline 的最后阶段,Jenkins 预计将更新后的 pom.xml 文件提交到 GitLab 上托管的应用程序存储库。 以下是必要的数据。

Jenkins Console Log

Masking supported pattern matches of $PASS
[Pipeline] {
[Pipeline] sh
+ git config --global user.email [email protected]
[Pipeline] sh
+ git config --global user.name jenkins
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [PASS]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ git remote set-url origin https://pop:****@gitlab.com/pop/my-web-app.git
[Pipeline] echo
[Pipeline] sh
+ git add .
[Pipeline] sh
+ git commit -m ci: version bump
[detached HEAD 2e99075] ci: version bump
 1 file changed, 1 insertion(+), 1 deletion(-)
[Pipeline] sh
+ git push origin HEAD:main
fatal: unable to access 'https://gitlab.com/pop/my-web-app.git/': URL using bad/illegal format or missing URL
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 128
Finished: FAILURE

Jenkinsfile

#!/usr/bin/env groovy

library identifier: 'jenkins-shared-library@main', retriever: modernSCM(
        [$class: 'GitSCMSource',
         remote: 'https://gitlab.com/pop/jenkins-shared-lib.git',
         credentialsId: 'gitlab'
        ]
)


pipeline {
    agent any

    environment {
        REPO_URL = 'gitlab.com/pop/my-web-app.git'
    }

    tools {
        maven 'Maven-3.9.5'
    }

    stages {
        stage('Version Increment') {
            steps {
                script {
                    sh 'mvn build-helper:parse-version versions:set \
                        -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} \
                        versions:commit'
                    def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'  
                    def version = matcher[0][1]     

                }
            }
        }
        stage('Build App') {
            steps {
                script {
                    buildJar()
                }
            }
        }
        stage('Build Docker Image & Push') {
            steps {
                script {
                    buildImage "pop/demo-app:java_maven-${IMAGE_NAME}"
                    echo 'Building Image Done'
                    dockerLogin()
                    dockerPush "pop/demo-app:java_maven-${IMAGE_NAME}"
                    echo 'Now Pushing The Image'
                }
            }
        }
        stage('Deploy') {
            steps {
                script {
                    echo 'deploying docker image to EC2...'
                }
            }
        }
        stage('commit version update') {
            steps {
                script {
                    gitLogin '${REPO_URL}'
                    gitCommit()
                }
            }
        }
    }
}

Required Files from Shared Library

VersionCommit.groovy

#!/usr/bin/env groovy

package com.example

class VersionCommit implements Serializable {

    def script

    VersionCommit(script) {
        this.script = script
    }

    def gitLogin(String repoURL) {
        script.withCredentials([script.usernamePassword(credentialsId: 'gitlab', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
            script.sh 'git config --global user.email "[email protected]"'
            script.sh 'git config --global user.name "jenkins"'
            script.sh "git remote set-url origin https://$script.USER:$script.PASS@$repoURL"
        }
    }

    def gitCommit() {
        script.sh 'git add .'
        script.sh 'git commit -m "ci: version bump"'
        script.sh 'git push origin HEAD:main'
    }
}

gitLogin.groovy

#!/usr/bin/env groovy

import com.example.VersionCommit

def call(String repoURL){
    return new VersionCommit(this).gitLogin(repoURL)
}

gitCommit.groovy

#!/usr/bin/env groovy

import com.example.VersionCommit

def call(){
    return new VersionCommit(this).gitCommit()
}

这是我到目前为止所做的:

Jenkins 作业运行到最后阶段都没有任何错误。 pom.xml 文件已使用新版本信息正确更新。 我已经仔细检查了 GitLab 凭据和 SSH 密钥配置,它们似乎是正确的。 尽管做了所有这些准备工作,我还是面临着一个问题,即 Jenkins 作业似乎没有将更新的 pom.xml 提交到 GitLab 存储库。它完成了作业,没有任何与 Git 提交过程相关的错误或警告,但是当我检查 GitLab 存储库时,pom.xml 中没有反映任何更改。

我不确定是什么原因导致这个问题。是 Jenkins 配置问题、GitLab 集成问题还是其他问题?我愿意接受您对于排查和解决此问题可能提出的任何见解或建议。

jenkins groovy gitlab jenkins-pipeline jenkins-groovy
1个回答
0
投票

如何对您的密码进行 URL 编码

我遇到了完全相同的问题,现在可以解决,问题出在我的密码上,它包含无法在 URL 中解析的“特殊字符”,因此我尝试在“git”中硬编码我的密码作为测试远程设置 url 来源......” 如果您的密码包含特殊字符,则需要对它们进行 URL 编码。例如,替换:

变成%23

@ 变为 %40 :变为%3A / 变为 %2F ?变成%3F & 变成 %26 = 变为 %3D

© www.soinside.com 2019 - 2024. All rights reserved.