Skip to main content

GitHub Copilot 명령 줄 인터페이스 (CLI)를 프로그래밍 방식으로 실행하는 방법

터미널, 스크립트 또는 Actions 워크플로에서 코파일럿 CLI를 사용합니다.

소개

대화형 세션을 시작할 필요 없이, 단일 명령어를 활용하여 코파일럿 CLI에 즉시 프롬프트를 전달할 수 있습니다. 이렇게 하면 터미널에서 직접 Copilot를 사용할 수 있을 뿐만 아니라, 스크립트, CI/CD 파이프라인 및 자동화 워크플로에서 CLI를 프로그래밍 방식으로 사용할 수 있습니다.

코파일럿 CLI를 프로그래밍 방식으로 활용하려면 아래에 제시된 방법 중 하나를 선택해 보시기 바랍니다.

  •         `copilot` 명령과 `-p` 또는 `--prompt` 명령줄 옵션을 사용한 후 프롬프트를 입력하세요.
    
    Shell
    copilot -p "Explain this file: ./complex.ts"
    
  •         `copilot` 명령에 프롬프트를 파이프합니다.
    
    Shell
    echo "Explain this file: ./complex.ts" | copilot
    

    참고

    프롬프트나 -p 또는 --prompt 옵션이 제공되면 파이프 입력은 처리되지 않습니다.

코파일럿 CLI를 프로그래밍 방식으로 활용하기 위한 팁

  •         **정확한 프롬프트를 제공합니다.** 명확하고 명확한 지침은 모호한 요청보다 더 나은 결과를 생성합니다. 파일 이름, 함수 이름, 정확한 변경 내용과 같은 컨텍스트를 더 많이 제공할수록 Copilot이(가) 추측할 필요성이 줄어듭니다.
    
  •           **프롬프트를 신중하게 따옴표 처리하기** — 셸에서 특수 문자가 해석되는 것을 방지하려면 프롬프트를 작은따옴표로 사용하면 됩니다.
    
  •         **항상 최소한의 권한을 부여하십시오** — Copilot에게 명령줄 옵션 `--allow-tool=[TOOLS...]``--allow-url=[URLs...]`을 사용해 작업을 완료하는 데 필요한 도구와 접근 권한만 부여하십시오. 샌드박스 환경에서 작업하지 않는 한 지나치게 허용되는 옵션(예: `--allow-all`)을 사용하지 마세요.
    
  • 출력을 캡처할 때 (자동)를 사용합니다-s. 이렇게 하면 세션 메타데이터가 표시되지 않으므로 텍스트가 정리됩니다.
  •         **사용 `--no-ask-user`** 에이전트가 명확한 질문을 하지 못하도록 합니다.
    
  • 환경 전체에서 일관된 동작을 위해 명시적으로 ****--model.

코파일럿 CLI를 프로그래밍 방식으로 실행할 때 유용하게 활용할 수 있는 옵션인 GitHub Copilot CLI 프로그래밍 방식 참조 을 확인해 보시기 바랍니다.

CI/CD 통합

CI/CD 워크플로 단계에 CLI 명령을 추가하는 것은 코파일럿 CLI를 프로그래밍 방식으로 실행하는 대표적인 활용 사례입니다.

GitHub Actions 워크플로에서 가져온 다음 예제를 통해 코파일럿 CLI 명령을 실행하는 기본적인 방법을 확인할 수 있습니다.

# Workflow step using Copilot CLI
- name: Generate test coverage report
  env:
    COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
  run: |
    copilot -p "Run the test suite and produce a coverage summary" \
      -s --allow-tool='shell(npm:*), write' --no-ask-user

자세한 내용은 Copilot CLI 및 GitHub Actions를 사용하여 작업 자동화을(를) 참조하세요.

프로그래밍 방식 사용의 예

커밋 메시지 생성

Bash
copilot -p 'Write a commit message in plain text for the staged changes' -s \
  --allow-tool='shell(git:*)'

파일 요약

Bash
copilot -p 'Summarize what src/auth/login.ts does in no more than 100 words' -s

모듈에 대한 테스트 작성

Bash
copilot -p 'Write unit tests for src/utils/validators.ts' \
  --allow-tool='write, shell(npm:*), shell(npx:*)'

lint 오류 수정

Bash
copilot -p 'Fix all ESLint errors in this project' \
  --allow-tool='write, shell(npm:*), shell(npx:*), shell(git:*)'

차이 설명

Bash
copilot -p 'Explain the changes in the latest commit on this branch and flag any potential issues' -s

분기를 검토하는 코드

슬래시 명령을 사용하여 /review 기본 제공 code-review 에이전트가 현재 분기의 코드 변경 내용을 검토하도록 합니다.

Bash
copilot -p '/review the changes on this branch compared to main. Focus on bugs and security issues.' \
  -s --allow-tool='shell(git:*)'

설명서 생성

Bash
copilot -p 'Generate JSDoc comments for all exported functions in src/api/' \
  --allow-tool=write

세션 내보내기

전체 세션 기록을 로컬 파일 시스템의 Markdown 파일에 저장합니다.

Bash
copilot -p "Audit this project's dependencies for vulnerabilities" \
  --allow-tool='shell(npm:*), shell(npx:*)' \
  --share='./audit-report.md'

쉽게 공유할 수 있도록 세션 내용을 GitHub.com의 Gist에 저장합니다.

Bash
copilot -p 'Summarize the architecture of this project' --share-gist

참고

Enterprise Managed Users에서는 사용할 수 없으며, data 상주 기능이 있는 GitHub Enterprise Cloud(*.ghe.com)를 사용하는 경우에도 지원되지 않습니다.

셸 스크립팅 패턴

Copilot의 출력을 변수에 캡처하기

Bash
result=$(copilot -p 'What version of Node.js does this project require? \
  Give the number only. No other text.' -s)
echo "Required Node version: $result"

조건부에서 사용

Bash
if copilot -p 'Does this project have any TypeScript errors? Reply only YES or NO.' -s \
  | grep -qi "no"; then
  echo "No type errors found."
else
  echo "Type errors detected."
fi

여러 파일 처리

Bash
for file in src/api/*.ts; do
  echo "--- Reviewing $file ---" | tee -a review-results.md
  copilot -p "Review $file for error handling issues" -s --allow-all-tools | tee -a review-results.md
done

추가 읽기

  •         [AUTOTITLE](/copilot/how-tos/copilot-cli)
    
  •         [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-programmatic-reference)
    
  •         [AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference#command-line-options)