使用 GitLab CI 使用 AWS CDK (python) 进行 CI/CD 的最佳方法是什么? [英] What is the best way to do CI/CD with AWS CDK (python) using GitLab CI?

查看:24
本文介绍了使用 GitLab CI 使用 AWS CDK (python) 进行 CI/CD 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 AWS CDK(与 Python)用于在 Fargate 上运行的容器化应用程序.我想在 GitLab CI 进程中运行 cdk deploy 并将 git 标签作为环境变量传递,以替换在 Fargate 中运行的容器.我目前正在使用 CloudFormation (aws cloudformation update-stack ...) 做类似的事情.还有其他人以这种方式使用 AWS CDK 进行 CI/CD 吗?有更好的方法吗?

I am using AWS CDK (with Python) for a containerized application that runs on Fargate. I would like to run cdk deploy in a GitLab CI process and pass the git tag as an environment variable that replaces the container running in Fargate. I am currently doing something similar with CloudFormation (aws cloudformation update-stack ...). Is anyone else doing CI/CD with AWS CDK in this way? Is there a better way to do it?

另外,我应该为这份工作的基本图像使用什么?我在想我可以从 python 容器开始并安装节点,反之亦然.或者,我还没有找到某个地方的预建容器.

Also, what should I use for my base image for this job? I was thinking that I can either start with a python container and install node or vice versa. Or maybe there is prebuilt container somewhere that I haven't been able to find yet.

这是开始,似乎运作良好:

Here is start that seems to be working well:

CDK:
  image: python:3.8
  stage: deploy
  before_script:
    - apt-get -qq update && apt-get -y install nodejs npm
    - node -v
    - npm i -g aws-cdk
    - cd awscdk
    - pip3 install -r requirements.txt
  script:
    - cdk diff
    - cdk deploy --require-approval never

编辑 2020-05-04:

Edit 2020-05-04:

CDK 可以在 cdk deploy 期间构建 docker 镜像,但它需要访问 docker.如果不需要 docker,上面的 CI 作业定义应该没问题.这是我正在使用的当前 CI 工作:

CDK can build docker images during cdk deploy, but it needs access to docker. If you don't need docker, the above CI job definition should be fine. Here's the current CI job I'm using:

cdk deploy:
  image: docker:19.03.1
  services:
    - docker:19.03.5-dind
  stage: deploy
  only:
    - master
  before_script:
    - apk add --no-cache python3
    - python3 -V
    - pip3 -V
    - apk add nodejs-current npm
    - node -v
    - npm i -g aws-cdk
    - cd awscdk
    - pip3 install -r requirements.txt
  script:
    - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_DEFAULT_REGION
    - cdk deploy --require-approval never

cdk bootstrap 是必需的,因为我在我的 cdk 代码中使用资产:

The cdk bootstrap is needed because I am using assets in my cdk code:

        self.backend_task.add_container(
            "DjangoBackend",
            image=ecs.AssetImage(
                "../backend",
                file="scripts/prod/Dockerfile",
                target="production",
            ),
            logging=ecs.LogDrivers.aws_logs(stream_prefix="Backend"),
            environment=environment_variables,
            command=["/start_prod.sh"],
        )

这里有更多关于 cdk bootstrap 的信息:https://github.com/aws/aws-cdk/blob/master/design/cdk-bootstrap.md

Here's more information on cdk bootstrap: https://github.com/aws/aws-cdk/blob/master/design/cdk-bootstrap.md

推荐答案

如果你有 lambda 或 ECS 资产,你肯定必须在 CI/CD 管道中使用 CDK 部署,否则,你可以运行 CDK 合成器并传递生成的 Cloudformation到 AWS 代码部署.这意味着您的大量 CI/CD 将用于部署,这可能会耗尽您的免费层构建时间,或者只是意味着您需要支付更多费用(AWS Code Deploy 是免费的)

you definitely have to use CDK deploy inside the CI/CD pipeline if you have lambda or ECS assets, otherwise, you could run CDK synth and pass the resulting Cloudformation to AWS Code Deploy. That means a lot of your CI/CD will be spent deploying which might drain your free tier build minutes or just means you pay more (AWS Code Deploy is free)

我在 CircleCi 中使用 Golang 做类似的事情.我使用 Go 基础镜像并安装 nodejs 和 cdk.我使用这个基础镜像来构建我所有的 go 二进制文件、vuejs 前端并编译 cdk typescript 并部署它.

I do something similar with Golang in CircleCi. I use the Go base image and install nodejs and cdk. I use this base image to build all my go binaries, the vuejs frontend and compile cdk typescript and deploy it.

FROM golang:1.13

RUN go get -u -d github.com/magefile/mage
WORKDIR $GOPATH/src/github.com/magefile/mage
RUN go run bootstrap.go

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
RUN npm i -g aws-cdk@1.36.x
RUN npm i -g typescript
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt update && apt install yarn

我希望这会有所帮助.

这篇关于使用 GitLab CI 使用 AWS CDK (python) 进行 CI/CD 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