github action이란?
공식 문서에 따르면 소프트웨어 개발 workflow를 레포지토리에서 바로 자동화하고 개별화하여 실행할 수 있도록 도와주는 도구이다.
CI/CD를 포함하여 원하는 작업을 만들고 공유하며 작업을 합칠 수 있다.
https://docs.github.com/en/actions
GitHub Actions Documentation - GitHub Docs
Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized wo
docs.github.com
즉, 깃허브 액션은 소프트웨어 개발에 필요한 작업 주기를 자동화하는 도구이다.
깃허브 액션을 사용하면 특정 이벤트가 발생했을 때 원하는 특정 명령 집합을 자동으로 실행시킬 수 있다.
(깃허브 액션은 레포지토리를 public으로 설정하면 무료로 사용할 수 있다)
액션 알아보기
깃허브 액션을 추가하기 위해서는 깃허브 원격 저장소에서 제공하는 템플릿 파일을 수정해서 사용할 수도 있고, 지역 저장소에서 .github/workflows 위치에 yml 파일을 생성하여 반영할 수도 있다.
아래는 깃허브에서 제공하는 node.js 깃허브 액션 템플릿이다.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- name: workflow의 이름
- on: workflow를 실행시킬 이벤트를 명시해준다. 예시에서는 master 브랜치에 push와 pull request 이벤트가 발생했을 때 동작하도록 설정해준 것이다.
- jobs: 같은 환경에서 실행할 작업을 정의한다.
- build: 특정 job의 이름 (원하는 이름으로 설정하면 된다)
- runs-on: 해당 job이 실행되는 환경을 정의한다. 예시에서는 우분투 운영체제에서 실행시킨다는 것을 의미한다.
- steps: 특정 job에 포함된 순차적인 명령들이다.
- uses: 현재 단계에서 사용할 액션을 정의한다.
- name: 현재 간계의 이름을 명시할 수 있으며 이름을 지정하면 원격 저장소 Actions 탭 페이지의 로그에서 해당 단계의 이름을 확인할 수 있다.
- run: 해당 job이 실행되는 환경에서 실행시킬 셸 명령어를 작성해줄 수 있다. 예시에서의 run: npm ci는 job 실행 환경에서 npm ci를 실행시키기 위함이다.
깃허브 액션 사용해보기
깃허브 액션을 사용해 master 브랜치에 push 및 pull request 이벤트가 발생했을 때 테스트 파일을 실행하도록 설정해보자.
test.spec.js라는 테스트 파일을 생성 후 실행을 시키면 되는데, node.js 환경에서 package.json scripts에 test 파일을 실행시키는 명령을 등록하면 복잡한 명령을 쉽게 실행시킬 수 있다.
name: bulid
on:
push:
pull_request:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x]
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm test
이렇게 설정 후에 push를 진행하면 github action에 등록해놓은 동작들이 자동으로 수행되며 테스트 결과를 알려준다.
'Etc > Git , Github' 카테고리의 다른 글
Git이 보는 파일의 4가지 상태 (0) | 2021.08.22 |
---|
댓글