Project Management/Github

MS - GitHub Actions로 개발 주기 자동화 학습 정리

bluebamus 2023. 5. 31.

1. ep2-1. GitHub Actions 소개

   1. actions 탭으로 이동 

   2. template 선택

   3. 오른쪽 상단의 start commit 버튼 누르고 commit 

   4. {root}/workflows/***.yml 파일이 생성됨

   5. 다시 actions 탭으로 이동하면 all flows의 동작 현황 확인 가능

   6. 배너 달기

      1. actions 탭의 해당 job으로 이동, 오른쪽의 점 세개 확장 메뉴를 누르면 뱃지 만들기 메뉴 확인

      2. 업데이트 이후 자동, 이전에는 마크다운 카피 후  README에 붙어 넣으면 가능

 

2. ep2-2. GitHub Actions 소개

   1. 용어 정리

      1. workflow

      2. job

      3. step

      4. action

   2. actions 탭에서 new workflow 버튼을 누르면 신규 template 선택 가능 

   3. yml 파일은 하나의 workflow라 생각하면 됨

   4. yml 기본 구성 요소

      1. name : worlflow 이름

      2. on : 동작 triger 

         1. 예시 :

on:
	push: //메인 브런치가 push가 될 때마다 실행
    	branches: [ main ]
    pull_request: //메인 브런치에 pull_request가 생길 때마다 실행
    	branches: [ main ]

         2. 참고 : 

            1.  https://docs.github.com/ko/actions/using-workflows/events-that-trigger-workflows

 

워크플로를 트리거하는 이벤트 - GitHub Docs

GitHub에 대한 특정 작업이 예약된 시간에 발생하거나 GitHub 외부의 이벤트가 발생할 때 실행되도록 워크플로를 구성할 수 있습니다.

docs.github.com

   5. cron을 이용해 스케줄링으로 동작하게 만들 수 있음

      1. 참고 :

         1. https://docs.github.com/ko/actions/using-workflows/events-that-trigger-workflows#schedule

 

워크플로를 트리거하는 이벤트 - GitHub Docs

GitHub에 대한 특정 작업이 예약된 시간에 발생하거나 GitHub 외부의 이벤트가 발생할 때 실행되도록 워크플로를 구성할 수 있습니다.

docs.github.com

      2. crontab guru tool 사용

         1. https://crontab.guru/

 

Crontab.guru - The cron schedule expression editor

 

crontab.guru

   6. jobs

      1. 예시 :

jobs:
	build: //job 이름
    		runs-on: ubuntu-latest //host 머신 정보
            
            strategy:
            	matrix:
                	node-versdion: [10.x, 12.x, 14.x]
                    
            steps:
            - uses: actions/checkout@v2 //checkout 명령어 <-- 여기서
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
              	node-version: ${{ matrix.node-version }} // <-- 여기까지
            - run: npm ci
            - run: npm run build --if-present
            - run: npm test

      2. actions는 코드를 복사해서 가상머신에 다운받아주는 명령어

         1. 참고 :

            1. https://github.com/actions/checkout

 

GitHub - actions/checkout: Action for checking out a repo

Action for checking out a repo. Contribute to actions/checkout development by creating an account on GitHub.

github.com

3. ep3-1. GitHub Actions 실무에 사용하기 1편: 코드, 빌드, 테스트

   1. 메트릭스 빌드란?

      1. 참고 :

      2. 설정 방법 :

jobs:
	build:
    	runs-on: ubuntu-latest
        
        strategy:
        	matrix:
            	os: [ubuntu-latest, windows-2016]
                node-version: [12.x, 14.x]

 

 2. build와 test의 분리

         1. 현재 코드 :

jobs:
	build:
    	
        runs-on: ubuntu-latest
        
        strategy:
        	matrix:
            	node-version: [10.x, 12.x, 14.x]
                
        steps:
        - uses: actions/checkout@v2
        - name: Use Node.js ${{ matrix.node-version }}
          uses: actions/setup-node@v1
          with:
          	node-version: ${{ matrix.node-version }}
        - run: npm ci
        - run: npm run build --if-present
        - run: npm test

         2. 수정 코드 :

jobs:
	build:
    	
        runs-on: ubuntu-latest
 		steps:
        - uses: actions/checkout@v2
        - name: npm install and build webpack
          run:
          	npm install
            npm run build
    
    test:
        runs-on: ubuntu-latest
        
        strategy:
        	matrix:
            	os: [ubuntu-latest, windows-2016]
            	node-version: [12.x, 14.x]
                
        steps:
        - uses: actions/checkout@v2
        - name: Use Node.js ${{ matrix.node-version }}
          uses: actions/setup-node@v1
          with:
          	node-version: ${{ matrix.node-version }}
        - name: npm install and test
		  run:
          	npm install
            npm test
          env:
          	CI: true

4. ep3-2. GitHub Actions 실무에 사용하기 1편: 코드, 빌드, 테스트

   1. ep3-1의 분리 과정에서 에러가 난 이유?

      1. 정의 : 모든 job은 각각 다른 host에서 동작한다 때문에, build에서 사용된 결과물을 test에서 사용해야 함

      2. 방법 : github 자체에서 제공하는 built-in 아티팩트 스토리지를 사용하면 된다

      3. 예시 :

jobs:
	build:
    	
        runs-on: ubuntu-latest
 		steps:
        - uses: actions/checkout@v2
        - name: npm install and build webpack
          run:
          	npm install
            npm run build
        - uses: actions/upload-artifact@master
          with:
          	name: webpack artifacts
            path: public/
    
    test:
        runs-on: ubuntu-latest
        
        strategy:
        	matrix:
            	os: [ubuntu-latest, windows-2016]
            	node-version: [12.x, 14.x]
                
        steps:
        - uses: actions/checkout@v2
        - uses: actions/download-artifact@master
          with:
          	name: webpack artifacts
            path: public/
        - name: Use Node.js ${{ matrix.node-version }}
          uses: actions/setup-node@v1
          with:
          	node-version: ${{ matrix.node-version }}
        - name: npm install and test
		  run:
          	npm install
            npm test
          env:
          	CI: true

   2. 저장소를 정의했는데도 에러가 난 이유?

      1. 정의 : 모든 job은 기본적으로 병렬로 같은 시간대에 동작하게 된다. 

      2. 방법 : 동작의 종속성을 정의해서 순서를 명시해주면 된다.

      3. 예시 :

    test:
    	needs: build
        runs-on: ubuntu-latest

5. ep4-1. GitHub Actions 실무에 사용하기 2편: 협업하기

   1. main 브랜치가 망가질까 걱정되는 경우

      1. settings 탭에 보면 branches라는 메뉴가 있고 "add branch protection rule"라는 버튼을 눌러 보호 설정 가능

         - 리뷰 횟수에 제한을 걸어 일정 수에 도달해야지 머지 가능

         - status checks(build 테스트) 완료시 머지 가능

         - admin도 무조건 이와 같은 제약을 따르게 강제할 수 있음

         - 기타 다른 설정들도 가능하며 main 외 정의한 브랜치들에게 모두 적용 가능함

6. ep4-2. GitHub Actions 실무에 사용하기 2편: 협업하기

   1. branches rule을 도입하면 PR이 엄청나게 쌓인다. 이를 관리하기 위해 라벨 기능을 도입할 수 있다

      1. 자동으로 설정 가능한 커스텀 라벨 설치 방법

         1. 상단 marketplace 메뉴를 눌러 label을 검색한다

         2. labeler를 추천하지만 기능이 복잡해 추가 학습이 요구된다 하지만 적극 추천한다

         3. label approved pull requests를 다음으로 추천한다 : 자동으로 PR에 대한 라벨을 추가해준다

            1. workflos에 labeling.yml 파일을 만들어 준다

            2. action의 호환 버전 확인은 marketplace의 버전 히스토리 확인 버튼을 통해 제공받을 수 있다 

            3. 라벨의 색과 같은 커스텀은 오른쪽 설정 아이콘을 통해 수정할 수 있다

            4. 예제:

name: Auto labeling
on: pull_request_review
jobs:
	labelWhenApproved:
    	name: Label when approved
        runs-on: ubuntu-latest
        steps:
        - name: Label approved pull requests
          uses: abinoda/label-when-approved-action@1.0.5
          env:
          	APPROVALS: "1"
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            ADD_LABEL: "approved"

7. ep4-3. GitHub Actions 실무에 사용하기 2편: 협업하기

   1. actions label commenter 기능 추가하기

      1. 기능 설명: invalid라는 라벨을 설정하면, 봇이 template 가이드 등을 댓글로 안내하고 이슈를 닫아준다

         1. 참고: https://github.com/peaceiris/actions-label-commenter

 

GitHub - peaceiris/actions-label-commenter: Label Commenter Action: Label triggered GitHub Action for posting a template comment

Label Commenter Action: Label triggered GitHub Action for posting a template comment, and automatically open/close/lock/unlock issues, pull-requests, and discussions. - GitHub - peaceiris/actions-l...

github.com

         2. 마켓플레이스: https://github.com/marketplace/actions/label-commenter

 

Label Commenter - GitHub Marketplace

Label triggered GitHub Actions for posting a template message, automatically close or reopen issues or pull requests

github.com

         3.설정 방법: .github안에 폴더 및 파일을 만들어 안내해주는 방법대로 설정하면 됨

8. ep5. 클로징

   1. 학습 더 하기

      1. github learning lab: https://github.com/apps/github-learning-lab

 

GitHub: Let’s build from here

GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

github.com

      2. github skill: https://github.com/skills

 

GitHub Skills

Learn how to use GitHub with interactive courses designed for beginners and experts. - GitHub Skills

github.com

      3. ms training cource: https://learn.microsoft.com/en-us/training/

 

Training

The skills required to advance your career and earn your spot at the top do not come easily. Now there’s a more rewarding approach to hands-on learning that helps you achieve your goals faster. Earn points, levels, and achieve more!

learn.microsoft.com

 

댓글