[Github Actions] GKE 배포 자동화

Seunghyun Moon·2024년 1월 13일
0

Github Actions

목록 보기
1/1

Github Actions란

Github Actions 는 Github에서 무료 또는 유료 플랜으로 제공합니다. Github 네이티브해서 서버관리등이 필요 없는게 장점이고 이벤트 기반(특정 브랜치로 푸시) 등 CI/CD 뿐이 아닌 여러가지 작업들을 할 수 있습니다.

오늘의 이슈

github으로 코드 푸시, 컨테이너라이즈, GCP의 artifact registry, GKE를 이용한 배포까지 알아봅니다.

준비물

  • Github 계정
  • GCP 계정
    • GKE 클러스터, artifact registry, service account, service account key

시작

코드 작업

Github 리포에 코드를 작업합니다.
.github/workflows 폴더에는 Github Actions 사용을 위한 작업이 정의된 yml 파일이 위치합니다.

.github/workflows/google.yml

# # This workflow will build a docker container, publish it to Google Container Registry, and deploy it to GKE when there is a push to the "main" branch.
# #
# # To configure this workflow:
# #
# # 1. Ensure that your repository contains the necessary configuration for your Google Kubernetes Engine cluster, including deployment.yml, kustomization.yml, service.yml, etc.
# #
# # 2. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
# #
# # 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below).
# #
# # For more support on how to run the workflow, please visit https://github.com/google-github-actions/setup-gcloud/tree/master/example-workflows/gke-kustomize

name: Deploy Hive-Web

on:
  push:
    branches:
    - 'develop'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:

    - name: code checkout
      uses: actions/checkout@v4

    - id: auth
      name: Authenticate to Google Cloud
      uses: google-github-actions/auth@v0.4.0
      with:
        project_id: ${{ secrets.GOOGLE_PROJECT_ID }}
        credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
  
    - name: build and push the docker image
      env:
        GOOGLE_PROJECT_ID: ${{ secrets.GOOGLE_PROJECT_ID }}
      run: |
        gcloud auth configure-docker asia-northeast3-docker.pkg.dev
        docker build -t asia-northeast3-docker.pkg.dev/$GOOGLE_PROJECT_ID/hivelab-repo-demo/hivelab-web:latest .
        docker push asia-northeast3-docker.pkg.dev/$GOOGLE_PROJECT_ID/hivelab-repo-demo/hivelab-web:latest

    - id: 'get-credentials'
      name: Configures authentication to a GKE cluster via a kubeconfig file
      uses: 'google-github-actions/get-gke-credentials@v2'
      with:
        cluster_name: hivelab-cluster-demo
        location: asia-northeast3

    - name: deploy to gke
      env:
        GOOGLE_PROJECT_ID: ${{ secrets.GOOGLE_PROJECT_ID }}
      run: |
        sed -i "s/GOOGLE_PROJECT_ID/$GOOGLE_PROJECT_ID/g" deployments.yaml
        kubectl apply -f deployments.yaml

간단 설명을 하면,
develop 브랜치에 push 됐을 때 아래의 deploy job이 실행됩니다.

  • steps
  1. 코드를 가상환경에 checkout
  2. gcloud 사용해 gcp에 인증
  3. 이미지 빌드
  4. gke 의 api 서버와 통신하기 위한 클러스터 컨피그 설정
  5. gke에 apply

간단한 작업입니다. 차후에 helm 관련해 작업을 해주고 argoCD 와의 연동까지 해주면 제법 그럴듯한 파이프라인이 완성될 수 있습니다.

Github setup

${{ secrets.GOOGLE_PROJECT_ID }}
와 같은 외부 변수를 사용하는데, 이 값은 리포 > settings > secrets > actions 에서 넣어줍니다.

확인

이제 로컬등에서 작업 후 push를 해봅니다.

파이프라인이 동작하고 성공실패 와 로그까지 꽤 자세히 나옵니다.

성공 케이스

실패 케이스



참고
https://youtu.be/7NyJWuRHQDA?si=9zu93eMf5DsknUaf
https://docs.github.com/ko/enterprise-cloud@latest/actions/quickstart
https://github.com/google-github-actions/auth
https://cloud.google.com/blog/products/identity-security/enabling-keyless-authentication-from-github-actions
https://github.com/google-github-actions/get-gke-credentials

profile
I live fullest

0개의 댓글