How do secrets work in `if` conditions?
IntermediateConceptSecrets, Variables, Environments & Permissions
Answer
They cannot be referenced directly in if. Map to an env var and test the env var at step level:
jobs:
publish:
runs-on: ubuntu-24.04
env:
HAS_NPM_TOKEN: ${{ secrets.NPM_TOKEN != '' }}
steps:
- if: env.HAS_NPM_TOKEN == 'true'
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
This is the standard way to skip publish steps on forks where secrets are empty.
More on Secrets, Variables, Environments & Permissions
Where can secrets be defined, and what is the precedence?BeginnerHow are secrets protected in transit and in logs, and what are the limits?IntermediateAre secrets available to every step automatically?BeginnerWhy is it a bad idea to store structured data like JSON or a whole `.env` file as one secret?IntermediateWhat is an environment, and which protection rules can it have?BeginnerWhen does a job wait for environment approval, and what does the reviewer see?Intermediate