From 41a56e46009a486d5898b974144121df446a7b1a Mon Sep 17 00:00:00 2001 From: Maxence Maireaux Date: Thu, 8 Jul 2021 17:49:18 +0200 Subject: [PATCH] Add deployement --- .github/deploy_ecs_service.py | 41 +++++++++++++++++++++++++++++++++++ .github/workflows/main.yml | 11 ++++++++++ .github/workflows/release.yml | 13 +++++++++++ 3 files changed, 65 insertions(+) create mode 100644 .github/deploy_ecs_service.py diff --git a/.github/deploy_ecs_service.py b/.github/deploy_ecs_service.py new file mode 100644 index 000000000..96ef5ea52 --- /dev/null +++ b/.github/deploy_ecs_service.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +from os import environ as ENV +import sys +import boto3 +import json +import subprocess + + +class bcolors: + HEADER = "\033[95m" + OKBLUE = "\033[94m" + OKCYAN = "\033[96m" + OKGREEN = "\033[92m" + WARNING = "\033[93m" + FAIL = "\033[91m" + ENDC = "\033[0m" + BOLD = "\033[1m" + UNDERLINE = "\033[4m" + + +def get_ecs_service(cluster_name, tag): + ecs = boto3.client("ecs") + + # Get Latest TaskDef + ecs_services = ecs.list_services( + cluster=cluster_name, + )["serviceArns"] + + for svc in ecs_services: + service_name = svc.replace( + "arn:aws:ecs:eu-west-1:496288716344:service/", "" + ).replace(cluster_name + "/", "") + print(f"{bcolors.OKGREEN}: Start to deploy {service_name} with {tag}") + process = subprocess.Popen(f"ecs deploy {cluster_name} {service_name} --tag {tag}", shell=True, stdout=subprocess.PIPE) + process.wait() + + return "true" + + +if __name__ == "__main__": + get_ecs_service(sys.argv[1], sys.argv[2]) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0257ff3b3..ef1a5cb0f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,10 +20,15 @@ jobs: env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: "eu-west-1" + AWS_ECS_CLUSTER: "numary-preprod-ecs" steps: - uses: actions/setup-go@v2 with: go-version: '1.16' + - uses: actions/setup-python@v2 + with: + python-version: '3.9' - uses: actions/checkout@v2 - name: Configure git for private modules env: @@ -65,3 +70,9 @@ jobs: docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest + - name: Deploy new version in ECS + env: + IMAGE_TAG: ${{ github.sha }} + run: | + pip install ecs-deploy boto3 + python .github/deploy_ecs_service.py $AWS_ECS_CLUSTER $IMAGE_TAG diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 535680079..f16608862 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,10 +15,17 @@ jobs: env: GOOS: linux GOARCH: ${{ matrix.arch }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: "eu-west-1" + AWS_ECS_CLUSTER: "numary-prod-ecs" steps: - uses: actions/setup-go@v2 with: go-version: '1.16' + - uses: actions/setup-python@v2 + with: + python-version: '3.9' - uses: actions/checkout@v2 - name: Configure git for private modules env: @@ -64,3 +71,9 @@ jobs: docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:latest . docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest + - name: Deploy new version in ECS + env: + IMAGE_TAG: ${{ steps.get_release.outputs.tag_name }} + run: | + pip install ecs-deploy boto3 + python .github/deploy_ecs_service.py $AWS_ECS_CLUSTER $IMAGE_TAG