如何在不在 Cloudformation 模板中硬编码其 id 的情况下将特定工件版本部署到 AWS ECS

How to deploy a specific artifact version to AWS ECS without hard coding its id in Cloudformation template

在基于 Jenkins、Docker 和 Artifactory 的 CI 管道中,我的组件被构建、容器化,然后使用以下 ID 发布到 Artifactory:

docker.artifacts.mycompany.com/my-component:{Unix timestamp}

Jenkins 的最后阶段还使用 "latest" 标签标记该工件:

docker.artifacts.mycompany.com/my-component:latest

通过此 Cloudformation 模板,此标签允许 AWS ECS 获取工件(标记为 "latest")并部署它:

Parameters:
  SourceInstanceRole:
    Type: String
    Description: The IAM role that the my-component instance will use
    Default: Jenken-Automation
  ImageRepository:
    Type: String
    Description: The name of the Docker repository which holds my-component Docker images
    Default: docker.artifacts.mycompany.com
  ImageName:
    Type: String
    Description: The name of the Docker image for my-component
    Default: my-component
  ImageVersion:
    Type: String
    Description: The version tag of my-component docker image in Artifactory
    Default: latest


MyComponentTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Volumes:
      - Name: no-volume
      ContainerDefinitions:
      - Name: !Ref ContainerName
        Essential: true
        Memory: !Ref Memory
        MemoryReservation: !Ref MemoryReservation
        Image: !Sub ${ImageRepository}/${ImageName}:${ImageVersion}

我需要放弃这种方法,因为我希望能够指定要部署的工件的任何版本 - 而不是 "latest"。我的问题是:

如何在不在 Cloudformation 模板中对图像版本进行硬编码的情况下触发将特定工件版本部署到 ECS?

例如:docker.artifacts.mycompany.com/my-component:1500436061

您需要使用参数覆盖以编程方式更改 CloudFormation 模板中硬编码的默认值。

例如,您的 Jenkins 构建可能有一个 bash 脚本,该脚本使用 AWS CLI 通过如下命令部署 CF 模板:

aws cloudformation deploy --template-file mystack.yml --stack-name mystack

您可以像这样为 ImageVersion 变量指定覆盖:

aws cloudformation deploy --template-file mystack.yml --stack-name mystack --parameter-overrides ImageVersion={Unix timestamp}

以下是使用 CLI 进行参数覆盖的文档:http://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html

如果您使用任何语言的 AWS 开发工具包,它也将具有参数覆盖功能。基本上,无论您使用何种机制以编程方式部署 CF 模板,都将有一种方法来指定参数覆盖,将默认值更改为特定版本的自定义值。