mcpField
G
github-actions

github-actions

github.com/dalestudy云与 DevOps4717 次安装5 天前更新
代码仓库

关于 github-actions

github-actions 是编写和维护 GitHub Actions 工作流的技能,提供 Action 版本、密钥、注入风险、权限、Runner 工具及常见工作流模式的指导。

主要功能

  1. 01提供 Action 版本与工作流结构指南
  2. 02说明密钥处理和注入风险防范
  3. 03指导遵循最小权限原则
  4. 04列出 Runner 预装工具参考
  5. 05提供常见事件与 Action 示例

适用场景

  1. 01检查工作流中的安全隐患
  2. 02为 CI 作业选择权限
  3. 03避免重复设置 Runner 工具

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dalestudy/skills --skill github-actions

安装后在 Claude Code 中输入/github-actions确认已加载。

详细介绍

GitHub Actions

参考: 有关运行 GitHub Actions 工作流、查看结果、管理 issue/PR 等 gh CLI 相关操作,请同时加载并参考 github 技能。

注意事项(Anti-patterns)

1. 使用过时版本
# ❌ 오래된 버전 - 가장 흔한 실수
uses: actions/checkout@v4 # v6가 최신인 경우

# ✅ 최신 메이저 버전 (gh release view로 확인 후 사용)
uses: actions/checkout@v6

请使用最新版本,以免错过性能改进和安全补丁。

检查版本的命令:

gh release view --repo {owner}/{repo} --json tagName --jq '.tagName'

# 예시
gh release view --repo actions/checkout --json tagName --jq '.tagName'
gh release view --repo oven-sh/setup-bun --json tagName --jq '.tagName'

参考:在安全敏感环境中,或使用可信度较低的第三方 action 时,可考虑固定 SHA(@a1b2c3...)。

2. 硬编码敏感信息
# ❌ 하드코딩 - 보안 위험
env:
  API_KEY: "sk-1234567890"
  DATABASE_PASSWORD: "mypassword123"
# ✅ secrets 사용
env:
  API_KEY: ${{ secrets.API_KEY }}
  DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}

密码或 API Key 等敏感信息可能会直接暴露,从而导致安全事故。 安全相关的重要信息必须保存为存储库或组织的密钥,并从中读取。

参考:Using secrets

3. 输入值注入漏洞
# ❌ 인젝션 취약 - github.event 직접 사용
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"

恶意用户可能会在 issue 标题或评论中注入 shell 命令。

参考:Script injections

4. 滥用 pull_request_target 事件
# ⚠️ 위험 - 포크의 코드를 신뢰된 컨텍스트에서 실행
on: pull_request_target
steps:
  - uses: actions/checkout@v{N}
    with:
      ref: ${{ github.event.pull_request.head.sha }} # 위험!

pull_request_target 事件可以访问来自 fork 的 PR 中的密钥。如果检出 fork 中的代码,可能会执行恶意代码。

参考:pull_request_target

5. 重复配置预装工具
# ❌ 불필요한 setup - node, npm, npx는 이미 설치됨
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

查看预装工具:

最佳实践(Best Practices)

最小权限原则

尽可能在较低层级声明权限。将范围控制在最小限度:

# ✅ 권한 범위: workflow > job > step (좁을수록 좋음)
jobs:
  build:
    permissions:
      contents: read # job 레벨에서 필요한 권한만

参考:Modifying the permissions for the GITHUB_TOKEN

推荐的工作流结构

name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      # 버전은 gh release view로 확인 후 사용
      - uses: actions/checkout@v{N}

      - name: Setup Bun
        uses: oven-sh/setup-bun@v{N}

常用事件

on:
  push: # 푸시 시
    branches: [main]
  pull_request: # PR 생성/업데이트 시
    branches: [main]
  workflow_dispatch: # 수동 실행
  schedule: # 스케줄 실행
    - cron: "0 0 * * 1" # 매주 월요일 00:00 UTC
  release: # 릴리스 생성 시
    types: [published]
  workflow_call: # 다른 워크플로우에서 호출

常用权限

permissions:
  contents: read        # CI (빌드/테스트), 코드 체크아웃
  contents: write       # 커밋/푸시
  pull-requests: write  # PR 코멘트 봇
  issues: write         # 이슈 코멘트
  packages: write       # 패키지 배포 (contents: write와 함께)
  id-token: write       # OIDC 클라우드 인증 (contents: read와 함께)

常用 action

# 버전은 gh release view --repo {owner}/{repo} --json tagName --jq '.tagName'으로 확인
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 설정

评论

支持 Markdown