์ ์ ๐ช๋์ ! ํ๋ฆฌํฐ์ด๋ก AWS ECS ์ฌ์ฉํ๊ธฐ: ECS ํด๋ฌ์คํฐ์ EC2 ์ธ์คํด์ค ์ฐ๊ฒฐ + ๊ฐ๋จ ๋ฐฐํฌ์์ AWS ์ฝ์์์ ๊ฐ๋จํ ECS ์ค์ต์ ํ์๋ค.
์ด์ github actions์ ํ์ฉํด์ ์ง์ง ์ต์ข ๋ชฉํ์ธ ๋ฐฐํฌ ์๋ํ๋ฅผ ๋ฌ์๋ณด์!
๋ฐ๋ก ์์!!
๋จผ์ ๋๋ ํ๋ฆฌํฐ์ด๋ฅผ ์ฌ์ฉํ๊ณ ์๊ณ , EC2 ์ธ์คํด์ค ํ๋๋ฅผ ECS ํด๋ฌ์คํฐ์ ์ฐ๊ฒฐํด์ ์ฌ์ฉํ๊ณ ์๋ค.
๋ฐฐํฌ ์ ์ฐจ๋ ๋ค์๊ณผ ๊ฐ๋ค.
release
ํ๊ทธ, dev
ํ๊ทธ๋ฅผ ๋ถ์ฌ ์ฌ์ฉํ๋ค.github actions์์ AWS์ ECR, ECS๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ์ธ์ฆ ๊ณผ์ ์ ๊ฑฐ์ณ์ผ ํ๋ค.
๋๋ aws-actions/configure-aws-credentials@v4
์ ์ฌ์ฉํ๊ณ github actions์์ ์ฌ์ฉํ IAM Role์ ์์ฑํด์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
(1) ๋จผ์ Identity providers ์์ฑํ๊ธฐ
๐ฑ AWS ์ฝ์ > IAM > Access management >Identity providers > [Add provider]
GitHub Actions์ OIDC(OpenID Connect) ํ ํฐ์ ์ฌ์ฉํ์ฌ AWS์์ ์์ ์๊ฒฉ ์ฆ๋ช ์ ์ป์ ์ ์๊ฒ ํ๊ธฐ ์ํด Identity providers์ ๋จผ์ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
OpenID Connect
https://token.actions.githubusercontent.com
sts.amazonaws.com
(2) IAM Role ์์ฑ
๐ฑ ์์ฑํ ์๊ฒฉ ์ฆ๋ช ๊ณต๊ธ์ > [์ญํ ํ ๋น] > โ ์ ์ญํ ์์ฑ
โ ์น ์๊ฒฉ ์ฆ๋ช
token.actions.githubusercontent.com
sts.amazonaws.com
๐ฑ Repository Setting > Security > Secrets and variables
.github/workflows/CD.yml ์์ฑํ๊ธฐ
name: CD
on:
push:
branches: [dev, release]
# ์ด ๋ถ๋ถ ๋นผ๋จน์ผ๋ฉด ์๋จ ##
permissions:
id-token: write
contents: read
#######################
jobs:
build:
name: ๐ณ ๋์ปค ์ด๋ฏธ์ง ๋น๋ & ํธ์
runs-on: ubuntu-latest
steps:
- name: โ ๋ ํฌ์งํฐ๋ฆฌ ์ฒดํฌ์์
uses: actions/checkout@v3
- name: โก AWS ์๊ฒฉ ์ฆ๋ช
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ vars.AWS_DEFAULT_REGION }}
์ด์ ํ์ฌ ๋ฒ์ ์ ECR์ docker image์ push ํด๋ณด์~!
ํ๋ก์ ํธ ๋ฃจํธ ํด๋์ Dockerfile์ ์์ฑํ๋ค.
FROM node:18-alpine
WORKDIR /app
RUN apk add --no-cache curl
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm
RUN pnpm install
COPY . .
RUN pnpm run build
EXPOSE 3000
ENV PORT 3000
CMD ["pnpm", "run", "start"]
name: CD
on:
push:
branches: [dev, release]
permissions:
id-token: write
contents: read
jobs:
build:
name: ๐ณ ๋์ปค ์ด๋ฏธ์ง ๋น๋ & ํธ์
runs-on: ubuntu-latest
steps:
- name: โ ๋ ํฌ์งํฐ๋ฆฌ ์ฒดํฌ์์
uses: actions/checkout@v3
- name: โก AWS ์๊ฒฉ ์ฆ๋ช
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ vars.AWS_DEFAULT_REGION }}
- name: โข AWS ECR ๋ก๊ทธ์ธ
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: โฃ ์ด๋ฏธ์ง ํ๊ทธ ์ค์
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "IMAGE_TAG=${BRANCH_NAME}" >> $GITHUB_ENV
- name: โค ๋์ปค ์ด๋ฏธ์ง ๋น๋
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.REPOSITORY }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
- name: โฅ AWS ECR ํธ์
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.REPOSITORY }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
ECS ๋ฐฐํฌ๋ ๊ฐ๋จํ๋ค!
depoly:
name: ๐ณ ECS ๋ฐฐํฌ
runs-on: ubuntu-latest
needs: build
steps:
- name: โ ๋ ํฌ์งํฐ๋ฆฌ ์ฒดํฌ์์
uses: actions/checkout@v3
- name: โก AWS ์๊ฒฉ ์ฆ๋ช
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ vars.AWS_DEFAULT_REGION }}
- name: โข ๋ธ๋์น ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
- name: โฃ ECS ๋ฐฐํฌ
env:
CLUSTER: ${{ vars.AWS_ECS_CLUSTER }}
SERVICE: ${{ vars.REPOSITORY }}-${{ env.BRANCH_NAME }}-service
run: |
aws ecs update-service \
--cluster $CLUSTER \
--service $SERVICE \
--force-new-deployment
์ ์ฒด ๋์ปค ํ์ผ
name: CD
on:
push:
branches: [dev, release]
permissions:
id-token: write
contents: read
jobs:
build:
name: ๐ณ ๋์ปค ์ด๋ฏธ์ง ๋น๋ & ํธ์
runs-on: ubuntu-latest
steps:
- name: โ ๋ ํฌ์งํฐ๋ฆฌ ์ฒดํฌ์์
uses: actions/checkout@v3
- name: โก AWS ์๊ฒฉ ์ฆ๋ช
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ vars.AWS_DEFAULT_REGION }}
- name: โข AWS ECR ๋ก๊ทธ์ธ
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: โฃ ์ด๋ฏธ์ง ํ๊ทธ ์ค์
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "IMAGE_TAG=${BRANCH_NAME}" >> $GITHUB_ENV
- name: โค ๋์ปค ์ด๋ฏธ์ง ๋น๋
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.REPOSITORY }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
- name: โฅ AWS ECR ํธ์
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.REPOSITORY }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
run: |
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
depoly:
name: ๐ณ ECS ๋ฐฐํฌ
runs-on: ubuntu-latest
needs: build
steps:
- name: โ ๋ ํฌ์งํฐ๋ฆฌ ์ฒดํฌ์์
uses: actions/checkout@v3
- name: โก AWS ์๊ฒฉ ์ฆ๋ช
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_NAME }}
aws-region: ${{ vars.AWS_DEFAULT_REGION }}
- name: โข ๋ธ๋์น ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
- name: โฃ ECS ๋ฐฐํฌ
env:
CLUSTER: ${{ vars.AWS_ECS_CLUSTER }}
SERVICE: ${{ vars.REPOSITORY }}-${{ env.BRANCH_NAME }}-service
run: |
aws ecs update-service \
--cluster $CLUSTER \
--service $SERVICE \
--force-new-deployment