# 복합 작업 만들기

이 자습서에서는 복합 작업을 빌드하는 방법을 알아봅니다.

## 소개

이 가이드에서는 패키지된 복합 작업을 만들고 사용하는 데 필요한 기본 구성 요소에 대해 알아봅니다. 작업을 패키지하는 데 필요한 구성 요소에 가이드의 초점을 맞추기 위해 작업 코드의 기능은 최소화됩니다. 작업은 "Hello World" 및 "Goodbye"를 인쇄하거나 사용자 지정 이름을 제공하는 경우 "Hello \[who-to-greet]" 및 "Goodbye"를 출력합니다. 이 작업은 또한 난수를 `random-number` 출력 변수에 매핑하고 `goodbye.sh`라는 스크립트를 실행합니다.

이 프로젝트를 완료한 후에는 고유한 복합 작업을 빌드하고 워크플로에서 테스트하는 방법을 이해해야 합니다.

> \[!WARNING]
> 워크플로와 작업을 만들 때는 코드가 공격자의 신뢰할 수 없는 입력을 실행할 수 있는지 항상 고려해야 합니다. 특정 컨텍스트는 공격자가 자신의 악성 콘텐츠를 삽입할 수 있으므로 신뢰할 수 없는 입력으로 취급해야 합니다. 자세한 내용은 [안전 사용 참조](/ko/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)을(를) 참조하세요.

### 복합 작업 및 재사용 가능한 워크플로

복합 작업을 사용하면 일련의 워크플로 작업 단계를 단일 작업으로 수집한 다음, 여러 워크플로에서 단일 작업 단계로 실행할 수 있습니다. 재사용 가능한 워크플로는 다른 워크플로 내에서 전체 워크플로를 실행할 수 있도록 하여 중복을 방지하는 또 다른 방법을 제공합니다. 자세한 내용은 [워크플로 구성 재사용](/ko/actions/using-workflows/avoiding-duplication)을(를) 참조하세요.

## 필수 조건

