amazon-web-services - GitLab Ci/Cd 到 Amazon LightSail

标签 amazon-web-services docker gitlab gitlab-ci-runner amazon-lightsail

我在将 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没有lightsailctlawscli不支持 lightsail .
  • 我不知道如何将构建的容器从 gitlab 上的私有(private)容器推送到 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 .或者,您也可以尝试添加 dockerdefault 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/

    相关文章:

    docker - 在 Gitlab 运行器中的 Docker 容器内运行测试命令时出现问题

    amazon-web-services - 具有 Wix 域的 Angular AWS

    c# - 如何在 AWS CDK PolicyStatement AddAction 方法中嵌套键值对?

    amazon-web-services - 地形资源 : Connection Error while executing apply?

    spring - SpringBoot-微服务的Docker化+ Angular + MySQL + RabbitMQ

    javascript - 如何使用 javascript 绘制之前保存在 docker workdir 中的图像?

    hadoop - 来自 inputStream 的 AWS 分段上传具有错误的偏移量

    linux - 如何在 Ubuntu docker image/container 上执行 docker 容器的内置 shell

    git - 如何强制 git 使用 SSH 身份验证?

    caching - GitLab CI 中的 ccache 没有命中