infra :: git action 수정 3차 #8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Pipeline with GitHub Actions for K8S | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
deploy: | |
name: Build And Deploy Backend K8S | |
environment: chatforyou-python-env | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
contents: read | |
actions: write | |
env: | |
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }} | |
K8S_NAMESPACE: chatforyou | |
DEPLOYMENT_NAME: chatforyou-python-api | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# Kubeconfig 설정 | |
- name: Configure Kubeconfig | |
run: | | |
mkdir -p ${HOME}/.kube | |
echo "${KUBE_CONFIG}" | base64 --decode > ${HOME}/.kube/config | |
export KUBECONFIG=${HOME}/.kube/config | |
# 타임스탬프 생성 (KST 기준) | |
- name: Generate TIMESTAMP in KST | |
id: timestamp | |
run: | | |
export TZ=Asia/Seoul | |
TIMESTAMP=$(date '+%Y%m%d%H%M%S') | |
echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV | |
echo "IMAGE_URI=ghcr.io/sejonj/chatforyou-python-api:$TIMESTAMP" >> $GITHUB_ENV | |
# Docker 빌드 및 푸시 (GitHub Packages 사용) | |
- name: Build Docker Image | |
run: | | |
docker build \ | |
--file Dockerfile \ | |
--tag $IMAGE_URI \ | |
. | |
- name: Push Docker Image to GitHub Packages | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
docker push $IMAGE_URI | |
# Kubernetes 배포 | |
- name: Deploy to Kubernetes | |
run: | | |
kubectl set image deployment/$DEPLOYMENT_NAME \ | |
chatforyou-python-api=$IMAGE_URI \ | |
-n $K8S_NAMESPACE | |
kubectl rollout status deployment/$DEPLOYMENT_NAME -n $K8S_NAMESPACE |