在詹金斯管道中找不到Docker的〜/ .bashrc中的别名

时间:2019-02-26 14:45:33

标签: bash shell docker jenkins jenkins-pipeline

我创建了一个别名作为docker映像的一部分,并将命令放入〜/ .bashrc中。别名非常简单。只需运行映像构建期间安装的脚本即可。当我在本地计算机上运行docker容器时,我能够看到别名。同样在jenkins管道中,当我让猫bashrc运行时,我能够看到alias命令。这是我的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映像添加别名。

2 个答案:

答案 0 :(得分:1)

您应该大致假设.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/

答案 1 :(得分:0)

您没有运行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'"