문서정보 : 2023.04.11.~05.09. 작성, 작성자 @SAgiKPJH
Git Hub에서 CICD를 활용하는 방법을 익힌다.
- : 1. CI/CD?
- : 2. Git Hub Action
- : 3. Git Hub Action 구성요소
- : 4. CI/CD 구축 (1) - 기본적인 Message 출력
- : 5. CI/CD 구축 (2) - Tag에 따라 Message 출력
- : 6. CI/CD 구축 (3) - Marster Brantch Push시 출력
- : 7. CI/CD 구축 (4) - 자동 빌드에 대한 내용
- : 8. CI/CD 구축 정리
- Continuous Integration/Continuous Delivery (지속적 통합/지속적 배포)
- "CI"는 개발자를 위한 자동화 프로세스인 지속적인 통합(Continuous Integration)
- "CD"는 지속적인 서비스 제공 (Continuous Delivery) 또는 지속적인 배포 (Continuous Deployment)
- 애플리케이션 개발 단계를 자동화하여 애플리케이션을 더욱 짧은 주기로 고객에게 제공하는 방법이다.
- 간단히 말해서, 특정한 시간에 통합, 빌드, 테스트, 배포등의 동작을 자동화하는 방법이다.
- Event
- Action을 실행할 이벤트 유형 및 트리거를 정의합니다.
- Job
- Action을 실행하는 단위로, 하나 이상의 Step으로 구성됩니다.
- Step
- Job에서 실행할 명령어와 작업을 정의합니다.
- Runner
- Action이 실행될 환경을 정의합니다.
- Workflow
- Event, Job, Step을 조합하여 작업의 흐름과 실행 순서를 정의합니다.
- GitHub에서 이미 제작한 Repository -> Actions를 접속한다.
- Simple workflow를 선택한다.
- 자동으로 생성되는 blank.yml을 확인한다.
- 이러한 yml 파일을 다음과 같이 수정한다. (파일 이름을 CICD-Test.yml로 설정하였다)
name: CI/CD-Test # workflow 이름 # Controls when the workflow will run on: push: # push 이벤트가 발생하면 branches: [ "main" ] # "main" 브랜치에서만 동작 jobs: build: # job 이름 runs-on: ubuntu-latest # 사용할 runner의 운영체제 steps: # 실행할 작업 목록 - uses: actions/checkout@v3 # v3 버전의 actions/checkout 를 사용 (코드를 로컬 환경으로 거져온다) - name: Run a one-line script # 작업 이름 run: echo Hello, world! # 실행할 명령어 - name: Run a multi-line script # 작업 이름 run: | # 실행할 명령어들 echo Add other actions to build, echo test, and deploy your project.
- 처음 name을 정의하면 다음과 같이 이름이 등록이 된다. 그렇지 않으면 파일 이름이 등록된다.
- 작성 후 commit한다.
- 이후 Action에 접속하여 실행여부를 확인한다.
- 이후 다른 내용을 Commit 한 후 상황을 재 확인한다.
-
- GitHub Repository
.github/workflow
폴더 내부에 존재하는 yml을 통해 Actions가 동작한다.
- GitHub Repository
-
- yml 파일을 만든다.
on: push: # push 이벤트 감지 tags: # 태그 이벤트 중 - 'release*' # release 태그가 붙은 커밋 감지 jobs: build: # build 작업 runs-on: ubuntu-latest # 우분투 환경에서 실행 steps: # 실행할 작업들 - name: Checkout code # 코드 체크아웃 액션 실행 (코드를 로컬 환경으로 거져온다) uses: actions/checkout@v2 - name: Run custom command # 사용자 정의 명령어 실행 if: contains(github.ref, 'refs/tags/release') # github.ref에 'refs/tags/release' 문자열이 포함되어 있는지 확인 run: | # 실행할 명령어들 echo Add other actions to build, echo test, and deploy your project.
-
- GitHub에서 Tag를 생성한다.
-
- 결과를 확인한다.
- 특정 Brantch에 push할 때 Actions가 동작하도록 한다.(main Brantch)
on: push: # push 이벤트 감지 pull_request: # 브랜치 이벤트 중 - 'main*' # main로 시작하는 브랜치에 대한 pull request 이벤트 감지 jobs: build: # build 작업 runs-on: ubuntu-latest # 우분투 환경에서 실행 steps: # 실행할 작업들 - name: Checkout code # 코드 체크아웃 액션 실행 (코드를 로컬 환경으로 거져온다) uses: actions/checkout@v2 - name: Run custom command # 사용자 정의 명령어 실행 if: startsWith(github.ref, 'refs/heads/master') # github.ref가 'refs/heads/master'로 시작하는지 확인 run: | # 실행할 명령어들 echo Add other actions to build, # 빌드할 다른 작업 추가 echo test, and deploy your project. # 프로젝트 테스트 및 배포
- Brantch를 만들어 main으로 push한다.
- 특정 Brantch에 Push가 되었을 때, 자동 배포와 더불어 특정 내용으로 배포할 수 있도록 한다.
- Main 브랜치에 Merge할 때, "Release"라는 문자로 시작하는 내용으로 Commit 했을 경우에 같은 내용으로 Release를 구성할 수 있도록 한다.
- Release를 구성하도록 하기 위해서는 GitHub Token이 필요하다. 이때 자동으로 제공되는 GITHUB_TOKEN를 활용하면 된다.
- 수동으로 GitHub Token 생성 방법
- GitHub > 계정 Setting > Developer settings > Personal access tokens > Generate new token
- GitHub 환경 변수 지정 방법
- GitHub > 원하는 Repository > Setting > Secret and variables > Actions > New repository secret > "JJU_TOKEN" 생성후 Token 값을 입력 > Add secre
- 이러한 Secret 환경 변수는 계정을 가지고 있지 않은 한은 접근이 불가능 하다.
- 다음과 같이 활용할 수 있다.
token: ${{ secrets.JJU_TOKEN }}
- 다음과 같이 yml 파일을 구성한다.
name: Deploy to production # workflow 이름 지정 on: push: branches: - 'main*' # main로 시작하는 브랜치에 대한 push 이벤트 감지 jobs: build-and-deploy: runs-on: ubuntu-latest # 실행할 runner 환경 지정 steps: - name: Checkout code uses: actions/checkout@v2 with: ref: ${{ github.ref }} # 코드를 checkout - name: Build and test # 빌드 및 테스트 수행 run: | # Add build and test steps here # 최근 commit의 메시지를 가져와서 message.txt 파일에 저장하고, output으로 message 변수에 저장 - name: Merge commit message run: | git log -1 --pretty=%B > message.txt echo "::set-output name=message::$(cat message.txt)" id: merge_message - name: Get Release Version if: startsWith(steps.merge_message.outputs.message, 'Release') id: extract_release_version # release 버전 추출 run: | echo "::set-output name=version::$(echo ${{ steps.merge_message.outputs.message }} | sed 's/Release //')" # release 태그로부터 version 정보 추출하여 output으로 설정 - name: Create release tag if: startsWith(steps.merge_message.outputs.message, 'Release') # startsWith 함수를 사용하여 message가 'Release'로 시작하는지 확인하고, tag 생성 uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: ${{ steps.extract_release_version.outputs.version }} # 반드시 v0.1.1와 같이 v 접두사가 들어가야 한다. release_name: Release v${{ github.run_number }} body: ${{ steps.merge_message.outputs.message }} draft: false prerelease: false - name: Deploy to production # 배포 단계 추가 if: startsWith(steps.merge_message.outputs.message, 'Release') run: | # Add deployment steps here
- main Brantch로 "Release v0.1.3"Command을 시도한다.
- Release 결과를 다음과 같이 확인할 수 있다.
- ssh 그리고 linux 언어가 동작하는 환경에 대해서 프로젝트를 빌드할 수 있다면, 자동화 빌드도 가능할 것으로 추측이 된다.
- name: Build and test run: | # Add build and test steps here dotnet build MySolution.sln dotnet test MyTestsProject.csproj --logger "trx;LogFileName=test_results.xml"
- c#에 대한 예시는 다음 링크를 통해 확인할 수 있다.
- yml을 통해 actions를 구성한다.
- yml 전반부 : 이름 및 이벤트 정의
name: Deploy to production # workflow 이름 지정 on: push: branches: - 'main*' # main로 시작하는 브랜치에 대한 push 이벤트 감지 branches: [ "main" ] # "main" 브랜치에서만 동작 pull_request: # 브랜치 이벤트 중 - 'main' # main 브랜치에 대한 pull request 이벤트 감지 tags: # 태그 이벤트 중 - 'release' # release 태그가 붙은 커밋 감지
- 환경 지정
jobs: build: # build 작업 runs-on: ubuntu-latest # 실행할 runner 환경 지정 build-and-deploy: runs-on: ubuntu-latest # 우분투 환경에서 실행
- Git Checkout
steps: # 실행할 작업들 - name: Checkout code # 코드 체크아웃 액션 실행 (코드를 로컬 환경으로 거져온다) uses: actions/checkout@v2 - uses: actions/checkout@v3 # v3 버전의 actions/checkout 를 사용 (코드를 로컬 환경으로 거져온다) - name: Checkout code uses: actions/checkout@v2 with: # Action에 전달할 인자(argument)를 지정 ref: ${{ github.ref }} # ref는 체크아웃할 브랜치를 지정 # ${{ github.ref }} 현재 이벤트에서 체크아웃해야 할 브랜치
- Build and test
- name: Build and test run: | # Add build and test steps here dotnet build MySolution.sln dotnet test MyTestsProject.csproj --logger "trx;LogFileName=test_results.xml"
- 기타 활용
- name: Run custom command # 사용자 정의 명령어 실행 if: startsWith(github.ref, 'refs/heads/master') # github.ref가 'refs/heads/master'로 시작하는지 확인 run: | # 실행할 명령어들 echo Add other actions to build, # 빌드할 다른 작업 추가 echo test, and deploy your project. # 프로젝트 테스트 및 배포 - name: Run custom command # 사용자 정의 명령어 실행 if: contains(github.ref, 'refs/tags/release') # github.ref에 'refs/tags/release' 문자열이 포함되어 있는지 확인 run: | # 실행할 명령어들 echo Add other actions to build, echo test, and deploy your project.
- Get Release
- name: Get Release Version if: startsWith(steps.merge_message.outputs.message, 'Release') id: extract_release_version # release 버전 추출 run: | echo "::set-output name=version::$(echo ${{ steps.merge_message.outputs.message }} | sed 's/Release //')" # release 태그로부터 version 정보 추출하여 output으로 설정
- release
- name: Create release tag if: startsWith(steps.merge_message.outputs.message, 'Release') # startsWith 함수를 사용하여 message가 'Release'로 시작하는지 확인하고, tag 생성 uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: ${{ steps.extract_release_version.outputs.version }} # 반드시 v0.1.1와 같이 v 접두사가 들어가야 한다. release_name: Release v${{ github.run_number }} body: ${{ steps.merge_message.outputs.message }} draft: false prerelease: false