How can I test if secrets are available in an action? #26726
-
I’d like an action to run on PRs from the main repo, but not from PRs from forked repos. |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 4 replies
-
Secrets are not passed to workflows that are triggered by a pull request from a fork. As you said , your PRs are within the main repo. Then the secrets could be passed in workflow. How do you use secrets in your action? Use secret variables as action input variable value? If so, you could check your logs , the secrets variable value will be masked with *** . If I misunderstanding your scenario, please share your workflow yml content here. |
Beta Was this translation helpful? Give feedback.
-
To clarify, I’d like my CI to run all the jobs and pass when run from the main repo. When someone sends a PR from a forked repo, I’d still like the subset of jobs that don’t require secrets to run and CI still to pass. I’ll follow up with a detailed example in the next day or two… |
Beta Was this translation helpful? Give feedback.
-
I was able to get it working by testing every step of the job for the existence of the environment variable associated with the secret. See firebase/firebase-ios-sdk#5180. It would be nicer if there were away to check for secrets availability at the job level. |
Beta Was this translation helpful? Give feedback.
-
@paulb777 I checked your PR, you use secrets as the value of environment variables. You could set the env in job level. Then the env could be used in your scripts directly. In bash, use it in syntax $var_name
You could enable step debug logging, in set up job step, the secrets will be evaluated. |
Beta Was this translation helpful? Give feedback.
-
Thanks @yanjingzhu. Setting the secret environment variables at the job level is much cleaner. I tried several different locations and variations for the |
Beta Was this translation helpful? Give feedback.
-
You can only use the env context in the value of the with and name keys, or in a step’s if conditional. It is not supported to use env in job’s if conditional. And screrts context could not be used in if conditional, neither job’s if nor step’s if . So, it is not possible to disable a job by identifying secrets . I am afraid that you need to add if contional to each steps. Sorry for any inconvenience. |
Beta Was this translation helpful? Give feedback.
-
This might not be what you’re after, but you can disable individual steps by putting the secret into a job’s env and then using if: ${{ env.SECRET_KEY != 0 }} Setting env at the workflow level does not appear to work, so ‘if’ for entire jobs won’t work. |
Beta Was this translation helpful? Give feedback.
-
You can make an entire job optional based on the presence of a secret if you had a previous job that set a job output: https://gist.github.com/jonico/24ffebee6d2fa2e679389fac8aef50a3 |
Beta Was this translation helpful? Give feedback.
-
Thanks jonico, I’ve created a more specific example here: |
Beta Was this translation helpful? Give feedback.
-
This did the trick for me:
If the |
Beta Was this translation helpful? Give feedback.
-
It may be a little cheeky but this is what I did at my org. I run a job before my main CI job that checks if all my secrets are in place. It normally does other things too but I took that out for the example below.
That said I also really like the solution by tomerfi. |
Beta Was this translation helpful? Give feedback.
-
Thank you all for the workarounds, but this is ridiculous, there should be a top level github.secrets_present variable or something similar. I don’t understand why these extremely basic issues haven’t been addressed by github. |
Beta Was this translation helpful? Give feedback.
-
I solved it the following way: ...
- name: myjob
env:
MY_SECRET: ${{ secrets.MY_SECRET }}
if: env.MY_SECRET != null
run: echo "running"
... Credits to @TomerFi and @ojeytonwilliams Do you guys have any security concerns? Is it save to expose a secret as a local environment variable during a step? |
Beta Was this translation helpful? Give feedback.
-
https://stackoverflow.com/questions/70249519/how-to-check-if-a-secret-variable-is-empty-in-if-conditional-github-actions also suggests a workaround for this issue. GH Actions: Context backs up what @Yanjingzhu wrote. |
Beta Was this translation helpful? Give feedback.
You can only use the env context in the value of the with and name keys, or in a step’s if conditional.
It is not supported to use env in job’s if conditional.
And screrts context could not be used in if conditional, neither job’s if nor step’s if .
So, it is not possible to disable a job by identifying secrets . I am afraid that you need to add if contional to each steps. Sorry for any inconvenience.