Building a Java CI/CD Pipeline with GitHub Actions
Fast recall
Quick revision
Core answer
Whether you can configure automated pipelines and understand CI/CD principles in practice.
Hardcoding credentials in workflow files instead of using GitHub Secrets, or not caching dependencies between runs.
Mention separating CI and CD workflows, using environments with approval gates, and artifact promotion between stages.
Interview-ready guide
Interview answer
A GitHub Actions CI pipeline for Java is a YAML file in .github/workflows/ that defines jobs triggered by push or pull request events. Jobs run on GitHub-managed runners and execute steps sequentially within each job.
Workflow trigger and checkout
on: [push, pull_request]triggers the workflow on every push and PR; useon: push: branches: [main]pluson: pull_requestto trigger differently for mainline pushes versus PR validation — PRs check but do not deploy, pushes to main deployactions/checkout@v4is always the first step — it clones the repository into the runner's workspace; without it, subsequent steps have no source code- Set
fetch-depth: 0in the checkout action if SonarQube analysis is needed — SonarQube requires the full git history to compute new vs changed code; shallow clones (the default) report everything as new
Java setup and caching
actions/setup-java@v4withdistribution: 'temurin'andjava-version: '21'installs the JDK and automatically configures theJAVA_HOMEandPATH— no manual installation steps- Enable Maven dependency caching in the
setup-javaaction withcache: 'maven'; GitHub Actions caches~/.m2/repositorykeyed on thepom.xmlhash — a cache hit skips the 2–3 minute dependency download on every build - Gradle projects use
gradle/actions/setup-gradle@v3which caches the Gradle home and the build cache simultaneously — the combination reduces a 6-minute Gradle build to under 2 minutes on cache hits
Build, test, and artifact publishing
run: mvn -B verifyruns the full lifecycle including integration tests and coverage;-B(batch mode) suppresses ANSI color codes and progress indicators that create noise in CI logs- Use
actions/upload-artifact@v4to save test reports and JARs: subsequent jobs can download them withactions/download-artifact, enabling a build-once-deploy-many pattern where the image build job and the Kubernetes deploy job share the same compiled artifact
Building a Java CI/CD Pipeline with GitHub Actions
Direct Answer
A Java CI/CD pipeline with GitHub Actions uses workflow YAML files to automate building, testing, and deploying your application on every push or pull request using ubuntu runners and the actions/setup-java action.
Common Mistakes & Pitfalls
Hardcoding credentials in workflow files instead of using GitHub Secrets, or not caching dependencies between runs.
Check your understanding
Answer these without looking back at the article.
- How do you roll back a failed deployment in GitHub Actions?
- How do you share workflow steps across multiple repositories?
- What is the difference between a job and a step in GitHub Actions?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.