json file
{
"family": "{family_name}",
"containerDefinitions": [
{
"name": "{task_name}",
"image": "{ECR_URI}",
"cpu": 0,
"portMappings": [
{
"name": "{service_name}-80-tcp",
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp",
"appProtocol": "http"
},
{
"name": "{service_name}-443-tcp",
"containerPort": 443,
"hostPort": 443,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"environmentFiles": [
{
"value": "arn:aws:s3:::{env_file_object_key_1}",
"type": "s3"
},
{
"value": "arn:aws:s3:::{env_file_object_key_2}",
"type": "s3"
}
],
"mountPoints": [],
"volumesFrom": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/{service_name}",
"awslogs-create-group": "true",
"awslogs-region": "ap-northeast-2",
"awslogs-stream-prefix": "ecs"
}
},
"systemControls": []
}
],
"taskRoleArn": "",
"executionRoleArn": "",
"networkMode": "awsvpc",
"volumes": [],
"placementConstraints": [],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "2048",
"memory": "4096",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
}
}
ECR_REPOSITORY_URI
: ECR의 URIAWS_REGION
, AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
: AWS 인증 정보ci-cd.yml
name: Update, Delete Old Autoscaling Policy, and Apply New Policy for ECS Service
on:
push:
branches:
- dev
jobs:
update-and-autoscale:
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name == 'main' && 'main' ||
github.ref_name == 'dev' && 'dev' }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Get Git Repository URL
id: get_repo_url
run: echo "REPOSITORY_URL=$(git config --get remote.origin.url)" >> $GITHUB_ENV
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Push Docker image to ECR
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: |
${{ secrets.ECR_REPOSITORY_URI }}:${{ github.sha }}
${{ secrets.ECR_REPOSITORY_URI }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
AWS_ECS_CLUSTER
: 앞에서 생성한 클러스터 명AWS_ECS_CLUSTER_SERVICE
: 앞에서 생성한 서비스 명AWS_ECS_TASK_DEFINITION
: 앞에서 정의한 태스크 개정명ci-cd.yml
name: Update, Delete Old Autoscaling Policy, and Apply New Policy for ECS Service
on:
push:
branches:
- dev
jobs:
update-and-autoscale:
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name == 'main' && 'main' ||
github.ref_name == 'dev' && 'dev' }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Get Git Repository URL
id: get_repo_url
run: echo "REPOSITORY_URL=$(git config --get remote.origin.url)" >> $GITHUB_ENV
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Push Docker image to ECR
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: |
${{ secrets.ECR_REPOSITORY_URI }}:${{ github.sha }}
${{ secrets.ECR_REPOSITORY_URI }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Update ECS service
run: |
aws ecs update-service \
--cluster ${{ secrets.AWS_ECS_CLUSTER }} \
--service ${{ secrets.AWS_ECS_CLUSTER_SERVICE }} \
--task-definition ${{ secrets.AWS_ECS_TASK_DEFINITION }} \
--force-new-deployment