簡體   English   中英

Maven發布/ Github OAuth令牌/ Jenkins:無法讀取“https://github.com”的用戶名:沒有此類設備或地址

[英]Maven release / Github OAuth token / Jenkins: could not read Username for 'https://github.com': No such device or address

來自Jenkins我正在嘗試做一個maven版本,代碼托管在github上,在repo上。 對於構建用戶,我生成了OAuth令牌以在RW模式下訪問repo。

在Jenkins中,我配置了存儲庫檢出URL,如https://token@github.com/username/project沒有任何憑據,因為前面的令牌應該足夠了。

在我的pom中,因為我確實設置了任何用戶名/密碼,而不是令牌。 價值很簡單:

<developerConnection>scm:git:https://github.com/username/project</developerConnection>

但是當maven試圖推送pom文件的提交時,我收到了一個錯誤:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.1:prepare (default-cli) on project cloudstack: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] fatal: could not read Username for 'https://github.com': No such device or address
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.1:prepare (default-cli) on project cloudstack:     Unable to commit files
Provider message:
The git-push command failed.
Command output:
fatal: could not read Username for 'https://github.com': No such device or address

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)

知道怎么解決這個問題嗎?

在Jenkins Credentials綁定插件的幫助下,我設法為maven發布插件解決了這個問題。 請注意,ssh身份驗證不需要此解決方案。 這是有問題的SCM部分:

<scm>
    <connection>scm:git:http://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@server.host.name/path/to/project.git</connection>
    <url>http://server.host.name/path/to/project.git</url>
    <tag>HEAD</tag>
</scm>

然后,我使用with credentials插件,在Jenkins管道腳本中使用以下內容:

withCredentials([[$class: 'UsernamePasswordMultiBinding', 
    credentialsId: 'id-of-credentials-from-those-set-up-in-Manage-Jenkins', 
    usernameVariable: 'GIT_USERNAME',
    passwordVariable: 'GIT_PASSWORD'
]]) {
        performRelease()
}

請注意,env.GIT_USERNAME和withCredentials中設置的變量是相同的,這不是偶然的。

我必須道歉,因為這個解決方案假設您熟悉Jenkins管道腳本。

您可以通過在運行自己的自定義maven發布插件腳本之前在環境中設置git憑據來進行調整。

您可以將Maven Release插件配置為不拉動並推送任何更改:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <localCheckout>true</localCheckout>
        <pushChanges>false</pushChanges>
    </configuration>
</plugin>
...

在Jenkins中,您可以使用Git Publisher構建后操作來推送您的分支和標記。 Git Publisher將使用您的OAuth憑據。

推送標簽並不容易,因為您需要指定由Maven生成的確切標簽名稱,例如foo-2.0.1

要從生成的POM中解析版本,我在Maven構建步驟之后的“Execute Shell”構建步驟中使用XMLStarlet

#!/usr/bin/env bash

VERSION=$(xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t -v "x:project/x:version" target/checkout/pom.xml)

echo "RELEASE_VERSION=$VERSION" > version.properties

該腳本生成包含發行版本的屬性文件。 使用“注入環境變量”構建步驟( EnvInject插件 )將屬性文件讀取為構建環境變量。 然后,您可以在“Git Publisher”中將標記名稱指定為foo-$RELEASE_VERSION

這不是很好,但它確實有效。 也許有人有更好的(也更簡單)這樣做的方式。

我只是通過添加一些變體來遵循user3596523的建議。 這是我遵循的步驟,

  1. 正如所建議的那樣,我在下面添加的行是pom.xml <scm> <connection>scm:git:http://${GIT_USERNAME}:${GIT_PASSWORD}@server.host.name/path/to/project.git</connection> <url>http://server.host.name/path/to/project.git</url> <tag>HEAD</tag> </scm>

  2. 既然我們可以將秘密注入jenkin工作( https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs ),我使用構建注入GIT_USERNAME和GIT_PASSWORD綁定。 希望它可以繞過創建管道腳本。 請按照上面的鏈接詳細了解它。 在此輸入圖像描述

我能夠找到另一種處理方式。 為此你需要


安裝Config File Provider插件后

  • Manage Jenkins - > Managed files - > Add a new Config下定義settings.xml 在此輸入圖像描述

  • scm添加到pom.xml

     <scm> <connection>scm:git:https://github.com/user/project.git</connection> <developerConnection>scm:git:https://github.com/user/project.git</developerConnection> <url>https://github.com/user/project/${project.scm.tag}</url> <tag>HEAD</tag> </scm> 
  • Jenkinsfile中添加階段

     stage("Maven release") { steps { configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) { sh "mvn -B clean release:clean" sh "mvn -B release:prepare -s ${env.MAVEN_SETTINGS}" sh "mvn -B release:perform -s ${env.MAVEN_SETTINGS}" } } } 

可以在“app-rest”項目下找到工作源代碼示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM