github-actions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitHub Actions
GitHub Actions
주의 사항 (Anti-patterns)
注意事项(反模式)
1. 오래된 버전 사용
1. 使用旧版本
yaml
undefinedyaml
undefined❌ 오래된 버전 - 가장 흔한 실수
❌ 旧版本 - 最常见的错误
uses: actions/checkout@v4 # v6가 최신인 경우
uses: actions/checkout@v4 # 当v6为最新版本时
✅ 최신 메이저 버전 (gh api로 확인 후 사용)
✅ 最新主版本(使用gh api确认后使用)
uses: actions/checkout@v6
최신 버전에서 제공하는 성능 개선과 보안 패치를 놓치지 않도록 합니다.
**버전 확인 명령어:**
```bash
gh api repos/{owner}/{repo}/releases/latest --jq '.tag_name'uses: actions/checkout@v6
不要错过最新版本提供的性能改进和安全补丁。
**版本确认命令:**
```bash
gh api repos/{owner}/{repo}/releases/latest --jq '.tag_name'예시
示例
gh api repos/actions/checkout/releases/latest --jq '.tag_name'
gh api repos/oven-sh/setup-bun/releases/latest --jq '.tag_name'
> 참고: 보안 민감 환경이나 신뢰도 낮은 서드파티 액션은 [SHA 피닝](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-third-party-actions)(`@a1b2c3...`)을 고려.gh api repos/actions/checkout/releases/latest --jq '.tag_name'
gh api repos/oven-sh/setup-bun/releases/latest --jq '.tag_name'
> 参考:在安全敏感环境或使用可信度较低的第三方Action时,考虑[SHA固定](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-third-party-actions)(`@a1b2c3...`)。2. 민감정보 하드코딩
2. 硬编码敏感信息
yaml
undefinedyaml
undefined❌ 하드코딩 - 보안 위험
❌ 硬编码 - 安全风险
env:
API_KEY: "sk-1234567890"
DATABASE_PASSWORD: "mypassword123"
env:
API_KEY: "sk-1234567890"
DATABASE_PASSWORD: "mypassword123"
✅ secrets 사용
✅ 使用secrets
env:
API_KEY: ${{ secrets.API_KEY }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
비밀번호나 API Key와 같은 민감 정보가 그대로 노출되어 보안 사고로 이어질 수 있습니다.
보안 상 중요한 정보는 반드시 저장소나 조직의 시크릿으로 저장해놓고 읽어 와야합니다.
> 참고: [Using secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions)env:
API_KEY: ${{ secrets.API_KEY }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
密码或API Key等敏感信息直接暴露可能导致安全事故。安全相关的重要信息必须存储在仓库或组织的secrets中,再进行读取使用。
> 参考:[Using secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions)3. 입력값 인젝션 취약점
3. 输入值注入漏洞
yaml
undefinedyaml
undefined❌ 인젝션 취약 - github.event 직접 사용
❌ 注入漏洞 - 直接使用github.event
run: echo "${{ github.event.issue.title }}"
run: gh issue comment ${{ github.event.issue.number }} --body "${{ github.event.comment.body }}"
run: echo "${{ github.event.issue.title }}"
run: gh issue comment ${{ github.event.issue.number }} --body "${{ github.event.comment.body }}"
✅ 환경변수로 전달하여 인젝션 방지
✅ 通过环境变量传递以防止注入
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
echo "$ISSUE_TITLE"
gh issue comment ${{ github.event.issue.number }} --body "$COMMENT_BODY"
악의적인 사용자가 이슈 제목이나 코멘트에 셸 명령어를 주입할 수 있습니다.
> 참고: [Script injections](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)env:
ISSUE_TITLE: ${{ github.event.issue.title }}
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
echo "$ISSUE_TITLE"
gh issue comment ${{ github.event.issue.number }} --body "$COMMENT_BODY"
恶意用户可能在议题标题或评论中注入shell命令。
> 参考:[Script injections](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)4. pull_request_target 이벤트 오용
4. 滥用pull_request_target事件
yaml
undefinedyaml
undefined⚠️ 위험 - 포크의 코드를 신뢰된 컨텍스트에서 실행
⚠️ 危险 - 在可信上下文中运行分支代码
on: pull_request_target
steps:
- uses: actions/checkout@v{N} with: ref: ${{ github.event.pull_request.head.sha }} # 위험!
`pull_request_target` 이벤트는 포크의 PR에서도 시크릿에 접근 가능합니다. 포크 코드를 체크아웃하면 악성 코드가 실행될 수 있습니다.
> 참고: [pull_request_target](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request_target)on: pull_request_target
steps:
- uses: actions/checkout@v{N} with: ref: ${{ github.event.pull_request.head.sha }} # 危险!
`pull_request_target`事件即使在分支的PR中也能访问secrets。如果检出分支代码,可能会执行恶意代码。
> 参考:[pull_request_target](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request_target)5. 사전 설치된 도구에 중복 설정
5. 重复配置已预安装的工具
yaml
undefinedyaml
undefined❌ 불필요한 setup - node, npm, npx는 이미 설치됨
❌ 不必要的设置 - node、npm、npx已预装
steps:
- uses: actions/setup-node@v{N}
- run: npx some-command
steps:
- uses: actions/setup-node@v{N}
- run: npx some-command
✅ 바로 사용
✅ 直接使用
steps:
- run: npx some-command
- run: python script.py
- run: docker build .
중복 설치는 워크플로우 실행 시간을 늘리고 불필요한 네트워크 요청을 발생시킵니다.
**주요 사전 설치 도구:** Node.js, npm, npx, Python, pip, Ruby, gem, Go, Docker, git, gh, curl, wget, jq, yq
**주요 미설치 도구:** Bun, Deno, Rust, Zig, pnpm, Poetry, Ruff
**사전 설치된 도구 확인:**
- Ubuntu: https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
- macOS: https://github.com/actions/runner-images/blob/main/images/macos/macos-15-Readme.md
- Windows: https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.mdsteps:
- run: npx some-command
- run: python script.py
- run: docker build .
重复安装会增加工作流执行时间并产生不必要的网络请求。
**主要预安装工具:** Node.js, npm, npx, Python, pip, Ruby, gem, Go, Docker, git, gh, curl, wget, jq, yq
**主要未预安装工具:** Bun, Deno, Rust, Zig, pnpm, Poetry, Ruff
**查看预安装工具:**
- Ubuntu: https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
- macOS: https://github.com/actions/runner-images/blob/main/images/macos/macos-15-Readme.md
- Windows: https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.md모범 사례 (Best Practices)
最佳实践
최소 권한 원칙
最小权限原则
권한은 가능한 하위 레벨에 선언. 범위를 좁게 유지:
yaml
undefined权限应尽可能在最细粒度级别声明,保持范围狭窄:
yaml
undefined✅ 권한 범위: workflow > job > step (좁을수록 좋음)
✅ 权限范围:workflow > job > step(越细越好)
jobs:
build:
permissions:
contents: read # job 레벨에서 필요한 권한만
> 참고: [Modifying the permissions for the GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token)jobs:
build:
permissions:
contents: read # job级别仅声明所需权限
> 参考:[Modifying the permissions for the GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token)권장 워크플로우 구조
推荐的工作流结构
yaml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 버전은 gh api로 확인 후 사용
- uses: actions/checkout@v{N}
- name: Setup Bun
uses: oven-sh/setup-bun@v{N}yaml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 版本使用gh api确认后填写
- uses: actions/checkout@v{N}
- name: Setup Bun
uses: oven-sh/setup-bun@v{N}자주 사용되는 이벤트
常用事件
yaml
on:
push: # 푸시 시
branches: [main]
pull_request: # PR 생성/업데이트 시
branches: [main]
workflow_dispatch: # 수동 실행
schedule: # 스케줄 실행
- cron: "0 0 * * 1" # 매주 월요일 00:00 UTC
release: # 릴리스 생성 시
types: [published]
workflow_call: # 다른 워크플로우에서 호출yaml
on:
push: # 推送时
branches: [main]
pull_request: # PR创建/更新时
branches: [main]
workflow_dispatch: # 手动执行
schedule: # 定时执行
- cron: "0 0 * * 1" # 每周一00:00 UTC
release: # 创建发布时
types: [published]
workflow_call: # 被其他工作流调用자주 사용되는 권한
常用权限
yaml
permissions:
contents: read # CI (빌드/테스트), 코드 체크아웃
contents: write # 커밋/푸시
pull-requests: write # PR 코멘트 봇
issues: write # 이슈 코멘트
packages: write # 패키지 배포 (contents: write와 함께)
id-token: write # OIDC 클라우드 인증 (contents: read와 함께)yaml
permissions:
contents: read # CI(构建/测试)、代码检出
contents: write # 提交/推送
pull-requests: write # PR评论机器人
issues: write # 议题评论
packages: write # 包发布(需配合contents: write)
id-token: write # OIDC云认证(需配合contents: read)자주 사용되는 액션
常用Action
yaml
undefinedyaml
undefined버전은 gh api repos/{owner}/{repo}/releases/latest --jq '.tag_name'으로 확인
版本通过gh api repos/{owner}/{repo}/releases/latest --jq '.tag_name'确认
steps:
- uses: actions/cache@v{N} # 의존성 캐싱
- uses: actions/checkout@v{N} # 코드 체크아웃
- uses: actions/download-artifact@v{N} # 아티팩트 다운로드
- uses: actions/upload-artifact@v{N} # 아티팩트 업로드
- uses: oven-sh/setup-bun@v{N} # Bun 설정
undefinedsteps:
- uses: actions/cache@v{N} # 依赖缓存
- uses: actions/checkout@v{N} # 代码检出
- uses: actions/download-artifact@v{N} # 下载制品
- uses: actions/upload-artifact@v{N} # 上传制品
- uses: oven-sh/setup-bun@v{N} # Bun环境配置
undefined