GitHub Actions Secrets: 8 Common Mistakes and Fixes
You build a small automation that posts to Discord when your website goes down. To make it work, you paste the webhook URL into your script. It works on the first try, so you commit it and move on. Months later, you make the repo public to share it. Now anyone can read that URL, and anyone can post to your Discord channel. Secrets are one of those things that feel like a small detail until they…
GitHub Actions provides a convenient way to automate tasks in your repository, but secrets such as API keys, passwords, tokens and webhook URLs can easily leak if not handled properly. Here are eight common mistakes and how to fix them:
1. Hardcoding secrets in files: Instead of placing the secret directly in your script or workflow file, store it as a repository secret in the Settings > Secrets and variables > Actions section. Reference it in your workflow using the env keyword: - name: Send alert env: WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK }} run: python alert.py
2. Printing secrets in logs: While GitHub attempts to hide secret values in logs, it's not guaranteed. Avoid printing sensitive data in your debug commands. If you need to verify a secret is set, check if it's empty instead of displaying its contents.
3. Trusting GitHub's log masking: Automatic redaction of recognized secrets in logs is not foolproof. If a secret is encoded or split into multiple parts, it may still appear in plain text. Store each secret as a single value and explicitly hide it using the add-mask command: - run: echo ::add-mask::$CUSTOMER_ID
4. Using overly powerful keys: Grant the minimum required permissions to a key. Use separate keys for each project and consider using restricted keys instead of your primary login credentials.
5. Exposing GITHUB_TOKEN: Every workflow run has a built-in token called GITHUB_TOKEN with potentially excessive permissions. Set read-only access as the default and grant additional permissions only when necessary. Specify permissions at the top of your workflow file: permissions: contents: read
6. Running workflows from pull requests: Public or forkable repositories should avoid running workflows from pull requests, especially when triggered by pull_request_target. Limit workflow execution to direct pushes or specific branches to avoid exposing secrets.
7. Running untrusted text in commands: User-provided input, such as issue or pull request titles, can contain malicious commands that may execute with your secrets. Treat such values as plain text by storing them in environment variables first: - env: TITLE: ${{ github.event.issue.title }} run: echo "New issue: $TITLE"
8. Using unverified third-party actions: Be cautious when using actions created by others. Prefer actions from GitHub itself or well-established, maintained projects. Review the action's functionality before granting it access to secrets. Pin the action to a specific commit SHA instead of a version tag to avoid potential code changes.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.