在jenkins管道中找不到docker的~/.bashrc中的别名
我创建了一个别名作为docker映像的一部分,并将该命令放在~/.bashrc中。别名非常简单。只需运行在映像生成期间安装的脚本。当我在本地机器上运行docker容器时,我能够看到别名。同样在jenkins管道中,当我运行cat-the-bashrc时,我能够看到alias命令。这是我的文件在jenkins管道中找不到docker的~/.bashrc中的别名,bash,shell,docker,jenkins,jenkins-pipeline,Bash,Shell,Docker,Jenkins,Jenkins Pipeline,我创建了一个别名作为docker映像的一部分,并将该命令放在~/.bashrc中。别名非常简单。只需运行在映像生成期间安装的脚本。当我在本地机器上运行docker容器时,我能够看到别名。同样在jenkins管道中,当我运行cat-the-bashrc时,我能够看到alias命令。这是我的文件 FROM ros:melodic-ros-core-stretch RUN apt-get update COPY ./scripts /scripts RUN cat scripts/alias.sh &
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 pre-build="bash /opt/scripts/pre-build.bash"
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()
}
}
}
这就是alias.sh的样子
#!/bin/bash
mkdir -p /opt/scripts
install --mode=755 --group=root --owner=root /scripts/pre-build.bash /opt/scripts/
rm -rf /scripts
alias pre-build="bash /opt/scripts/pre-build.bash"
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()
}
}
}
这就是Jenkins文件的外观
#!/bin/bash
mkdir -p /opt/scripts
install --mode=755 --group=root --owner=root /scripts/pre-build.bash /opt/scripts/
rm -rf /scripts
alias pre-build="bash /opt/scripts/pre-build.bash"
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
我正在试图找到如何向docker映像添加别名,以便在运行声明性管道时在Jenkinsfile中访问这些别名。您没有运行bash,而是运行了sh,第一个命令是引用以下/bin/bash的注释:
sh '''#!/bin/bash
pre-build
'''
如果要运行bash,请尝试以下操作:
sh "/bin/bash -c 'pre-build'"
您可能还需要将bash配置为类似于登录shell,以使其处理所有正常的登录文件,如.bashrc:
您应该大致假设.bashrc和类似的点文件在Docker中不起作用。有许多常见的运行Docker命令的路径,这些命令要么根本不运行Shell,要么以不读取点文件的方式运行它们:
docker run --rm myimage some command
直接运行一些命令而不调用shell。即使显式使用sh-c,也可以使用一些不会读取shell点文件的命令
您不需要像您显示的那样的别名声明,尤其是在Docker中。只需将脚本安装到$PATH中的某个目录中,如/usr/local/bin
简单的解决方案总是不使用别名。完全