简单目标:
我想让两个集装箱都在我的本地机器上运行。一个jenkins
容器和一个SSH server
容器。然后,jenkins作业可以连接到SSH服务器容器&执行aws命令将文件上传到S3。
我的工作区目录结构:
docker-compose.yml
(详见下文)centos/
、centos/
的目录I有一个Dockerfile
用于构建SSH服务器映像。H 216f 217
The docker-compose.yml:
在我的docker-compose.yml
中我声明了两个容器(服务)。
名为jenkins
.
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:
用于Dockerfile
的remote_host
如下所示(注意最后一个RUN
安装AWS ):
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服务器)容器都已启动并成功运行。
I 可以通过以下方式进入jenkins
容器:
$ docker exec -it jenkins bash
jenkins@7551f2fa441d:/$
I可以通过以下方法成功地将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访问密钥和密钥
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
,我可以成功地运行命令,将文件从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作业时,我总是在控制台中得到错误:upload failed: ../../tmp/myfile to s3://mybucket123asx/myfile Unable to locate credentials
。
为什么相同的s3命令在remote_host
容器中执行时工作,而在从jenkins作业运行时却不能工作?
我还尝试在脚本中显式地导出aws键id & secrete键。(请记住,我在~.aws/credentils
中配置了remote_host
,它可以不显式导出aws密钥)
#/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
发布于 2020-09-29 20:32:03
好的,我通过将export
语句改为大写案例来解决我的问题。因此,问题的原因是,当jenkins运行脚本时,它以remote_user
形式在remote_host
上运行。虽然在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时,Unable to locate credentials
失败了。因为remote_user
无法读取凭据文件。因此,我仍然必须取消导出aws密钥id和机密密钥的行。@Marcin的评论很有帮助,因为这些字母必须是大写字母,否则就行不通了。
所以,总的来说,我为解决这个问题所做的就是用以下内容更新我的脚本:
export aws_access_key_id=AKAARXL1CFQNN4UV5TIO
export aws_secret_access_key=MY_SECRETE_KEY
https://stackoverflow.com/questions/64085932
复制相似问题