Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 80

    GitHub Actions

    GitHub Actions is the default CI for many Spring Boot teams: workflows as YAML next to the TechLearningPRO Orders repo, marketplace actions for Java setup, caching, and registry…

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    GitHub Actions is the default CI for many Spring Boot teams: workflows as YAML next to the TechLearningPRO Orders repo, marketplace actions for Java setup, caching, and registry login.

    A solid Boot 3 workflow checks out code, sets up Temurin 21 with Maven cache, runs ./mvnw verify, then on main builds and publishes an image. Prefer OIDC for cloud auth over long-lived keys stored as repository secrets.

    Keep workflows small, pin action versions, and separate PR verification from release deploy jobs.

    This lesson implements Orders CI on GitHub Actions.

    Purpose of this lesson

    Author a GitHub Actions workflow that tests and publishes Boot 3 Orders images on Java 21.

    Understanding the topic

    Workflows are event-driven pipelines living in .github/workflows.

    • Triggers: push, pull_request, workflow_dispatch.
    • Jobs/steps: runners execute sequential steps.
    • Cache: Maven/Gradle caches save minutes per build.
    • Secrets: repository/environment secrets — never hard-code.
    • OIDC: federate to cloud IAM without static keys when possible.

    Step-by-step explanation

    1. Add .github/workflows/ci.yml with Java 21 Temurin setup.
    2. Run ./mvnw -B verify on every PR.
    3. On main, build-image and push to GHCR using the job token.
    4. Add an environment protection rule for production deploy jobs.

    Informative example

    Orders CI workflow (test + publish).

    yaml
    # .github/workflows/ci.yml
    name: CI
    on:
    push:
    branches: [main]
    pull_request:
    jobs:
    build:
    runs-on: ubuntu-latest
    permissions:
    contents: read
    packages: write
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-java@v4
    with:
    distribution: temurin
    java-version: "21"
    cache: maven
    - name: Test
    run: ./mvnw -B verify
    - name: Login GHCR
    if: github.ref == 'refs/heads/main'
    uses: docker/login-action@v3
    with:
    registry: ghcr.io
    username: ${{ github.actor }}
    password: ${{ secrets.GITHUB_TOKEN }}
    - name: Publish image
    if: github.ref == 'refs/heads/main'
    run: >
    ./mvnw -B spring-boot:build-image
    -Dspring-boot.build-image.imageName=ghcr.io/${{ github.repository }}/orders:${{ github.sha }}
    -Dspring-boot.build-image.publish=true

    Real-world use

    TechLearningPRO's first Actions workflow downloaded the internet on every run. Enabling cache: maven and pinning action SHAs cut PR feedback from 12 minutes to under 5 — reviewers actually waited for green checks instead of merging blind.

    Security implications

    Workflow permissions are a blast radius control.

    • Set least permissions: at job level.
    • Prefer secrets.GITHUB_TOKEN over personal PATs for GHCR.
    • Never commit cloud access keys — use OIDC or encrypted secrets stores.

    Best practices

    • Pin actions to full commit SHAs for high-security repos.
    • Use environment approval gates for prod deploy workflows.
    • Cache dependencies; fail fast on verify before image build.

    Common mistakes

    • Unpinned actions/checkout@main floating tags.
    • Storing long-lived cloud access keys when OIDC is available.
    • Printing env dumps that include secret values.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhere do GitHub Actions workflows live?+

    Answer

    In .github/workflows/*.yml inside the repository.
    2BeginnerQuestionWhy cache Maven in setup-java?+

    Answer

    It reuses downloaded dependencies across runs and significantly shortens build time.
    3IntermediateQuestionWhy prefer OIDC over static cloud keys?+

    Answer

    Short-lived federated credentials reduce leak impact and remove key rotation toil.

    Hands-on exercise

    Lab. Add the workflow above to Orders. Open a PR that breaks a unit test and confirm CI fails. Merge a fix to main and confirm an image tag matching the commit SHA appears in GHCR.

    Summary

    GitHub Actions automates TechLearningPRO Orders CI. Next: see the same ideas expressed in Jenkins for enterprise shops.

    Ready to mark this lesson complete?Track your journey across the entire course.