How would you implement conditional logic in a pipeline?

Declarative stage gating without shell hacks.
Use if conditions with contexts like github.ref, separate trigger filters from runtime conditions, and add env rules.
Shell if statements in a job, not native conditionals.
What's really being asked
This question tests whether you understand the difference between trigger-time filtering and runtime conditional execution in pipeline engines. A senior candidate should demonstrate that they can use the platform's native expression language to skip or include jobs based on branch, event type, or custom variables, rather than falling back to imperative shell scripting which obscures the DAG and breaks the visual UI.
The full answer
First, job-level conditionals using the if key with built-in contexts and expressions, for example checking github.ref equals refs/heads/main to decide whether a deploy job runs. Second, the distinction between on.push.branches filters at the workflow trigger level versus runtime if conditions inside the job; triggers prevent the whole workflow from starting while runtime conditionals let earlier stages like test run on every branch and only gate the deploy stage. Third, using environment protection rules such as required reviewers or deployment branches as an additional safety layer beyond YAML conditionals. Fourth, handling pull request versus push events differently by inspecting github.event_name so that deploys do not fire on PR builds. Fifth, composing reusable workflows with inputs and conditionals passed from the caller so logic stays DRY across repositories.
The mistakes people make
A red flag is suggesting a single monolithic job where a bash script checks the branch name and exits zero or non-zero; this hides job state from the platform dashboard and prevents granular retry. Another mistake is conflating workflow triggers with job conditions and claiming that on.push.branches main will selectively run only the deploy job when in fact it suppresses the entire workflow for other branches. Candidates also err by hardcoding secrets or environment names inside conditional expressions rather than using environment protection rules.
What usually comes next
The interviewer may ask how you would handle a hotfix branch pattern where release branches should also trigger deploys, or how you would implement a manual approval gate before production. They might also probe failure modes, such as what happens when an if expression evaluates incorrectly due to a null context field, or how you test conditional logic without merging to main.
A concrete example
In GitHub Actions, you define a build job that runs on every push and pull request. Then you define a deploy job that needs build and includes if github.ref equals refs/heads/main and github.event_name equals push. The deploy job targets an environment named production which has a protection rule restricting deployment branches to main and requiring one reviewer. This means even if a malicious actor bypasses the YAML conditional, the environment gate blocks the deployment.
Interview question
Which approach lets tests run on every branch while gating deploy to main only?
- a.Configure an environment protection rule limited to main on the deploy job
- b.Add if: github.ref == 'refs/heads/main' to the deploy jobCorrect
- c.Set the workflow trigger to on.push.branches: main
- d.Use a shell if statement inside the deploy job to check the branch
Why? this is the answer
A job-level if condition is a native runtime conditional that keeps the test job in the DAG for all branches while skipping deploy when not on main. Using on.push.branches: main is a trigger filter that suppresses the entire workflow for other branches, so tests would never run there.
Just read this? Test yourself on what you have been reading.
Read the original → docs.github.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ci/cd — each one lists the topics its interview covers.
See open roles