# Github Actions

## Github Actions

> 특정 이벤트가 발생했을 때 원하는 작업을 자동으로 수행하게 만들어주는 도구

* 프로젝트 루트 경로에 `.github/workflows/` 디렉토리를 생성하고  `.yml` 파일을 만들어 워크플로우를 정의한다.

```yaml
// workflows 이름
name: CI

// workflows를 실행시킬 트리거
// 각 이벤트 하위에 지정하는 상세 설정은 AND 연산자를 사용하여 조건이 충족되어야 실행됨
on:
  push:
    branches:
      - main
  pull_request:
      types: [opened, synchronize, reopened]

jobs:
  build:
   // job을 실행시킬 환경을 지정
    runs-on: ubuntu-latest 
    steps:
      // uses는 이미 만들어둔 Action을 사용하겠다는 것
      // checkout은 'runs-on'에 명시한 워크플로우 실행 환경에서 해당 프로젝트를 불러오는 동작을 의미
      // 워크플로우의 첫 번째 단계에서 사용되며 꼭 필요하다.
      - name: Checkout
        uses: actions/checkout@v3
     // node.js 환경 세팅
      - name: Set up Node.js
        uses: actions/setup-node@v3
        // with을 사용하여 액션에 값 전달
        with:
          node-version: '16'
          // 종속성을 관리하고 캐싱할 패키지매니저 지정
          cache: npm
      // 종속성 설치
      - name: Install dependencies
      // run은 package.json에 정의한 스크립트 명령어를 실행시키기 위해 사용됨
        run: npm ci
      - name: Lint
        run: npx eslint --ext .js,.jsx,.ts,.tsx .
      - name: Compile to check syntax
        run: npx tsc --noEmit
      - name: Run tests
        run: npx jest --verbose --coverage
      - name: Build
        run: npm run build
      - name: Run E2E test
        run: HEADLESS=true npm run ci
```

### Events

> **When** - 수행할 작업이 트리거되는 상황

* github에서 발생되는 대부분의 이벤트를 지정할 수 있다.
  * ex) push, merge, pr 생성 등
* 이벤트가 호출되면 워크플로우가 실행된다.&#x20;

### Workflow

> 특정 이벤트가 발생했을 때 실행되는 작업들의 집합

* 하나의 `workflows`는 하나 또는 다수의 `job`을 갖는다.

### Job

> 워크플로우 내에서 실행되는 독립적인 작업

* 기본적으로 병렬로 실행된다.
* `job`은 하나 이상의  `step`으로 구성된다.
* `shell script` 또는 `npm 명령어` 작성 가능

### Actions

> 특정 작업, 명령어를 실행하는 단위

* 자주 수행되는 작업을 액션으로 정의하여 재사용성을 높힌다.
* Github actions 에는 사전에 정의된 다양한 액션들을 제공한다.&#x20;

#### Composite Action

> 여러 개의 단일 액션으로 구성되어 더 복잡한 작업을 수행하기 위해 사용된다.

* Composite Action은 워크플로우와 독립적으로 관리하기 위해 별도의 디렉토리를 생성하여 관리하자.
* Composit Action마다 개별 폴더를 만들고  `action.yml` 파일을 생성하여 정의한다.
* `using: 'composite'` 로 선언해야 한다.
* 해당 액션을 사용하는 곳에서 `uses`를 통해 액션의 상대 경로를 지정하여 사용한다.
  * ```
    uses: ./.github/actions/yarn-install
    ```

<details>

<summary>.github/actions/yarn-install/action.yml</summary>

```yaml
name: 'Monorepo install (yarn)'
description: 'Run yarn install'

runs:
  using: 'composite'

  steps:
    - name: Expose yarn config as "$GITHUB_OUTPUT"
      id: yarn-config
      shell: bash
      // 뒤에서 사용하기 위해 캐쉬폴더를 변수에 담아 output에 값을 내보낸다
      run: |
        echo "CACHE_FOLDER=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT

    - name: Restore yarn cache
      uses: actions/cache@v3
      id: yarn-download-cache
      with:
        path: ${{ steps.yarn-config.outputs.CACHE_FOLDER }}
        key: yarn-download-cache-${{ hashFiles('yarn.lock') }}
        restore-keys: |
          yarn-download-cache-

    - name: Restore yarn install state
      id: yarn-install-state-cache
      uses: actions/cache@v3
      with:
        path: .yarn/ci-cache/
        key: ${{ runner.os }}-yarn-install-state-cache-${{ hashFiles('yarn.lock', '.yarnrc.yml') }}

    - name: Install dependencies
      shell: bash
      run: |
        yarn install --immutable --inline-builds
      env:
        YARN_ENABLE_GLOBAL_CACHE: 'false'
        YARN_INSTALL_STATE_PATH: .yarn/ci-cache/install-state.gz # Very small speedup when lock does not change

```

</details>

### Runner

> 작업을 수행할 실행 환경으로 github에서 호스팅하여 워크플로우를 실행시키기 위해 사용된다.

* `job`을 실행하는 것이 바로 **`Runner`**,&#x20;
* `job`은 독립적인 `Runner Container`에서 실행된다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://taewoongs-organization.gitbook.io/jtwjs-dev-wiki/dev_note/git/github-actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
