我的问题:
我希望在由packer (和ansible)构建的亚马逊EC2注册表中保存一个码头映像,作为工件保存。
My限制:构建需要由Bitbucket管道触发。因此,构建步骤需要在Bitbucket管道本身或AWS EC2实例/容器中执行。
这是因为并不是所有的dev机器都有从其本地环境构建的权限/包。我只希望这些图像是一个自动化的CI过程的结果。
我尝试过的:
使用封隔器,我可以远程构建非盟驻苏特派团。我还可以使用Packer (本地构建并远程推送到Amazon )构建Docker映像。
但是,已经在码头容器中执行构建步骤的Bitbucket管道无法访问坞守护进程'docker run‘。
我在Bitbucket管道中收到的错误:
+ packer build ${BITBUCKET_CLONE_DIR}/build/pipelines_builder/template.json
docker output will be in this color.
==> docker: Creating a temporary directory for sharing data...
==> docker: Pulling Docker image: hashicorp/packer
docker: Using default tag: latest
docker: latest: Pulling from hashicorp/packer
docker: 88286f41530e: Pulling fs layer
...
...
docker: 08d16a84c1fe: Pull complete
docker: Digest: sha256:c093ddf4c346297598aaa13d3d12fe4e9d39267be51ae6e225c08af49ec67fc0
docker: Status: Downloaded newer image for hashicorp/packer:latest
==> docker: Starting docker container...
docker: Run command: docker run -v /root/.packer.d/tmp/packer-docker426823595:/packer-files -d -i -t hashicorp/packer /bin/bash
==> docker: Error running container: Docker exited with a non-zero exit status.
==> docker: Stderr: docker: Error response from daemon: authorization denied by plugin pipelines: Command not supported..
==> docker: See 'docker run --help'.
==> docker:
Build 'docker' errored: Error running container: Docker exited with a non-zero exit status.
Stderr: docker: Error response from daemon: authorization denied by plugin pipelines: Command not supported..
See 'docker run --help'.
==> Some builds didn't complete successfully and had errors:
--> docker: Error running container: Docker exited with a non-zero exit status.
Stderr: docker: Error response from daemon: authorization denied by plugin pipelines: Command not supported..
See 'docker run --help'.
==> Builds finished but no artifacts were created.下面的引语说明了一切(摘自链接):
由于安全原因,其他命令(如docker )目前在我们共享的构建基础设施上被禁止。
所以,我知道为什么会发生这样的事情。这是我面临的一个限制。我知道我需要另一种选择。
--一个可能的解决方案:--我目前唯一能想到的解决方案是,使用带有terraform和ansible的映像的Bitbucket管道,包含以下内容:
- terraform apply (spins up an instance/container from AMI with ansible and packer installed)
- clone devops repo with packer build script on it
- execute packer build command (build command depends on ansible, build creates ec2 container registry image)
- terraform destroy
上述解决方案是可行的选择吗?还有其他选择吗?Packer不能从ECS中远程运行的容器中运行命令和提交吗?
我的长期解决方案将是只使用bitbucket管道来触发AWS中的lambda函数,这将在我们的EC2容器注册表中拆分容器并在那里执行构建。更多的控制,我们可以让devs从他们的机器触发lambda函数(使用更多定制的动态变量)。
发布于 2019-03-23 18:19:48
我设置了一些terraform脚本,这些脚本可以用于从任何CI工具执行,附带一些先决条件:
这将在您自己选择的VPC中创建一个新的EC2实例,并执行一个脚本。
对于此堆栈溢出问题,该脚本将包含一些用于构建和推送停靠程序映像的packer命令。EC2实例的AMI需要安装packer和docker。
欲了解更多信息,请访问:https://github.com/dnk8n/remote-provisioner
发布于 2017-08-31 01:23:33
我对要阻止您的问题的理解是,bitbucket管道(通常称为agents)没有足够的权限对您的AWS帐户执行工作(terraform apply,packer build)。
由于bitbucket管道代理运行在Bitbucket云中,而不是在您的AWS帐户中(您可以在它们上分配IAM角色),所以您应该创建一个带有IAM角色的帐户(策略和权限列表如下),将其API密钥(AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY和AWS_SESSION_TOKEN选项)指定为管道中的环境变量。
您可以参考本文档,介绍如何将AWS凭据添加到Bitbucket管道中
https://confluence.atlassian.com/bitbucket/deploy-to-amazon-aws-875304040.html
这样,您就可以运行packer或terraform命令,而不会出现问题。
要获得运行packer build所需的最小策略,请参考以下文档:
https://www.packer.io/docs/builders/amazon.html#using-an-iam-task-or-instance-role
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action" : [
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CopyImage",
"ec2:CreateImage",
"ec2:CreateKeypair",
"ec2:CreateSecurityGroup",
"ec2:CreateSnapshot",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteKeypair",
"ec2:DeleteSecurityGroup",
"ec2:DeleteSnapshot",
"ec2:DeleteVolume",
"ec2:DeregisterImage",
"ec2:DescribeImageAttribute",
"ec2:DescribeImages",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSnapshots",
"ec2:DescribeSubnets",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"ec2:DetachVolume",
"ec2:GetPasswordData",
"ec2:ModifyImageAttribute",
"ec2:ModifyInstanceAttribute",
"ec2:ModifySnapshotAttribute",
"ec2:RegisterImage",
"ec2:RunInstances",
"ec2:StopInstances",
"ec2:TerminateInstances"
],
"Resource" : "*"
}]
}对于terraform plan/apply,您需要分配最多的权限,因为terraform可以处理几乎所有的aws资源。
其次,对于现有的需求,您只需要运行packer和terraform命令,就不需要在bitbucket管道中运行docker命令。
因此,正常的管道在以上aws API环境下,应该直接工作。
image: hashicorp/packer
pipelines:
default:
- step:
script:
- packer build <your_packer_json_file>您也可以在图像hashicorp/terraform中运行terraform命令。
发布于 2017-09-22 15:54:51
我想我会这样对待它:
terraform apply )。destroy环境这样做可以将基础设施中的所有内容都保持为代码,如果Docker构建在任何时候都支持运行BitBucket,那么将其本地迁移到docker run管道中应该非常简单。
https://stackoverflow.com/questions/45962358
复制相似问题