简体   繁体   English

在 open-jdk 容器中运行时,sh 命令在 Jenkins 管道中挂起

[英]sh command hangs in Jenkins pipeline when run inside a open-jdk container

I am hoping someone can provide me some guidance here:我希望有人可以在这里为我提供一些指导:

I'm using jenkins (2.289.3) as a docker container to run some CI jobs in my macbook Monterey.我正在使用 jenkins (2.289.3) 作为 docker 容器在我的 macbook Monterey 中运行一些 CI 作业。 Here is my docker command这是我的码头工人命令

docker run -d  \
  -u root \
  --name jenkins \
  -p 9080:8080 \
  -v ./jenkins_home:/var/jenkins_home \
  -v "$HOME":/home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  jenkins

I'm using Jenkinsfile to build a project.我正在使用 Jenkinsfile 来构建一个项目。 Below is the code:下面是代码:

#!/usr/bin/env groovy

node {
    def workspace = pwd()
    
    stage('checkout') {
        checkout scm
        sh "echo checkout" ## this works fine!
    }

    docker.image('openjdk:17-jdk-alpine').inside('-u root -e MAVEN_OPTS="-Duser.home=./my-service/"') {
        stage('check java') {
            sh "java -version" ## this fails!
        }

        stage('artifact build and deploy') {
        
            configFileProvider([configFile(fileId: 'gcRepositoryCred', variable: 'GC_MVN_REPOSITORY_CREDENTIAL'),configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                
                sh "chmod +x my-service/mvnw"
                sh "./my-service/mvnw -f my-service/pom.xml clean deploy -DskipTests -s $MAVEN_SETTINGS"
            }
        }

    }
   
}

It fails at line sh "java -version" inside the open jdk container.它在打开的 jdk 容器内的sh "java -version"行失败。 Basically it hangs for a while and I get the below error code:基本上它挂了一段时间,我得到以下错误代码:

[Pipeline] sh
 + echo checkout
 checkout
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] isUnix
 [Pipeline] sh
 + docker inspect -f . openjdk:17-jdk-alpine
 .
 [Pipeline] withDockerContainer
 Jenkins does not seem to be running inside a container
 $ docker run -t -d -u 0:0 -u root -e MAVEN_OPTS=-Duser.home=./my-service/ -w /var/jenkins_home/workspace/my-service -v /var/jenkins_home/workspace/my-service:/var/jenkins_home/workspace/my-service:rw,z -v /var/jenkins_home/workspace/my-service@tmp:/var/jenkins_home/workspace/my-service@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** openjdk:17-jdk-alpine cat
 $ docker top 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf -eo pid,comm
 [Pipeline] {
 [Pipeline] stage
 [Pipeline] { (check java)
 [Pipeline] sh
 process apparently never started in /var/jenkins_home/workspace/my-service@tmp/durable-25c4f40f
 (running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] }
 $ docker stop --time=1 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf
 $ docker rm -f 7413692ee7a423413e80909000e1e94adc2c880016f32071c6ab9d47645092cf
 [Pipeline] // withDockerContainer
 [Pipeline] }
 [Pipeline] // node
 [Pipeline] End of Pipeline
 ERROR: script returned exit code -2
 Finished: FAILURE

Update The same code works in my another laptop (Mac running Big Sur).更新相同的代码在我的另一台笔记本电脑(运行 Big Sur 的 Mac)中工作。 The main difference I see is the docker version.我看到的主要区别是 docker 版本。 Big Sur mac has version 20.10.8 and the machine where it doesn't work has the docker version of 20.10.12. Big Sur mac 有 20.10.8 版本,它不工作的机器有 20.10.12 的 docker 版本。

The another key difference is the log from the working mac.另一个关键区别是来自工作 mac 的日志。 Instead of logging Jenkins does not seem to be running inside a container it says Jenkins is running inside a container .而不是记录Jenkins does not seem to be running inside a container而是说Jenkins is running inside a container Not sure if they both are related in some way.不确定它们是否以某种方式相关。

I got it working with the following instructions: https://www.jenkins.io/doc/book/installing/docker/我按照以下说明进行操作: https ://www.jenkins.io/doc/book/installing/docker/

Looks like with the new updates, I need to use docker:dind container to run docker commands and create my own Dockerfile with the provided instructions in the link above.看起来有了新的更新,我需要使用 docker:dind 容器来运行 docker 命令并使用上面链接中提供的说明创建我自己的 Dockerfile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM