diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6c1142a..a4dd528 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,27 +10,28 @@ jobs: name: Build and analyze runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v7 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: Set up JDK 11 - uses: actions/setup-java@v3 + uses: actions/setup-java@v6 with: java-version: 11 distribution: 'zulu' # Alternative distribution options are available. - name: Cache SonarCloud packages - uses: actions/cache@v3 + uses: actions/cache@v5 with: path: ~/.sonar/cache key: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar - name: Cache Maven packages - uses: actions/cache@v3 + uses: actions/cache@v5 with: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-m2 - name: Build and analyze + working-directory: launcher env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000..572ac01 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,128 @@ +name: Build Docker image for new Java releases + +on: + # Weekly check for new Temurin releases (every Monday at 04:17 UTC) + schedule: + - cron: "17 4 * * 1" + + # Allow manual trigger + workflow_dispatch: + push: + branches: + - "main" + +permissions: + contents: read + packages: write + +env: + DOCKER_IMAGE: fathzer/db-backup + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + # ------------------------------------------------------------ + # 1. Set up JDK 21 and build the scanner + # ------------------------------------------------------------ + - name: Set up JDK 21 + uses: actions/setup-java@v5 + with: + java-version: 21 + distribution: 'temurin' + + - name: Cache Maven packages + uses: actions/cache@v5 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Build new-java-release-scanner + working-directory: new-java-release-scanner + run: mvn -B -q package + + # ------------------------------------------------------------ + # 2. Find the launcher version and target Java version on this branch + # ------------------------------------------------------------ + - name: Get launcher version and Java version + id: app-version + run: | + APP_VERSION=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version -f launcher/pom.xml) + JAVA_VERSION=$(mvn -q -DforceStdout help:evaluate -Dexpression=maven.compiler.target -f launcher/pom.xml) + echo "version=$APP_VERSION" >> "$GITHUB_OUTPUT" + echo "javaVersion=$JAVA_VERSION" >> "$GITHUB_OUTPUT" + echo "Launcher version: $APP_VERSION" + echo "Target Java version: $JAVA_VERSION" + + # ------------------------------------------------------------ + # 3. Find the Java tag to build (or skip if already published) + # ------------------------------------------------------------ + - name: Scan Docker Hub for new Java version + id: scan + env: + DOCKER_USER: ${{ secrets.DOCKER_USER }} + DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + run: | + set -euo pipefail + SCANNER_JAR="new-java-release-scanner/target/docker-java-version-scanner-1.0.0.jar" + RESULT=$(java -jar "$SCANNER_JAR" "${{ steps.app-version.outputs.version }}" "${{ steps.app-version.outputs.javaVersion }}" "${DOCKER_IMAGE}") + echo "$RESULT" + SHOULD_BUILD=$(echo "$RESULT" | jq -r '.shouldBuild') + echo "shouldBuild=$SHOULD_BUILD" >> "$GITHUB_OUTPUT" + if [ "$SHOULD_BUILD" = "true" ]; then + BASE_IMAGE=$(echo "$RESULT" | jq -r '.baseImage') + echo "baseImage=$BASE_IMAGE" >> "$GITHUB_OUTPUT" + { + echo "tags<> "$GITHUB_OUTPUT" + fi + + # ------------------------------------------------------------ + # 4. Download the launcher jar from Maven Central + # (fails if the version is not published yet) + # ------------------------------------------------------------ + - name: Download jar from Maven Central + if: steps.scan.outputs.shouldBuild == 'true' + run: | + set -euo pipefail + VERSION="${{ steps.app-version.outputs.version }}" + JAR_URL="https://repo1.maven.org/maven2/com/fathzer/jdbbackup-docker/${VERSION}/jdbbackup-docker-${VERSION}.jar" + echo "Downloading $JAR_URL" + mkdir -p launcher/target + curl -fSL -o launcher/target/jdbbackup.jar "$JAR_URL" + + # ------------------------------------------------------------ + # 5. Build and push the Docker image + # ------------------------------------------------------------ + - name: Login to Docker Hub + if: steps.scan.outputs.shouldBuild == 'true' + uses: docker/login-action@v4 + with: + username: ${{ secrets.DOCKER_USER }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Setup Buildx + if: steps.scan.outputs.shouldBuild == 'true' + uses: docker/setup-buildx-action@v4 + + - name: Build and push image + if: steps.scan.outputs.shouldBuild == 'true' + uses: docker/build-push-action@v7 + with: + context: . + push: true + build-args: | + JAVA_BASE_IMAGE=${{ steps.scan.outputs.baseImage }} + JAR_FILE=launcher/target/jdbbackup.jar + tags: ${{ steps.scan.outputs.tags }} + labels: | + org.opencontainers.image.title=db-backup + org.opencontainers.image.version=${{ steps.app-version.outputs.version }} + org.opencontainers.image.base.name=${{ steps.scan.outputs.baseImage }} diff --git a/.gitignore b/.gitignore index 0c837c9..43bb0f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ target/ +bin/ .settings/ dependency-reduced-pom.xml -/.classpath -/.project +.classpath +.project +.vscode/ diff --git a/Dockerfile b/Dockerfile index 7e837ad..864c817 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,16 @@ -FROM openjdk:11-jre +ARG JAVA_BASE_IMAGE=eclipse-temurin:25.0.4_7-jre +FROM ${JAVA_BASE_IMAGE} -RUN apt-get update && apt-get install -y default-mysql-client -RUN apt-get clean +RUN apt-get update && apt-get install -y default-mysql-client && apt-get clean -ENV RUN_USER jdbbackup -ENV RUN_GROUP jdbbackup +ENV RUN_USER=jdbbackup +ENV RUN_GROUP=jdbbackup RUN groupadd -r ${RUN_GROUP} && useradd -g ${RUN_GROUP} -m -s /bin/bash ${RUN_USER} WORKDIR /home/${RUN_USER} -COPY target/jdbbackup.jar . +ARG JAR_FILE +COPY ${JAR_FILE} jdbbackup.jar RUN chown -R ${RUN_USER}:${RUN_USER} . USER ${RUN_USER} diff --git a/README.md b/README.md index d50421a..e26ef6b 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ A ready to use docker container, based on [jdbbackup-core](https://github.com/jd ### Without Docker This application requires Java11+. -The [artifact deployed in Maven central](https://repo1.maven.org/maven2/com/fathzer/jdbbackup-docker/1.0.0/jdbbackup-docker-1.0.0.jar) is a runnable jar. -Launch it with ```java -jar jdbbackup-docker-1.0.0.jar config.json``` where *config.json* is the configuration file ([see below](#configuration-file)) or set the environment variable *TASKS_PATH* to the path of the configuration file and launch it with ```java -jar jdbbackup-docker-1.0.0.jar```. You may also leave *TASKS_PATH* unset, its default value is *tasks.json*. +The [artifact deployed in Maven central](https://repo1.maven.org/maven2/com/fathzer/jdbbackup-docker/1.1.0/jdbbackup-docker-1.1.0.jar) is a runnable jar. +Launch it with ```java -jar jdbbackup-docker-1.1.0.jar config.json``` where *config.json* is the configuration file ([see below](#configuration-file)) or set the environment variable *TASKS_PATH* to the path of the configuration file and launch it with ```java -jar jdbbackup-docker-1.1.0.jar```. You may also leave *TASKS_PATH* unset, its default value is *tasks.json*. If you want to include this application in a Java program, the main class is *com.fathzer.jdbbackup.cron.Main*. @@ -96,6 +96,18 @@ The default configuration logs to the console, rejecting entries below *info* le If you want to change logback configuration, please have a look at [the logback manual](https://logback.qos.ch/manual/configuration.html). # Developer notes -- How to build the Docker image?: ```mvn package -Pdocker``` +- Build the jar: + ```bash + cd launcher + mvn package -DskipTests + ``` +- Build the Docker image (from the repository root): + + ```bash + docker build -t fathzer/db-backup:1.1.0-jre-25.0.4 --build-arg JAVA_BASE_IMAGE=eclipse-temurin:25.0.4_7-jre --build-arg JAR_FILE=launcher/target/jdbbackup.jar . + ``` + The `JAVA_BASE_IMAGE` build arg sets the base JRE image. The `JAR_FILE` build arg points to the jar relative to the repository root (the Docker context). + + > **Warning**: do not push the built image to Docker Hub unless you're sure about the tag format; it may not follow the naming convention used by the official images. diff --git a/pom.xml b/launcher/pom.xml similarity index 79% rename from pom.xml rename to launcher/pom.xml index e5d699d..b82d3c4 100644 --- a/pom.xml +++ b/launcher/pom.xml @@ -3,15 +3,18 @@ com.fathzer parent-pom - 1.0.8 + 1.1.0 + jdbbackup-docker - 1.0.0 - A simple java library to perform data source backup and store them in dropbox, sftp or local file + 1.1.0 + jdbbackup-docker + A launcher for jdbbackup that can be used in a docker container - https://github.com/jdbbackup/jdbbackup-dropbox.git - https://github.com/jdbbackup/jdbbackup-dropbox.git + https://github.com/jdbbackup/jdbbackup-docker.git + https://github.com/jdbbackup/jdbbackup-docker.git + UTF-8 11 @@ -19,6 +22,7 @@ ${maven.compiler.target} ${maven.compiler.target} jdbbackup + 1.18.48 @@ -29,29 +33,6 @@ jdbbackup - - - com.spotify - dockerfile-maven-plugin - 1.4.13 - - - default - - build - push - - - - - fathzer/db-backup - ${project.version} - - target/${project.build.finalName}.jar - - - - @@ -60,7 +41,7 @@ com.fathzer jdbbackup-core - 1.0.0 + 1.1.0