It is clear on how to make conditional steps based on the branch of the project. i.e. your workflow article provided. However, is there a way so only certain steps/jobs happen just for pull requests?
For instance, I’d like to do a full build/test for all PRs. Let’s say that passes, I merge the PR to master, then I’d like to automatically do the same exact steps except this time also do a deploy which requires my secrets/credentials. It would be ideal if I could conditionally set steps within a job to know to execute if it is a PR versus a branch.
Got a syntax error with that, but tweaked it with help from ShellCheck:
command: |
if [[ $(echo "$CIRCLE_BRANCH" | grep -c "pull") -gt 0 ]]; then
echo "Skip doing stuff since it is a PR."
else
echo "Not a PR, so now do what I want."
fi
Regular expression lookarounds aren’t supported by all regex engines, so while it works on CircleCI now, it may not in the future if they switch to a different language/engine for parsing the config
I’m not sure how only: /^(?!pull\/).*$/ would work, if I make a PR from a master branch in a fork, it shows up as CIRCLECI_BRANCH=master, does filter work differently?
@afeld I was hoping you could help me understand your YAML a bit better.
If I understand canonical repository correctly, this should match against any pushes to my repository that are not coming from forks - so, if I have a branch such as feature/some-new-feature, and push to it, would it trigger the jobs specified in this workflow?
I also presume you’d need to have CircleCI configured to not run only on pull requests?
this should match against any pushes to my repository that are not coming from forks
Correct. The ?! in the regular expression is called a negative lookahead, and is basically saying “match branches where CircleCI doesn’t include pull/ at the beginning,” which are pull requests from forks.
if I have a branch such as feature/some-new-feature, and push to it, would it trigger the jobs specified in this workflow?
Yep!
you’d need to have CircleCI configured to not run only on pull requests?
I think it would work work either way. If set to only build on pull requests, it would only build pull requests from branches in the canonical repository.