首页 > 解决方案 > 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.

  1. 我不知道如何将构建的容器从 gitlab 上的私有容器推送到lightsail
  2. 我不知道如何aws ctl通过跑步者传递凭据。

最佳,Marcin Włoch

标签: amazon-web-servicesdockergitlabgitlab-ci-runneramazon-lightsail

解决方案


AWS CLI有2 个版本,而您在第 2 版之后,这是唯一包含 lightsail 命令的版本push-container-image。您可以python:latest放弃图像,因为这仅用于构建 AWSCLI v1。

请注意,要上传 docker 映像,您将需要 docker-in-docker 和 AWSCLI (v2),以便您可以在本地获取可以上传的映像。为此,最好的方法是使用映像并使用脚本docker在本地构建 AWSCLI (v2) 。或者,您也可以尝试添加到默认 AWSCLIv2 映像,但我不喜欢这种方法,因为我更熟悉 alpine(映像的基本 linux 发行版),而且我喜欢它的轻量级和快速性。dockerdocker

这是我的方法:

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\":\"/\"}}"

推荐阅读