简单的目标:
我希望两个容器都在我的本地计算机上运行。一个 jenkins
容器和一个 SSH 服务器
容器。然后,jenkins 作业可以连接到 SSH 服务器容器并执行 aws 命令将文件上传到 S3。
我的工作区目录结构:
- a
docker-compose.yml
(详细信息见下文) - 名为
centos/
的目录, - 在
centos/
中,我有一个用于构建 SSH 服务器镜像的Dockerfile
。
docker-compose.yml:
在我的 docker-compose.yml
中,我声明了两个容器(服务)。
- 一个 jenkins 容器,名称
jenkins
。 - 一个 SSH 服务器容器,名为
remote_host
。
version: '3'
services:
jenkins:
container_name: jenkins
image: jenkins/jenkins
ports:
- "8080:8080"
volumes:
- $PWD/jenkins_home:/var/jenkins_home
networks:
- net
remote_host:
container_name: remote_host
image: remote-host
build:
context: centos7
networks:
- net
networks:
net:
remote_host
的 Dockerfile
如下所示(注意最后一个 RUN
安装 AWS CLI):
FROM centos
RUN yum -y install openssh-server
RUN useradd remote_user && \
echo remote_user:1234 | chpasswd && \
mkdir /home/remote_user/.ssh && \
chmod 700 /home/remote_user/.ssh
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
RUN chown remote_user:remote_user -R /home/remote_user/.ssh/ && \
chmod 600 /home/remote_user/.ssh/authorized_keys
RUN ssh-keygen -A
RUN rm -rf /run/nologin
RUN yum -y install unzip
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install
上述设置的当前情况:
我运行docker-compose build
和docker-compose up
。 jenkins
容器和 remote_host
(SSH 服务器)容器均已成功启动并运行。
我可以进入jenkins
容器:
$ docker exec -it jenkins bash
jenkins@7551f2fa441d:/$
我可以通过以下方式成功 ssh 到 remote_host
容器:
jenkins@7551f2fa441d:/$ ssh -i /tmp/remote-key remote_user@remote_host
Warning: the ECDSA host key for 'remote_host' differs from the key for the IP address '172.19.0.2'
Offending key for IP in /var/jenkins_home/.ssh/known_hosts:1
Matching host key in /var/jenkins_home/.ssh/known_hosts:2
Are you sure you want to continue connecting (yes/no)? yes
[remote_user@8c203bbdcf72 ~]$
在 remote_host
容器内,我还在 ~.aws/credentials
下配置了我的 AWS 访问 key 和 secret key :
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
我可以成功运行 aws 命令,将文件从 remote_host
容器上传到我的 AWS S3 存储桶。喜欢:
[remote_user@8c203bbdcf72 ~]$ aws s3 cp myfile s3://mybucket123asx/myfile
问题是什么
现在,我希望我的 jenkins 作业执行 aws 命令将文件上传到 S3。所以我在我的 remote_host
容器中创建了一个 shell 脚本,脚本如下:
#/bin/bash
BUCKET_NAME=$1
aws s3 cp /tmp/myfile s3://$BUCKET_NAME/myfile
在我的 jenkins 中,我已经配置了 SSH & 在我的 jenkins 作业配置中,我有:
如您所见,它只是运行位于 remote_host
容器中的脚本。
当我构建 jenkins 作业时,我总是在控制台中收到错误:上传失败:../../tmp/myfile 到 s3://mybucket123asx/myfile 无法找到凭据
。
为什么相同的 s3 命令在 remote_host
容器中执行时有效,但在从 jenkins 作业运行时无效?
我还尝试在脚本中显式导出 aws key ID 和 secret key 。 (请记住,我在 remote_host
中配置了 ~.aws/credentis
,无需显式导出 aws key 即可工作)
#/bin/bash
BUCKET_NAME=$1
export aws_access_key_id=AKAARXL1CFQNN4UV5TIO
export aws_secret_access_key=MY_SECRETE_KEY
aws s3 cp /tmp/myfile s3://$BUCKET_NAME/myfile
最佳答案
好的,我通过将 export
语句更改为大写来解决了我的问题。因此,问题的原因是,当 jenkins 运行脚本时,它在 remote_host
上以 remote_user
身份运行。虽然在 remote_host
上我有 ~/.aws/credentials
设置,但该文件仅对 root
以外的用户具有读取权限:
[root@8c203bbdcf72 /]# ls -l ~/.aws/
total 4
-rw-r--r-- 1 root root 112 Sep 25 19:14 credentials
这就是为什么当 jenkins 运行脚本将文件上传到 S3 时出现无法找到凭据
失败的原因。因为 remote_user
无法读取凭据文件。因此,我仍然必须取消注释导出 aws key id 和 Secret key 的行。 @Marcin 的评论很有帮助,字母必须是大写字母,否则它将不起作用。
因此,总的来说,我为解决该问题所做的就是更新我的脚本:
export aws_access_key_id=AKAARXL1CFQNN4UV5TIO
export aws_secret_access_key=MY_SECRETE_KEY
关于amazon-web-services - 运行脚本将文件上传到 AWS S3 有效,但通过 jenkins 作业运行相同的脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64085932/