amazon-web-services - GitLab Ci/Cd 到 Amazon LightSail
问题描述
我在将 docker 映像部署到AWS LightSail
. 我在 GitLab 上使用私有容器,并且我的图像在构建后推送到那里。我为 ci/cd 创建了第二阶段,用于将图像部署到 lightsail。
image: docker:19.03.12
services:
- docker:19.03.12-dind
build:
stage: build
before_script:
- docker login registry.gitlab.com --username $UserName -p $CiCdToken
script:
- docker build -t registry.gitlab.com/nickname/testprojectname .
- docker push registry.gitlab.com/nickname/testprojectname
deploy:
stage: deploy
image: python:latest
script:
- pip install awscli
- pip install lightsailctl
- aws lightsail push-container-image --service-name testprojectname --label testprojectname --image registry.gitlab.com/nickname/testprojectname
不幸的是,python 没有lightsailctl
也不awscli
支持lightsail
.
- 我不知道如何将构建的容器从 gitlab 上的私有容器推送到
lightsail
- 我不知道如何
aws ctl
通过跑步者传递凭据。
最佳,Marcin Włoch
解决方案
AWS CLI有2 个版本,而您在第 2 版之后,这是唯一包含 lightsail 命令的版本push-container-image
。您可以python:latest
放弃图像,因为这仅用于构建 AWSCLI v1。
请注意,要上传 docker 映像,您将需要 docker-in-docker 和 AWSCLI (v2),以便您可以在本地获取可以上传的映像。为此,最好的方法是使用映像并使用脚本docker
在本地构建 AWSCLI (v2) 。或者,您也可以尝试添加到默认 AWSCLIv2 映像,但我不喜欢这种方法,因为我更熟悉 alpine(映像的基本 linux 发行版),而且我喜欢它的轻量级和快速性。docker
docker
这是我的方法:
image: docker:19.03.12
services:
- docker:19.03.12-dind
build:
stage: build
before_script:
- docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
script:
- docker build -t registry.gitlab.com/nickname/testprojectname:${CI_PIPELINE_ID} .
- docker push registry.gitlab.com/nickname/testprojectname:${CI_PIPELINE_ID}
deploy:
stage: deploy
image: docker # NOTE: we need docker cli to make this work!
variables:
AWS_ACCESS_KEY_ID: MYSUPERSECRETACCESSKEYID
AWS_SECRET_ACCESS_KEY: MYSUPERSECRETACCESSKEYSECRET
AWS_DEFAULT_REGION: eu-west-1
before_script:
# 1. Install AWSCLIv2 (https://stackoverflow.com/questions/60298619/awscli-version-2-on-alpine-linux#answer-61268529)
- ./alpine.awscliv2.install.sh
- aws --version
# 2. Install LightsailCTL Plugin (https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-install-software)
- apk --no-cache add curl jq
- curl https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl -o /usr/local/bin/lightsailctl
- chmod +x /usr/local/bin/lightsailctl
script:
# 3. Download the docker image for this pipeline
- docker info
- docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
- docker pull registry.gitlab.com/nickname/testprojectname:${CI_PIPELINE_ID}
# 4. Upload the docker image for this pipeline
- aws lightsail push-container-image
--service-name testprojectname
--label pipeline-${CI_PIPELINE_ID}
--image registry.gitlab.com/nickname/testprojectname:${CI_PIPELINE_ID}
# 5. Get the uploaded image (its different every time)
- PIPELINE_IMAGE_TAG=$(aws lightsail get-container-images --service testprojectname | jq -r .containerImages[0].image)
# 6. Create a deployment with the uploaded docker image
- aws lightsail create-container-service-deployment
--service-name testprojectname
--containers "{\"testprojectname\":{\"image\":\"$PIPELINE_IMAGE_TAG\",\"ports\":{\"8000\":\"HTTP\"}}}"
--public-endpoint "{\"containerName\":\"testprojectname\",\"containerPort\":8000,\"healthCheck\":{\"path\":\"/\"}}"
推荐阅读
- airflow - 使用 Airflow 运算符将运行时版本传递给 CMLE
- c++ - 如何在 C++ 中输出特定的代码行
- java - 有人可以解释 makeDeepCopy 方法。静态部分是什么意思?
- r - 在生成的文件夹结构中使用 Rmarkdown 生成自动报告
- function - 编写函数,给出输入并在条件下打印它们
- r - 如何在ggplot2散点图图例中添加观察计数(n)
- java - 如何配置连接池 Apache Tomcat-> PostgreSQL-> Persistence Java?
- php - 条带,使用幂等键的多个请求返回错误
- python - Python Beautiful Soup,如何检索“data-id”
- r - 我可以为 SQL Server 2016 中的存储过程动态生成 WITH RESULT SETS 子句吗?