我在将 docker 镜像部署到 AWS LightSail
时遇到问题.我在 GitLab 上使用私有(private)容器,并且我的图像在构建后推送到那里。我为 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
.lightsail
aws ctl
通过运行者。 最好的,
马辛·沃赫
最佳答案
有2 versions of the AWS CLI ,并且您在版本 2 之后,这是唯一包含 lightsail 命令 push-container-image
的版本.你可以放弃 python:latest
图像,因为这仅用于构建 AWSCLI v1。
请注意,要上传 docker 镜像,您将需要 docker-in-docker 和 AWSCLI (v2),以便您可以在本地获取可以上传的镜像。要做到这一点,最好的方法是使用 docker
镜像并在本地构建 AWSCLI (v2) using a script .或者,您也可以尝试添加 docker
到default AWSCLIv2 image ,但我不太喜欢这种方法,因为我更熟悉 alpine(docker
图像的基本 linux 发行版),我喜欢它的轻量和快速。
这是我的方法:
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\":\"/\"}}"
关于amazon-web-services - GitLab Ci/Cd 到 Amazon LightSail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65159967/