هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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/shopjobs:
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 }}Step by step
- Choose the level:
| Level | Use |
|---|---|
| Repository secret | Used by this repo's workflows. |
| Environment secret | Only for jobs with environment: production — with required reviewers and branch rules. |
| Organization secret | Shared by chosen repos (one place to rotate). |
- **Pass secrets through
env:**, never paste them into therun:script text — and never print them. Masking hides the exact value, but not a base64 or split version of it. - Protect production with an environment: Settings → Environments →
production→ required reviewers and deployment branches =mainonly. A job withenvironment: productionwaits for approval and gets the environment's secrets only then. - Pull requests from forks get no secrets (on purpose). Never use
pull_request_targettogether with checking out and running the PR's code — that gives untrusted code your secrets. - Least privilege for the automatic token:
permissions:
contents: readAdd only what a job needs (packages: write, id-token: write).
- 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).
- 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.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Secret is empty in the job | Wrong name, environment secret without environment:, or a fork PR. | Check name/level; add environment:; forks never get secrets. |
| Secret visible in logs | Printed in another form (base64, JSON, split). | Never print secrets; rotate it now. |
Resource not accessible by integration | Token permissions too low. | Add the needed permission to that job only. |
Leak via pull_request_target | Running fork code with secrets. | Use pull_request; never check out PR code in _target. |
| A secret was committed or logged | It is compromised. | Rotate it, delete the logs/runs, then fix the workflow. |
| Many repos with copies of one secret | Hard to rotate. | Organization secrets or OIDC. |
Go further
- Command pages: gh.
- Next how-tos: Deploy to Azure from GitHub Actions with OIDC, Create a CI pipeline in GitHub Actions, Remove a secret from Git history.
- Learn it: GitHub Actions — Secrets, Environments, The GITHUB_TOKEN, Forks, Script injection.
- Practise: Lab: harden a weak workflow.
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.