【问题标题】:Alias in docker's ~/.bashrc not found in jenkins pipeline在 jenkins 管道中找不到 docker 的 ~/.bashrc 中的别名
【发布时间】:2019-07-20 03:33:31
【问题描述】:

我创建了一个别名作为我的 docker 映像的一部分,并将命令放在 ~/.bashrc 中。别名真的很简单。只需运行在映像构建期间安装的脚本。当我在本地机器上运行 docker 容器时,我能够看到别名。同样在詹金斯管道中,当我运行 cat bashrc 时,我能够看到别名命令。这是我的 dockerfile。

FROM ros:melodic-ros-core-stretch
RUN apt-get update
COPY ./scripts /scripts
RUN cat scripts/alias.sh > ~/.bashrc
RUN bash scripts/install-tools.bash

这就是 install-tools.bash 的样子。

#!/bin/bash

mkdir -p /opt/scripts
install --mode=755 --group=root --owner=root /scripts/pre-build.bash /opt/scripts/
rm -rf /scripts

这就是 alias.sh 的样子。

alias pre-build="bash /opt/scripts/pre-build.bash"

这就是 Jenkinsfile 的样子。

pipeline {
    agent {
        docker {
            args '--network host -u root:root'
            image <private repo from dockerhub>
            registryCredentialsId 'docker-credentials'
            registryUrl 'https://registry.hub.docker.com'
        }
    }
    stages {
        stage ('Test') {
            steps {
                sh '''#!/bin/bash
                        pre-build
                   '''
            }
        }
    }
    post {
        always {
            cleanWs()
        }
    }
}

这是我从詹金斯那里得到的错误。

Started by user Automated Build Environment
Obtained Jenkinsfile from git https://github.com/<Organization>/test_pkg
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test_pkg
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 205a054c-54a3-4cb3-9a7f-519516cc3050
Cloning the remote Git repository
Cloning repository https://github.com/<Organization>/test_pkg
 > git init /var/lib/jenkins/workspace/test_pkg # timeout=10
Fetching upstream changes from https://github.com/<Organization>/test_pkg
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://github.com/<Organization>/test_pkg +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://github.com/<Organization>/test_pkg # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/<Organization>/test_pkg # timeout=10
Fetching upstream changes from https://github.com/<Organization>/test_pkg
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://github.com/<Organization>/test_pkg +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 514613afaf7c8be82bd20e219c062f7a4b3fb733 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 514613afaf7c8be82bd20e219c062f7a4b3fb733
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk 8c840617b0aed9ae50d00700192ed3573bb1c0d4 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
$ docker login -u <private docker hub username> -p ******** https://registry.hub.docker.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/lib/jenkins/workspace/test_pkg@tmp/98982a2f-21bf-4460-9dab-d13f8ddba164/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . <private docker hub repo>

Error: No such object: <private docker hub repo>
[Pipeline] sh
+ docker inspect -f . registry.hub.docker.com/<private docker hub repo>
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 125:130 --network host -u root:root -w /var/lib/jenkins/workspace/test_pkg -v /var/lib/jenkins/workspace/test_pkg:/var/lib/jenkins/workspace/test_pkg:rw,z -v /var/lib/jenkins/workspace/test_pkg@tmp:/var/lib/jenkins/workspace/test_pkg@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 ******** -e ******** -e ******** -e ******** registry.hub.docker.com/<private docker hub repo> cat
$ docker top 657db353b9e3249c87468eafdeae64246f6b2b1ebeb1cd5ed00c618fc8f1691c -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
/var/lib/jenkins/workspace/test_pkg@tmp/durable-e7036032/script.sh: line 2: pre-build: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
[WS-CLEANUP] done
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 657db353b9e3249c87468eafdeae64246f6b2b1ebeb1cd5ed00c618fc8f1691c
$ docker rm -f 657db353b9e3249c87468eafdeae64246f6b2b1ebeb1cd5ed00c618fc8f1691c
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

我正在尝试了解如何在运行声明性管道时将别名添加到我可以在 Jenkinsfile 中访问的 docker 映像。

【问题讨论】:

  • 最简单的解决方案是始终不使用别名。完全没有。

标签: bash shell docker jenkins jenkins-pipeline


【解决方案1】:

你没有运行bash,你运行了sh,第一个命令是在下面引用/bin/bash的评论:

sh '''#!/bin/bash
      pre-build
   '''

如果您想运行 bash,请尝试以下操作:

sh "/bin/bash -c 'pre-build'"

您可能还需要将 bash 配置为类似于登录 shell 以使其处理所有正常的登录文件,例如 .bashrc:

sh "/bin/bash -l -c 'pre-build'"

【讨论】:

    【解决方案2】:

    您应该大致假设.bashrc 和类似的点文件在 Docker 中不起作用。运行 Docker 命令的常见路径有很多,它们要么根本不运行 shell,要么以不读取点文件的方式运行它们:

    docker run --rm myimage some command
    

    直接运行some command,根本不调用shell。即使您明确使用sh -c "some command" 也不会读取 shell 点文件。

    您不需要像您展示的那样声明别名,尤其是在 Docker 中。只需将脚本安装到 $PATH 中已经存在的某个目录中,例如 /usr/local/bin

    COPY ./scripts /usr/local/bin/
    

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2021-07-23
      • 2017-12-20
      • 2019-05-20
      • 1970-01-01
      相关资源
      最近更新 更多