متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 149CI/CD · GitHub

How to use secrets safely in GitHub Actions

Pipelines need credentials — a registry token, an SSH deploy key, a cloud login. In GitHub Actions they are secrets: encrypted, masked in logs, given only to the jobs that need them. But secrets still leak through echo, debug output, pull requests from forks, or a compromised third-party action. This how-to stores secrets at the right level, uses them safely, protects production with environments, and replaces long-lived cloud keys with OIDC.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

149.1

The quick answer

gh secret set REGISTRY_TOKEN --repo ORG/shop < registry-token.txt
gh secret set DEPLOY_KEY --repo ORG/shop --env production < deploy_key
gh secret list --repo ORG/shop
.github/workflows/release.yml (part)
jobs:
  deploy:
    environment: production
    runs-on: ubuntu-24.04
    steps:
      - run: echo "$TOKEN" | docker login registry.example.com -u ci --password-stdin
        env:
          TOKEN: ${{ secrets.REGISTRY_TOKEN }}

الأوامر في هذا الدرس

149.2

Step by step

  1. Choose the level:
LevelUse
Repository secretUsed by this repo's workflows.
Environment secretOnly for jobs with environment: production — with required reviewers and branch rules.
Organization secretShared by chosen repos (one place to rotate).
  1. **Pass secrets through env:**, never paste them into the run: script text — and never print them. Masking hides the exact value, but not a base64 or split version of it.
  2. Protect production with an environment: Settings → Environments → production → required reviewers and deployment branches = main only. A job with environment: production waits for approval and gets the environment's secrets only then.
  3. Pull requests from forks get no secrets (on purpose). Never use pull_request_target together with checking out and running the PR's code — that gives untrusted code your secrets.
  4. Least privilege for the automatic token:
.github/workflows/ci.yml (top)
permissions:
  contents: read

Add only what a job needs (packages: write, id-token: write).

  1. Better than stored cloud keys: OIDC — the job gets a short-lived token from the cloud (AWS, Azure, GCP) for this repo/branch/environment only. No secret to rotate or leak (see the Azure OIDC how-to).
  2. Third-party actions can read your secrets in the job — pin them by commit SHA, use few of them, and keep secret-using steps in separate jobs.

149.3

Check that it worked

gh secret list shows the names (never values), the logs show *** where a secret is used, a production job waits for approval, and a test PR from a fork runs without secrets.

الأوامر في هذا الدرس

149.4

Common problems

ProblemCauseFix
Secret is empty in the jobWrong name, environment secret without environment:, or a fork PR.Check name/level; add environment:; forks never get secrets.
Secret visible in logsPrinted in another form (base64, JSON, split).Never print secrets; rotate it now.
Resource not accessible by integrationToken permissions too low.Add the needed permission to that job only.
Leak via pull_request_targetRunning fork code with secrets.Use pull_request; never check out PR code in _target.
A secret was committed or loggedIt is compromised.Rotate it, delete the logs/runs, then fix the workflow.
Many repos with copies of one secretHard to rotate.Organization secrets or OIDC.

149.5

Go further

1Why use an environment for production secrets?

Only jobs with that environment get them, and only after the required reviewers approve.

2Why do pull requests from forks not get secrets?

Their code is untrusted; giving them secrets would let anyone steal them with a PR.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل