GitHub Actions provide a seamless way to integrate CI/CD into your repository, automating various tasks like running tests, building artifacts, and deploying your project.
Workflow for SNAPSHOT
This workflow is triggered on non-main branches or manually through the workflow dispatch event. It includes the following steps:
- Run tests: Ensures that your code passes all tests.
- Build the JAR file: Compiles your Spring Boot application.
- Build Docker image and push it to GitHub Docker Registry: Creates a Docker image from the generated artifact and pushes it to the GitHub Container Registry.
name: Test and Build Snapshot on: push: branches-ignore: - main workflow_dispatch: permissions: contents: read packages: write jobs: maven-verify: if: "!contains(github.event.head_commit.message, '[skip ci]')" runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' cache: maven cache-dependency-path: '**/pom.xml' - name: Verify run: mvn -B clean verify --file pom.xml maven-build: needs: maven-verify runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' cache: maven cache-dependency-path: '**/pom.xml' - name: Build run: mvn -B package --file pom.xml - name: Upload artifact uses: actions/upload-artifact@v4 with: name: app.jar path: target/*.jar docker-build-push: needs: maven-build runs-on: ubuntu-latest env: CURRENT_VERSION: '' steps: - uses: actions/checkout@v4 - name: Download artifact uses: actions/download-artifact@v4 with: name: app.jar path: target - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GHCR_PAT }} - name: Get Version run: echo "CURRENT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV - name: Build Docker image uses: docker/build-push-action@v6 with: context: . push: true tags: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ env.CURRENT_VERSION }}-${{ github.run_id }}
Workflow for RELEASE
This workflow runs when changes are pushed to the main branch and includes the following steps:
- Build the release version: Compiles the release version of your project.
- Build Docker image and push it to GitHub Docker Registry: Creates and pushes the Docker image to the GitHub Container Registry.
- Increment SNAPSHOT version: Updates the version in pom.xml for future snapshot versions.
name: Build Release on: push: branches: - main workflow_dispatch: permissions: contents: read packages: write jobs: maven-build-release: if: "!contains(github.event.head_commit.message, '[skip ci]')" runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' cache: maven cache-dependency-path: '**/pom.xml' - name: Prepare Release Version run: mvn versions:set -DremoveSnapshot - name: Build run: mvn -B clean package --file pom.xml -DskipTests - name: Upload artifact uses: actions/upload-artifact@v4 with: name: app.jar path: target/*.jar docker-build-push: needs: maven-build-release runs-on: ubuntu-latest env: CURRENT_VERSION: '' steps: - uses: actions/checkout@v4 - name: Download artifact uses: actions/download-artifact@v4 with: name: app.jar path: target - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GHCR_PAT }} - name: Get Version run: | mvn versions:set -DremoveSnapshot echo "CURRENT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV - name: Build Docker image uses: docker/build-push-action@v6 with: context: . push: true tags: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ env.CURRENT_VERSION }}-${{ github.run_id }} increment-version: needs: docker-build-push runs-on: ubuntu-latest permissions: contents: write steps: - uses: actions/checkout@v4 with: token: ${{ secrets.GIT_PAT }} - name: Configure Git run: | git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" - name: Increment Snapshot Version run: | # Extract current version and increment patch version CURRENT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1) MINOR=$(echo $CURRENT_VERSION | cut -d. -f2) PATCH=$(echo $CURRENT_VERSION | cut -d. -f3 | cut -d- -f1) NEW_PATCH=$((PATCH + 1)) NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH-SNAPSHOT" # Update version in pom.xml mvn versions:set -DnewVersion=$NEW_VERSION mvn versions:commit # Commit and push the new snapshot version git add pom.xml git commit -m "Increment version to $NEW_VERSION [skip ci]" git push origin $(git rev-parse --abbrev-ref HEAD)
Bonus workflow
The CodeQL workflow analyzes your Java code to identify potential vulnerabilities and issues.
name: CodeQL on: pull_request: branches: [ "main" ] workflow_dispatch: jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: actions: read contents: read security-events: write strategy: fail-fast: false matrix: language: [ 'java' ] steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Java uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' cache: maven cache-dependency-path: '**/pom.xml' - name: Initialize CodeQL uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} - name: Autobuild uses: github/codeql-action/autobuild@v3 - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v3 with: category: "/language:${{matrix.language}}"
Updated Dockerfile
### Build stage FROM eclipse-temurin:21-jre-alpine AS builder # Set the working directory inside the container WORKDIR /tmp # Copy the source code into the container COPY target/*.jar app.jar # Extract the layers RUN java -Djarmode=layertools -jar app.jar extract ### Run stage # Create a minimal production image FROM eclipse-temurin:21-jre-alpine # Set the working directory inside the container WORKDIR /app # Set the working directory inside the container COPY --from=builder /tmp/dependencies/ ./ COPY --from=builder /tmp/snapshot-dependencies/ ./ COPY --from=builder /tmp/spring-boot-loader/ ./ COPY --from=builder /tmp/application/ ./ # Run the binary when the container starts ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
The above is the detailed content of SpringBoot Web Service - Part Github Action. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The difference between HashMap and Hashtable is mainly reflected in thread safety, null value support and performance. 1. In terms of thread safety, Hashtable is thread-safe, and its methods are mostly synchronous methods, while HashMap does not perform synchronization processing, which is not thread-safe; 2. In terms of null value support, HashMap allows one null key and multiple null values, while Hashtable does not allow null keys or values, otherwise a NullPointerException will be thrown; 3. In terms of performance, HashMap is more efficient because there is no synchronization mechanism, and Hashtable has a low locking performance for each operation. It is recommended to use ConcurrentHashMap instead.

StaticmethodsininterfaceswereintroducedinJava8toallowutilityfunctionswithintheinterfaceitself.BeforeJava8,suchfunctionsrequiredseparatehelperclasses,leadingtodisorganizedcode.Now,staticmethodsprovidethreekeybenefits:1)theyenableutilitymethodsdirectly

The JIT compiler optimizes code through four methods: method inline, hot spot detection and compilation, type speculation and devirtualization, and redundant operation elimination. 1. Method inline reduces call overhead and inserts frequently called small methods directly into the call; 2. Hot spot detection and high-frequency code execution and centrally optimize it to save resources; 3. Type speculation collects runtime type information to achieve devirtualization calls, improving efficiency; 4. Redundant operations eliminate useless calculations and inspections based on operational data deletion, enhancing performance.

Instance initialization blocks are used in Java to run initialization logic when creating objects, which are executed before the constructor. It is suitable for scenarios where multiple constructors share initialization code, complex field initialization, or anonymous class initialization scenarios. Unlike static initialization blocks, it is executed every time it is instantiated, while static initialization blocks only run once when the class is loaded.

Java uses wrapper classes because basic data types cannot directly participate in object-oriented operations, and object forms are often required in actual needs; 1. Collection classes can only store objects, such as Lists use automatic boxing to store numerical values; 2. Generics do not support basic types, and packaging classes must be used as type parameters; 3. Packaging classes can represent null values ??to distinguish unset or missing data; 4. Packaging classes provide practical methods such as string conversion to facilitate data parsing and processing, so in scenarios where these characteristics are needed, packaging classes are indispensable.

Factory mode is used to encapsulate object creation logic, making the code more flexible, easy to maintain, and loosely coupled. The core answer is: by centrally managing object creation logic, hiding implementation details, and supporting the creation of multiple related objects. The specific description is as follows: the factory mode handes object creation to a special factory class or method for processing, avoiding the use of newClass() directly; it is suitable for scenarios where multiple types of related objects are created, creation logic may change, and implementation details need to be hidden; for example, in the payment processor, Stripe, PayPal and other instances are created through factories; its implementation includes the object returned by the factory class based on input parameters, and all objects realize a common interface; common variants include simple factories, factory methods and abstract factories, which are suitable for different complexities.

InJava,thefinalkeywordpreventsavariable’svaluefrombeingchangedafterassignment,butitsbehaviordiffersforprimitivesandobjectreferences.Forprimitivevariables,finalmakesthevalueconstant,asinfinalintMAX_SPEED=100;wherereassignmentcausesanerror.Forobjectref

There are two types of conversion: implicit and explicit. 1. Implicit conversion occurs automatically, such as converting int to double; 2. Explicit conversion requires manual operation, such as using (int)myDouble. A case where type conversion is required includes processing user input, mathematical operations, or passing different types of values ??between functions. Issues that need to be noted are: turning floating-point numbers into integers will truncate the fractional part, turning large types into small types may lead to data loss, and some languages ??do not allow direct conversion of specific types. A proper understanding of language conversion rules helps avoid errors.
