我正在嘗試在 Jenkins 中創建一個 CI/CD 管道。該管道利用 Jenkinsfile 從私有 Bitbucket 服務器中提取源代碼,構建 Docker 映像并嘗試將其推送到托管 Docker 注冊表的私有 Artifactory。
作為最后一步,我已成功使用此作業流程和 Amazon ECR,但我無法推動 Artifactory 存盤庫作業。
我正在使用這個 Jenkinsfile:
pipeline {
environment {
jenkinsArtifactoryCredentialsID = '...'
}
agent any
options {
skipStagesAfterUnstable()
}
stages {
stage('Clone repository') {
steps {
script {
checkout scm
}
}
}
stage('Build') {
steps {
script {
image = docker.build("test")
}
}
}
stage('Deploy to Artifactory') {
steps {
script {
docker.withRegistry(
'https://artifactory.my.company.com:8443/artifactory/docker_registry_repo',
jenkinsArtifactoryCredentialsID) {
image.push('latest')
image.push("${env.BUILD_NUMBER}")
}
}
}
}
}
}
正如您從檔案中看到的那樣,我在 Artifactory 中創建了一個 repo,據我所知,它是一個成熟的 docker 注冊表,例如,我可以將具有不同標簽的不同影像推送到其中。我已經在 J??enkins 中安裝了 Artifactory 插件并配置了我在此處用作環境變數的 ID 的憑據。
當涉及到構建步驟時,構建會卡住:
...
Login Succeeded
docker tag test artifactory.my.company.com:8443/test:latest
docker push artifactory.my.company.com:8443/test:latest
The push refers to repository [artifactory.my.company.com:8443/test]
154c211638e0: Preparing
0997ee847b8f: Preparing
bca59febf004: Preparing
994393dc58e7: Preparing
0997ee847b8f: Retrying in 5 seconds
...
154c211638e0: Retrying in 1 second
unknown: Not Found
ERROR: script returned exit code 1
Finished: FAILURE
令我惱火的是,在標記期間,以某種方式省略了 repo 的路徑(例如 docker_registry_repo),我懷疑這會導致構建失敗。任何幫助表示贊賞。
編輯:
然后使用 Jenkinsfile 中的 Artifactory API,通過以下命令完成推送:
rtDockerPush(
serverId: 'company-artifactory',
image: 'artifactory.my.company.com:8443/test:latest',
targetRepo: 'docker_registry_repo'
)
我還在 Jenkins 的系統配置中配置了 Artifactory 服務器,這樣我就不必將它們放入檔案中。
在這些更改之后,推送仍然失敗,但需要更多時間才能完成。我看到以下輸出:
INFORMATION: Pushing image: artifactory.my.company.com:8443/test:latest
com.github.dockerjava.api.exception.DockerClientException: Could not push image: unknown: Not Found
at com.github.dockerjava.core.command.PushImageResultCallback.throwFirstError(PushImageResultCallback.java:42)
at com.github.dockerjava.api.async.ResultCallbackTemplate.awaitCompletion(ResultCallbackTemplate.java:93)
at org.jfrog.build.extractor.docker.DockerJavaWrapper.pushImage(DockerJavaWrapper.java:42)
at org.jfrog.build.extractor.docker.extractor.DockerPush.execute(DockerPush.java:84)
at org.jfrog.build.extractor.packageManager.PackageManagerExtractor.executeAndSaveBuildInfo(PackageManagerExtractor.java:33)
at org.jfrog.build.extractor.docker.extractor.DockerPush.main(DockerPush.java:69)
ERROR: Couldn't execute docker task. RuntimeException: docker build failed with exit code 1
java.lang.RuntimeException: docker build failed with exit code 1
at org.jfrog.hudson.pipeline.common.Utils.launch(Utils.java:280)
Caused: java.lang.RuntimeException: docker build failed. Couldn't execute docker task. RuntimeException: docker build failed with exit code 1
at org.jfrog.hudson.pipeline.common.Utils.launch(Utils.java:285)
at org.jfrog.hudson.pipeline.common.executors.BuildInfoProcessRunner.execute(BuildInfoProcessRunner.java:59)
at org.jfrog.hudson.pipeline.common.executors.DockerPushExecutor.execute(DockerPushExecutor.java:42)
at org.jfrog.hudson.pipeline.declarative.steps.docker.DockerPushStep$Execution.runStep(DockerPushStep.java:98)
at org.jfrog.hudson.pipeline.declarative.steps.docker.DockerPushStep$Execution.runStep(DockerPushStep.java:83)
at org.jfrog.hudson.pipeline.ArtifactorySynchronousNonBlockingStepExecution.run(ArtifactorySynchronousNonBlockingStepExecution.java:55)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE
我懷疑它可能與影像名稱有關,但是在 docker.build 命令中更改名稱artifactory.my.company.com:8443/test:latest以匹配要推送的影像會產生相同的錯誤。
uj5u.com熱心網友回復:
由于您安裝了 Artifactory 插件,因此您可以使用與該插件互動的 Artifactory 插件全域變數方法而不是 Docker 全域變數方法來執行此操作。對于宣告性管道:
rtServer(
id: 'myArtifactoryServer',
url: <artifactory docker registry url>,
credentialsId: <artifactory docker registry credentaisl id>
)
rtDockerPush(
serverId: 'Artifactory-1',
image: <registry>/<repo>:<tag>,
targetRepo: <target repo>,
// Attach custom properties to the published artifacts:
properties: 'project-name=docker1;status=stable',
// Optional - Only if this build is associated with a project in Artifactory, set the project key as follows.
project: <project key>,
)
對于腳本化管道(或script宣告性管道內的塊內):
// if artifactory registry id is configured within jenkins
artServer = Artifactory.server(<registry id>)
// otherwise
artServer = Artifactory.newServer(url: <registry url>, credentialsId: <artifactory docker registry credentaisl id>)
artDocker = Artifactory.docker(server: artServer)
artDocker.push(<registry>/<repo>:<tag>, <target repo>)
請注意,回傳的物件docker.build必須來自具有方法引數的完整注冊表和存盤庫名稱的呼叫:
image = docker.build(<registry>/<repository>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508712.html
