Jenkins sh 脚本在特定容器中运行时挂起

Wür*_*den 7 continuous-integration sh jenkins docker kubernetes

我正在尝试使用官方 ArgoCD docker 映像(https://hub.docker.com/r/argoproj/argocd/dockerfile)自动部署

我使用 kubernetes 插件为代理创建了一个声明性 jenkins 管道,并使用 yaml 定义了 pod,容器定义如下所示:

pipeline {
    agent {
        kubernetes {
            yaml """
kind: Pod
metadata:
  name: agent
spec:
  containers:
  - name: maven
    image: maven:slim
    command:
    - cat
    tty: true
    volumeMounts:
      - name: jenkins-maven-cache
        mountPath: /root/.m2/repository
  - name: argocd
    image: argoproj/argocd:latest
    command:
    - cat
    tty: true
    ...
Run Code Online (Sandbox Code Playgroud)

我正在尝试在该容器内运行命令,管道中的该步骤如下所示:

stage('Build') {
    steps {
        container('maven') {
            sh 'echo testing' // this works just fine
        }
    }
}
stage('Deploy') {
    steps {
        container('argocd') {
            sh "echo testing" // this does not work
            // more deploy scripts here, once sh works
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我有两个容器,一个 sh 脚本可以正常工作,另一个则不能。“argocd”容器中的 sh 脚本只挂了 5 分钟,然后 Jenkins 将其杀死,退出消息是: process apparently never started in /home/jenkins/agent/workspace/job-name@tmp/durable-46cefcae (running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)

我不能在这个特定的容器中回显一个简单的字符串。

它在其他容器中运行良好,例如来自 Docker 的 Maven 官方,我用来构建 Spring Boot 应用程序。我也可以使用 docker exec 从命令行手动直接在 argocd 容器中运行命令,但 jenkins 由于某种原因不会在管道中。会是什么呢?

我正在运行持久任务插件的最新版本 (1.33)。

更新: 结果证明 argo-cd(连续部署工具)argoproj/argocd:latest 的映像不包含除 之外的其他命令argocd,因此问题出在我尝试使用的容器映像上,而不是 Jenkins 本身。我的解决方案是将 Argo-CD CLI 安装到自定义 docker 容器中并使用它而不是官方容器。

gkc*_*gkc 5

我刚刚在我创建的自定义 docker 映像中遇到了类似的问题。事实证明,我USER nobody在该映像的 Dockerfile 中使用了某种方式,这样 jenkins 代理 pod 无法cat从我的管道脚本中运行命令或任何其他 shell 命令。使用 root 用户运行特定容器对我有用。

所以在你的情况下,我会添加 securityContext: runAsUser: 0 如下。

...
  - name: argocd
    image: argoproj/argocd:latest
    command:
    - cat
    tty: true
    securityContext:
      runAsUser: 0
...

Run Code Online (Sandbox Code Playgroud)

Kubernetes 参考:https ://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container


Dav*_*vid -1

如果问题与 Jenkins 相关,这里有一些可能有助于解决问题的事情:

  1. 工作目录的问题,如果您从某个旧版本更新了 Jenkins,则工作目录/home/jenkins应该是,而在最近的版本中它应该是/home/jenkins/agent ,或者如果您在 Windows 中运行它,路径应该以 开头C:\dir,而不是/dir
  2. 您可以尝试使用新的全新安装,apt-get --purge remove jenkins然后apt-get install jenkins
  3. 当您运行最新版本的持久任务插件时,这不是您的情况。但对于其他人来说,1.28-1.30 之前的参考版本也导致了同样的问题

如果您的 Jenkins 是干净的,则应该以不同的方式调查问题,似乎它不会向 sh 命令返回退出代码和/或脚本在不同的 shell 中执行。我会尝试将 sh 文件放置在容器的工作目录中

#!/bin/bash
echo "testing"
echo $?
Run Code Online (Sandbox Code Playgroud)

并尝试使用source my_script.sh 或运行它bash my_script.sh

$?是最新 bash 操作的退出代码,将其打印出来将确保您的脚本正确终止。运行脚本的source命令将使其在调用它的同一个 shell 中运行,以便可以访问 shell 变量。Bash 命令将在另一个子 shell 中运行它。