GitLab Ci/Cd 到 Amazon LightSail

6

我在将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

  1. 我不知道如何将从GitLab私有容器构建的容器推送到 lightsail
  2. 我不知道如何通过 runner 向 aws ctl 提供凭证。

祝好, Marcin Włoch

2个回答

4

2个版本的AWS CLI,您需要的是包含lightsail命令push-container-image的第2个版本。可以放弃python:latest镜像,因为这仅用于构建AWSCLI v1。

请注意,为了上传Docker镜像,您需要同时拥有docker-in-docker和AWSCLI(v2),以便您可以在本地获得可上传的映像。最佳方法是使用docker镜像,并使用脚本在本地构建AWSCLI(v2)。或者,您还可以尝试将docker添加到默认的AWSCLIv2镜像,但我不太喜欢这种方法,因为我更熟悉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://dev59.com/kFIH5IYBdhLWcg3wR7pm)
    - ./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\":\"/\"}}"

2
花了半天时间才搞明白。我使用了这个 Docker 镜像 bentolor/docker-dind-awscli,并借鉴了你的代码的一些部分,最终成功了!顺便说一句,如果有人在 Lightsail 中使用环境变量来存储秘密或凭据,建议在 aws lightsail create-container-service-deployment 命令的末尾添加 >/dev/null,以防止这些秘密在 CI 日志中输出。 - IMB
你好。是否可以设置AWS凭证以执行这些命令?我尝试过设置AWS配置,但仍然遇到相同的问题:UnrecognizedClientException: The security token included in the request is invalid. 我在本地测试凭证,它完美地工作。 - Ignacio Giagante
AWS凭证已添加到脚本中。我将它们作为CI/CD变量,因此它们没有出现。 - Steven de Salas
好的。但是你在哪里配置这些凭据以避免我的错误?我尝试了几件事情,但我找不到为什么会收到那个错误。也许,你可以检查一下这段代码 https://gist.github.com/igiagante/8cbc8b87b0dd91ef34e67103e812f2e0 并帮我一下。 - Ignacio Giagante

1
"

aws lightsail push-container-image" 可能需要使用 docker。
我创建了一个包含 awscli、lightsailctl 和 docker 的镜像。

"
-  image: python:latest
+  image: ytoune/aws-lightsail-cli
   script: 
-    - pip install awscli
-    - pip install lightsailctl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接