> \[!NOTE]
> 이 예제에서는 별도의 리포지토리 내에서 복합 작업을 만드는 방법을 설명합니다. 그러나 동일한 리포지토리 내에서 복합 작업을 만들 수 있습니다. 자세한 내용은 [복합 작업 만들기](/ko/actions/creating-actions/creating-a-composite-action#creating-a-composite-action-within-the-same-repository)을(를) 참조하세요.

시작하기 전에 .에 리포지토리를 만듭니다 GitHub.

1. 에 새 공용 리포지토리를 만듭니다 GitHub. 리포지토리 이름을 선택하거나 다음 `hello-world-composite-action` 예제를 사용할 수 있습니다. 파일은 프로젝트가 GitHub에 업로드된 후에 추가할 수 있습니다. 자세한 내용은 [새 리포지토리 만들기](/ko/repositories/creating-and-managing-repositories/creating-a-new-repository)을(를) 참조하세요.

2. 컴퓨터에 리포지토리를 복제합니다. 자세한 내용은 [리포지토리 복제](/ko/repositories/creating-and-managing-repositories/cloning-a-repository)을(를) 참조하세요.

3. 터미널에서 디렉터리를 새 리포지토리로 변경합니다.

   ```shell copy
   cd hello-world-composite-action
   ```

4. ```
          `hello-world-composite-action` 리포지토리에서 다음 예제 코드로 `goodbye.sh`라는 새 파일을 만드세요.
   ```

   ```shell copy
   echo "echo Goodbye" > goodbye.sh
   ```

5. 터미널에서 `goodbye.sh` 실행 파일을 만듭니다.

   <div class="ghd-tool linux">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git add --chmod=+x -- goodbye.sh
   ```

   </div>

6. 터미널에서 `goodbye.sh` 파일을 체크 인합니다.

   <div class="ghd-tool linux">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

## 작업 메타데이터 파일 만들기

1. ```
          `hello-world-composite-action` 리포지토리에서 `action.yml`라는 새 파일을 만들고 다음 예제 코드를 추가합니다. 이 구문에 대한 자세한 내용은 [AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions)을(를) 참조하세요.
   ```

   ```yaml copy
   name: 'Hello World'
   description: 'Greet someone'
   inputs:
     who-to-greet:  # id of input
       description: 'Who to greet'
       required: true
       default: 'World'
   outputs:
     random-number:
       description: "Random number"
       value: ${{ steps.random-number-generator.outputs.random-number }}
   runs:
     using: "composite"
     steps:
       - name: Set Greeting
         run: echo "Hello $INPUT_WHO_TO_GREET."
         shell: bash
         env:
           INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}

       - name: Random Number Generator
         id: random-number-generator
         run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
         shell: bash

       - name: Set GitHub Path
         run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
         shell: bash
         env:
           GITHUB_ACTION_PATH: ${{ github.action_path }}

       - name: Run goodbye.sh
         run: goodbye.sh
         shell: bash

   ```

   이 파일은 `who-to-greet` 입력을 정의하고, 임의 생성된 숫자를 `random-number` 출력 변수에 매핑하며, 실행기 시스템 경로에 동작의 경로를 추가(실행 중 `goodbye.sh` 스크립트를 찾기 위해)하고, `goodbye.sh` 스크립트를 실행합니다.

   출력 관리에 대한 자세한 정보는 [메타데이터 구문 참조](/ko/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions)을(를) 참조하세요.

   ```
          `github.action_path` 사용 방법에 대한 자세한 내용은 [AUTOTITLE](/actions/learn-github-actions/contexts#github-context)을(를) 참조하세요.
   ```

2. 터미널에서 `action.yml` 파일을 체크 인합니다.

   ```shell copy
   git add action.yml
   git commit -m "Add action"
   git push
   ```

3. 터미널에서 태그를 추가합니다. 이 예제에서는 `v1`이라는 태그를 사용합니다. 자세한 내용은 [사용자 지정 작업 정보](/ko/actions/creating-actions/about-custom-actions#using-release-management-for-actions)을(를) 참조하세요.

   ```shell copy
   git tag -a -m "Description of this release" v1
   git push --follow-tags
   ```

## 워크플로에서 작업 테스트

다음 워크플로 코드는 [복합 작업 만들기](/ko/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)에서 수행한 완료된 hello world 작업을 사용합니다.

워크플로 코드를 다른 리포지토리의 `.github/workflows/main.yml` 파일에 복사하여 `OWNER` 및 `SHA`를 각각 리포지토리 소유자와 사용할 커밋의 SHA로 바꿉니다.
`who-to-greet` 입력을 자신의 이름으로 바꿀 수도 있습니다.

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v6
      - id: foo
        uses: OWNER/hello-world-composite-action@SHA
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
```

리포지토리에서 **작업** 탭을 클릭하고 최신 워크플로 실행을 선택합니다. 출력에는 “Hello Mona the Octocat”, “Goodbye” 스크립트의 결과와 난수가 포함되어야 합니다.

## 동일한 리포지토리 내에서 복합 작업 만들기

1. ```
          `hello-world-composite-action`라는 새 하위 폴더를 만듭니다. 이 폴더는 리포지토리 내의 모든 하위 폴더에 배치할 수 있습니다. 하지만 조직을 더 쉽게 정리하기 위해서는 `.github/actions` 하위 폴더에 배치하는 것이 더 좋습니다.
   ```

2. ```
          `hello-world-composite-action` 폴더에서 `goodbye.sh` 스크립트를 만들기 위해 동일한 단계를 수행합니다.
   ```

   ```shell copy
   echo "echo Goodbye" > goodbye.sh
   ```

   <div class="ghd-tool linux">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git add --chmod=+x -- goodbye.sh
   ```

   </div>

   <div class="ghd-tool linux">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

3. ```
          `hello-world-composite-action` 폴더에 `action.yml`의 단계에 따라 [](/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file) 파일을 만듭니다.
   ```

4. 이 작업을 진행할 때는 `action.yml` 키에 복합 작업의 `uses` 파일이 위치한 폴더의 상대 경로를 지정합니다. 아래 예제에서는 `.github/actions/hello-world-composite-action` 폴더에 작업이 있다고 가정합니다.

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v6
      - id: foo
        uses: ./.github/actions/hello-world-composite-action
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
```