CompTIA Linux+ (XK0-006)Automation, Orchestration and ScriptingMedium
A DevOps engineer is configuring a Continuous Integration/Continuous Delivery (CI/CD) pipeline for a new microservice. They need to ensure that after a successful build and passing unit tests, the Docker image is tagged and pushed to a container registry, but only if the build originates from the 'main' branch. Which CI/CD pipeline stage and conditional logic would best achieve this requirement?
- ARelease Stage: `unless branch != 'main' then docker push`
- BBuild Stage: `if commit_status == 'success' then docker tag`
- CDeploy Stage: `when: branch = 'main'`
- DTest Stage: `if branch == 'main' then docker push`
Show answer & explanationAnswer & explanation
Correct answer: C. Deploy Stage: `when: branch = 'main'`
Pushing an image to a container registry is typically part of a deployment or release process. CI/CD systems often provide 'when' conditions to execute steps or stages only if certain criteria, like the branch name, are met.
Why the other options are wrong
- A. While 'Release Stage' could be appropriate, the `unless branch != 'main'` is a double negative and less common than a direct positive condition like `when: branch = 'main'`.
- B. Tagging can happen earlier, but pushing to a registry is a deployment action. The condition `commit_status == 'success'` is implicit for any subsequent stage and doesn't filter by branch.
- D. Pushing to a registry is a deployment action, not typically part of the 'Test' stage. The syntax is also generic and not specific to CI/CD tools.
CI/CD Conditional Deployment
The practice of executing specific deployment steps or stages in a CI/CD pipeline only when certain conditions are met, such as the source branch or build status.
- Ensures deployments only occur from designated branches (e.g., 'main', 'release').
- Prevents accidental deployments from feature branches.
- Implemented using conditional logic (`if`, `when`) within pipeline configuration files.
Memory trick: Deploy only when conditions are right, like a rocket launching from the correct pad.