CI & CD/Jenkins

Jenkins에서 ECS 실행

ZzangHo 2022. 12. 9. 22:58
728x90

Jenkins Pipeline을 이용하여 AWS ECS를 실행하는 방법에 대해 정리해본다.

ECR 업로드 할 때 처럼 AWS CLI를 이용할텐데 아래 페이지에 접속을 해보면 API 설명이 잘 나와 있다.

https://docs.aws.amazon.com/cli/latest/reference/ecs/index.html

 

ecs — AWS CLI 1.27.26 Command Reference

Note: You are viewing the documentation for an older major version of the AWS CLI (version 1). AWS CLI version 2, the latest major version of AWS CLI, is now stable and recommended for general use. To view this page for the AWS CLI version 2, click here. F

docs.aws.amazon.com

 

 

ECS 실행 명령어

aws ecs run-task --cluster {클러스터명} --network-configuration '{"awsvpcConfiguration": {"subnets": [{서브넷}],"securityGroups": [{보안그룹}]}}' --overrides '{"containerOverrides": [ {"name": "{컨테이너명}", "command" : [{어플리케이션 파라미터}]}]}' --task-definition {작업 정의} --launch-type FARGATE
  • cluster : 클러스터명
  • subnets : 서브넷
  • securityGroups : 보안그룹(sg)
  • containerOverrides
    - name : 컨테이너 명
    - command : 어플리케이션 실행 시 넘겨 줄 파라미터
  • task-definition : 작업 정의

 

Jenkins Pipeline

전체 소스는 아래와 같다

pipeline {
    
    agent any
    
    environment {
        registryCredential = 'AWS'
    }
    
    stages{
        stage ('ECS Deploy') {
            steps {
                script{
                    try {
                        withAWS(credentials: registryCredential) {
                            sh '''
                                aws ecs run-task --cluster {클러스터명} --network-configuration '{"awsvpcConfiguration": {"subnets": [{서브넷}],"securityGroups": [{보안그룹}]}}' --overrides '{"containerOverrides": [ {"name": "{컨테이너명}", "command" : [{어플리케이션 파라미터}]}]}' --task-definition {작업 정의} --launch-type FARGATE
                            '''
                        }
                        
                    } catch (error) {
                        print(error)
                        currentBuild.result = 'FAILURE'
                        sh "exit 1"
                    }
                }
            }
        }
    }
}