๊นํ์ก์ ์ฝ๋ ์ค๋ฅ ๊ฒ์ฌ ์ฌ์ดํธ
๊ฐ์ ์๋๋ฆฌ์ค 3
- ํ์ฌ ํ๋ก์ ํธ์ ํฌ์ํ ์ ์๋ ์๊ฐ์ ๊ณ ๋ คํด์ ์ผ๋จ ๊ฐ๋จํ CICD๋ฅผ ์ ์ฉํด๋ณด๊ธฐ ์ํด ์๋๋ฆฌ์ค 3์ ์ ํํจ
- ๊ฐ์
- ๊ฐ๋ฐํ๊ฒฝ, QA, ์ด์ํ๊ฒฝ์ CICD ๊ตฌ์ฑ
- branch
- dev : ๊ฐ๋ฐํ๊ฒฝ
- master : ์ด์ํ๊ฒฝ
- QA
- ํ ์คํธ๋ฅผ ํตํด ์ํ๋ฅด์จ์ด ๊ฒฐํจ ์๋ณํ๊ธฐ
- ์ฝ๋์ ํ์ง๊ณผ ๊ธฐ๋ฅ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ๋์ง ๊ฒ์ฆํจ
- ๋ฐฐํฌ์์
- 1. ๊ฐ๋ฐ ํ๊ฒฝ (dev branch)
- 2. QA ํ๊ฒฝ
- 3. ์ด์ ํ๊ฒฝ (master branch)
- ๊ธฐ์กด ์๊ตฌ์ฌํญ
- ํน์ path์ ๋ํด์๋ง ์คํ
- dev, master branch๋ก PR์ด ์์ฑ&๋๊ธฐํ ๋ ๋ ํ ์คํธ ์์ ์คํ [CI]
- PR์ด dev branch์ ๋จธ์ง๋๋ฉด, ์ด๋ฏธ์ง๋ฅผ ๋น๋ํ๊ณ ๊ฐ๋ฐ ํ๊ฒฝ์ ๋ฐฐํฌ [CD]
- ํ์ํ ์์ ์ QA ํ๊ฒฝ์ ๋ฐฐํฌ [CD] (Tag ์ด๋ฒคํธ๋ก ๋ฐ์)
- QA ๋ฐฐํฌ Tag๋ฅผ ์ด๋ฆ์ผ๋ก ํ๋ Release ๋ธ๋์น ์์ฑ -> master๋ก PR
- PR์ด master branch์ ๋จธ์ง๋๋ฉด, ์ด๋ฏธ์ง๋ฅผ ๋น๋ํ๊ณ ์ด์ ํ๊ฒฝ์ ๋ฐฐํฌ [CD]
- ๋ฐฐํฌ ์ฑ๊ณต ์ฌ๋ถ๋ฅผ ์ฌ๋์ผ๋ก ์ ์กํจ
- Tag๋ฅผ ์ด์ฉํ๋ ์ด์
- ํ์ํ ์์ ์ ๋ฐฐํฌํ๊ธฐ ์ํด์
- ๋กค๋ฐฑ์ ๊ณ ๋ คํ๊ธฐ ์ํด์ : ํ๊ทธ๋ ํน์ ์ปค๋ฐ์ ๋ช ํํ๊ฒ ์ฐธ์กฐํจ
๊ฐ์ ์๋๋ฆฌ์ค3 ์์
- ๋ด ํ๋ก์ ํธ์ ๋ง๊ฒ ์์ ํด์ผํจ
name: cicd-3
on:
push:
paths:
- 'my-app/**'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
pull_request:
types: [opened, synchronize, closed]
branches: [dev, master]
paths:
- 'my-app/**'
jobs:
test:
if: github.event.action == 'opened' || github.event.action == 'synchronize'
runs-on: ubuntu-latest
steps:
- name: checkout the code
uses: actions/checkout@v4
- name: setup-node
uses: actions/setup-node@v3
with:
node-version: 18
- name: Cache Node.js modules
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install dependencies
run: |
cd my-app
npm ci
- name: npm build
run: |
cd my-app
npm run build
set-environment:
if: github.event.pull_request.merged == true || github.ref_type == 'tag'
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.set-env.outputs.environment }}
steps:
- name: set env
id: set-env
run: |
if [[ ${{ github.ref_type }} == "tag" ]]; then
echo "environment=qa" >> $GITHUB_OUTPUT
exit 0
fi
if [[ ${{ github.ref_type }} == "branch" ]]; then
echo "environment=dev" >> $GITHUB_OUTPUT
if [[ ${{ github.base_ref }} == "master" ]]; then
echo "environment=prod" >> $GITHUB_OUTPUT
fi
fi
- name: check env
run: echo ${{ steps.set-env.outputs.environment }}
image-build:
runs-on: ubuntu-latest
needs: [set-environment]
permissions:
id-token: write
contents: read
strategy:
matrix:
environment: ["${{ needs.set-environment.outputs.environment }}"]
environment: ${{ matrix.environment }}
steps:
- name: checkout the code
uses: actions/checkout@v4
- name: Configure AWS Credentials
id: credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: 'true'
- name: docker build & push
run: |
docker build -f Dockerfile --tag ${{ secrets.REGISTRY }}/${{ vars.REPOSITORY }}:${{ github.sha }} .
docker push ${{ secrets.REGISTRY }}/${{ vars.REPOSITORY }}:${{ github.sha }}
deploy:
runs-on: ubuntu-latest
needs: [ set-environment, image-build ]
permissions:
id-token: write
contents: read
strategy:
matrix:
environment: ["${{ needs.set-environment.outputs.environment }}"]
environment: ${{ matrix.environment }}
steps:
- name: checkout the code
uses: actions/checkout@v4
- name: Configure AWS Credentials
id: credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
- name: setup kubectl
uses: azure/setup-kubectl@v3
with:
version: latest
- name: setup helm
uses: azure/setup-helm@v3
with:
version: v3.11.1
- name: access kubernetes
run: |
aws eks update-kubeconfig --name ${{ vars.CLUSTER_NAME }}
- name: deploy
id: status
run: |
helm upgrade --install my-app kubernetes/my-app --create-namespace --namespace my-app-${{ vars.SUFFIX }} \
--set image.tag=${{ github.sha }} \
--set image.repository=${{ secrets.REGISTRY }}/${{ vars.REPOSITORY }}
- name: notify
if: always()
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "message",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Environment : ${{ matrix.environment }}, Deploy Result : ${{ steps.status.outcome }}, Repository : ${{ github.repository }}."
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
create-pr:
if: needs.set-environment.outputs.environment == 'qa'
runs-on: ubuntu-latest
needs: [set-environment, deploy]
steps:
- name: checkout
uses: actions/checkout@v4
- name: gh auth login
run: |
echo ${{ secrets.PERSONAL_ACCESS_TOKEN }} | gh auth login --with-token
- name: create branch
run: |
git checkout -b release/${{ github.ref_name }}
git push origin release/${{ github.ref_name }}
- name: create pr
run: |
gh pr create --base master --head release/${{ github.ref_name }} --title "release/${{ github.ref_name }} -> master" --body "release pr"
- ์์ง ์ต์ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ด ํ๋ก์ ํธ๋ 4๊ฐ์ง์ ์๋๋ฆฌ์ค ์ค, 3๋ฒ์งธ ์๋๋ฆฌ์ค๋ฅผ ๋ฐ๋ฅด๋ ๊ฒ์ผ๋ก ์ ํํ์๋ค.
- ์ฒ์ฒํ ๋์ ํด๋ด์ผ๊ฒ ๋ค ใ ใ
'๐ฅ๏ธ ๋ฐฑ์๋ > CICD' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[CICD] ํค์๋ ๊ธฐ๋ฐ ์ด์ ์๋ฆผ ์๋ํ (0) | 2024.03.23 |
---|---|
[CICD] ๊นํ ๋ ํฌ ์์ฑ ์๋ํ (0) | 2024.03.23 |
[CICD] Github Actions์ ๋ค์ํ ๊ธฐ๋ฅ (0) | 2024.03.22 |
[CICD] Github Actions์ event (0) | 2024.03.21 |
[CICD] ๊นํ๋ธ ์ก์ ์ปดํฌ๋ํธ (0) | 2024.03.21 |