Gitlab CI PIP AWSCLI避免再次安装

时间:2020-10-17 00:43:08

标签: gitlab gitlab-ci gitlab-ci-runner

我正在使用GitLab CE,Runners Docker和AWS ECS进行部署。 我们创建了一个脚本,可以执行所需的工作,但是我们将开发的阶段和工作分开了。

问题在于,我们需要运行以下脚本以连接到AWS,并让我们注册容器并部署资源:

services:
  - docker:dind
before_script:
  - apk add build-base python3-dev python3 libffi-dev libressl-dev bash git gettext curl
  - apk add py3-pip
  - pip install six awscli awsebcli
  - $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}")
  - IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)"

问题在于脚本每次随Job一起运行时,这不会成为问题,因为脚本避免了重新安装依赖项:

JOBS INSTALL ALL AGAIN

因此,我们需要知道是否可以避免这种行为,以便仅在每次工作都花很长时间完成此操作之后才运行脚本。

我们完整的脚本.gitlab-ci.yml:

image: docker:latest

stages:
  - build
  - tag
  - push
  - ecs
  - deploy

variables:
  REPOSITORY_URL: OUR_REPO
  REGION: OUR_REGION
  TASK_DEFINITION_NAME: task
  CLUSTER_NAME: default
  SERVICE_NAME: service

services:
  - docker:dind
before_script:
  - apk add build-base python3-dev python3 libffi-dev libressl-dev bash git gettext curl
  - apk add py3-pip
  - pip install six awscli awsebcli
  - $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}")
  - IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)"

build:
  stage: build
  tags:
    - deployment
  script:
      - docker build -t $REPOSITORY_URL:latest .
  only:
      - refactor/ca

tag_image:
  stage: tag
  tags:
    - deployment
  script:
      - docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG
  only:
      - refactor/ca

push_image:
  stage: push
  tags:
    - deployment
  script:
      - docker push $REPOSITORY_URL:$IMAGE_TAG
  only:
      - refactor/ca

task_definition:
  stage: ecs
  tags:
    - deployment
  script:
      - TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_NAME" --region "${REGION}")
      - NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | python3 $CI_PROJECT_DIR/update_task_definition_image.py $REPOSITORY_URL:$IMAGE_TAG)
      - echo "Registering new container definition..."
      - aws ecs register-task-definition --region "${REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}"
  only:
      - refactor/ca

register_definition:
  stage: ecs
  tags:
    - deployment
  script:
      - aws ecs register-task-definition --region "${REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}"
  only:
      - refactor/ca

deployment:
  stage: deploy
  tags:
    - deployment
  script:
      - aws ecs update-service --region "${REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}"  --task-definition "${TASK_DEFINITION_NAME}"
  only:
      - refactor/ca

1 个答案:

答案 0 :(得分:0)

一种解决方法是仅为运行before_scriptaws ecs register-task-definition的阶段定义aws ecs update-service

另外,由于pushecs阶段访问IMAGE_TAG变量,将其存储在build.env工件上很方便,因此该阶段可以通过指定a依赖image_tag阶段。

image: docker:latest
    
stages:
  - build
  - tag
  - push
  - ecs
    
variables:
  REPOSITORY_URL: OUR_REPO
  REGION: OUR_REGION
  TASK_DEFINITION_NAME: task
  CLUSTER_NAME: default
  SERVICE_NAME: service
    
services:
  - docker:dind
    
build:
  stage: build
  tags:
    - deployment
  script:
    - docker build -t $REPOSITORY_URL:latest .
  only:
    - refactor/ca
    
tag_image:
  stage: tag
  tags:
    - deployment
  script:
    - IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)"
    - echo "IMAGE_TAG=$IMAGE_TAG" >> build.env 
    - docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG
  only:
    - refactor/ca
  artifacts:
    reports:
      dotenv: build.env 
    
push_image:
  stage: push
  tags:
    - deployment
  script:
    - docker push $REPOSITORY_URL:$IMAGE_TAG
  only:
    - refactor/ca
  dependencies:
    - tag_image

task_definition:
  stage: ecs
  tags:
    - deployment
  before_script:
    - apk add build-base python3-dev python3 libffi-dev libressl-dev bash git gettext curl
    - apk add py3-pip
    - pip install six awscli awsebcli
    - $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}")
  script:
    - TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_NAME" --region "${REGION}")
    - NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | python3 $CI_PROJECT_DIR/update_task_definition_image.py $REPOSITORY_URL:$IMAGE_TAG)
    - echo "Registering new container definition..."
    - aws ecs register-task-definition --region "${REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}"
    - aws ecs update-service --region "${REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}"  --task-definition "${TASK_DEFINITION_NAME}"
  only:
    - refactor/ca
  dependencies:
    - tag_image
    
相关问题