본문 바로가기
기술/토스 Slash

토스ㅣSLASH 23 - 유연하고 안전하게 배포 Pipeline 운영하기 - 9 (git을 통한 pipeline 관리, helm을 이용하여 다양화)

by MiewONE 2023. 7. 22.

토스ㅣSLASH 23 - 유연하고 안전하게 배포 Pipeline 운영하기 - 1 (기술 정리)

 

토스ㅣSLASH 23 - 유연하고 안전하게 배포 Pipeline 운영하기 - 1 (기술 정리)

토스 Slash 23에서 나오는 운영 방법 영상 속 개념, 도구 Pipeline Pipeline 운영 전략 (중앙화) GoCD(Go Continuous Delivery) - 도구 gocd-yaml-config-plugin GoCD Pipeline Wizard Pipeline As Code (PAC) GoCD Template Helm Template CI (Co

crongb.tistory.com

SCM으로 파이프라인 관리

GoCD 에서 템플릿을 추상화 하기 위해서는 GoCD Template을 사용해야하며 Template를 이용하기 위해서는 Admin -> Config Repositories에 들어가서 Add 를 통해 Template이 존재하는 레포를 연결 해줘야합니다.

 

Template repository가 제대로 연결된 모습입니다.

 

그러면 자동적으로 해당 템플릿을 활용하여 파이프라인이 생성되었습니다.

 

git 에 파이프라인을 추가하거나 수정을 하면 gocd 서버가 센싱을 통해 알아서 최신화를 시켜줍니다.

이제 yaml 으로 생성된 템플릿들을 git을 통해 관리하는 방법을 알아봤으니 k8s와 helm을 통해서 동적 template을 이용하는 방법을 실습해보도록 하겠습니다.

 

Helm으로 Dynamic Pipeline 만들기

helm 은 k8s 패키지 매니저로 k8s 애플리케이션을 쉽게 배포, 관리하는데 도움을 줍니다. 본래는 k8s에 사용하는 manifest yaml 들에 사용하는 건데 토스팀에서 gocd.yaml 파일에 적용하여 사용을 진행하였습니다.

 

위 내용처럼 git에 파이프라인 yaml 파일을 올리면 적용되지만 파이프라인 yaml을 일관성있게 관리하기가 힘듭니다. 회사에서 사용하는 도구라면 하나의 정의서만 가지고 있는게아니라 많은 수의 파일을 관리하다 보니 관리의 편의성이 필요했을 겁니다.

 

기존의 파이프 라인 yaml 

format_version: 10
pipelines:
  another_pipeliens:
    group: defaultGroup
    label_template: "${git[:7]}"
    materials:
      git:
        git: https://github.com/miewone/myfirstgocd
        username: dlsrk489@gmail.com
        shallow_clone: false
        auto_update: true
        branch: main
    stages:
      - build:
          jobs:
            build:
              tasks:
               - exec:
                   command: ./build.sh
      - test:
          jobs:
            test:
              tasks:
               - exec:
                   command: ./test.sh
      - deploy:
          jobs:
            deploy:
              tasks:
               - exec:
                   command: ./deploy.sh

helm 차트를 사용하기 위한 yaml 파일

--- usingHelmTempalte1.gocd.yaml
format_version: 10
pipelines:
  build-{{ .Values.name }}:
    group: {{ .Values.name }}
    environment_variables:

    label_template: "${git[:7]}"
    materials:
      git:
        git: https://github.com/miewone/myfirstgocd
        username: dlsrk489@gmail.com
        shallow_clone: false
        auto_update: true
        branch: {{ .Values.branch }}
    stages:
      {{- if .Values.buildOnly}}
      - build:
          jobs:
            build:
              environment_variables:
                BUILD_VERSION: 0.0.3
              tasks:
               - exec:
                  command: ./build.sh
                  run_if: {{ .Values.run_if_value }}
                   
      {{- else }}
      - build:
          jobs:
            build:
              tasks:
               - exec:
                  command: ./build.sh
                  run_if: {{ .Values.run_if_value }}
                   
      - test:
          jobs:
            test:
              tasks:
               - exec:
                  command: ./test.sh
                  run_if: {{ .Values.run_if_value }}
                   
      - deploy:
          jobs:
            deploy:
              tasks:
               - exec:
                  command: ./deploy.sh
                  run_if: {{ .Values.run_if_value }}
                   

      {{- end }}
--- values.yaml
buildOnly : true
name : dynamicPipeline
branch : main
run_if_value : passed

helm 차트의 기능을 사용하므로써 크게 다를거 없는 파일 내용이지만 상황에 따라 values.yaml 만을 관리하리하면 상태를 변경할 수 있게 되었습니다.

 

위 처럼 파일을 구성하고 `helm template helmusingmaked . -f values.yaml > ../../pipelines/makebyhelmchart.gocd.yaml` 명령어를 이용하면 아래와 같은 파일이 생성됩니다.

 

---
# Source: gocd-pipeline/templates/usingHelmTempalte1.gocd.yaml
format_version: 10
pipelines:
  build-dynamicPipeline:
    group: dynamicPipeline
    environment_variables:

    label_template: "${git[:7]}"
    materials:
      git:
        git: https://github.com/miewone/myfirstgocd
        username: dlsrk489@gmail.com
        shallow_clone: false
        auto_update: true
        branch: main
    stages:
      - build:
          jobs:
            build:
              environment_variables:
                BUILD_VERSION: 0.0.3
              tasks:
               - exec:
                  command: ./build.sh
                  run_if: passed

토스팀은 좀 더 나아가 `helm template ` 명령어를 github action을 이용하여 CI를 구성하여 template 명령어로 렌더링할때 실패하면 메신저로 통해 검증 오류가 있는것을 알렸습니다.

저는 여기서 github action에 내가 helm template 명령어로 만들지 않고 github에서 검증 절차 후 문제가 없으면 templates/ 안에 yaml 파일만 정의 해주고 values.yaml 을 수정을 하면 자동적으로 makedBygithubActionTemplate.gocd.yaml 같이 만들도록 하였습니다.

 

git push 후 action이 진행되고 자동적으로 values.yaml에 따라 파일들이 렌더링되어 레포지토리에 추가되고 gocd dashboard 에 추가됩니다.

 

이상으로 토스ㅣSLASH 23 - 유연하고 안전하게 배포 Pipeline 운영하기  영상 내용 실습하며 익히기를 마치겠습니다.

 

github action 파일

 

---
name : CI
on:
  push:
    branches:
     - main
jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          persist-credentials: false
      - name: Grant execute permission for script
        run: chmod +x ./.github/validate.sh

      - name: Validate
        run: ./.github/validate.sh
      
      - name: Commit and push if changed
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add -A
          if [[ $(git diff --cached | wc -l) -gt 0 ]]; then
            git commit -m "Update pipeline"
            git remote set-url origin https://x-access-token:${{ secrets.CI_GITHUB_TOKEN }}@github.com/${{ github.repository }}
            git push
          fi