From 05519e0f5849c37ceca26bacb672df07ce177f35 Mon Sep 17 00:00:00 2001 From: "Kipchumba C. Bett" Date: Fri, 17 May 2024 12:24:34 +0300 Subject: [PATCH] Check all maven modules in project. --- .github/workflows/check-deps.yml | 197 +++++++++++++++++++++++++++++-- 1 file changed, 189 insertions(+), 8 deletions(-) diff --git a/.github/workflows/check-deps.yml b/.github/workflows/check-deps.yml index 8089e9b8..359d0a06 100644 --- a/.github/workflows/check-deps.yml +++ b/.github/workflows/check-deps.yml @@ -6,11 +6,192 @@ on: - cron: '*/60 * * * *' jobs: - check-deps: - uses: mekomsolutions/shared-github-workflow/.github/workflows/maven-check-deps-build-publish.yml@main - with: - java-version: "8" - maven-args: "-P prod -DskipTests=true" - secrets: - NEXUS_USERNAME: ${{ secrets.MEKOM_NEXUS_USERNAME }} - NEXUS_PASSWORD: ${{ secrets.MEKOM_NEXUS_PASSWORD }} + check-for-dependency-changes: + outputs: + hasChanged: ${{ steps.compareDependencyChanges.outputs.hasChanged }} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 8 + uses: actions/setup-java@v4 + with: + java-version: 8 + distribution: 'temurin' + cache: 'maven' + + - name: Set settings.xml + uses: s4u/maven-settings-action@v3.0.0 + with: + servers: | + [{ + "id": "mks-private-repo", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-private", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-public-snapshots", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-public-releases", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-private-snapshots", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-private-releases", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }] + + - name: Check for dependency changes + id: compareDependencyChanges + shell: bash + run: | + #!/usr/bin/env bash + set -e + + # Download the remote dependency report + remoteRepoUrl=https://nexus.mekomsolutions.net/repository/maven-public + + # Get the list of sub-modules + modules=$(mvn -q -Pprod --also-make exec:exec -Dexec.executable="echo" -Dexec.args='${project.build.directory}') + + echo "Modules to check" + echo "$modules" + + # Build the local dependency report + echo "Compile a local dependency report..." + mvn clean compile -P prod + + # Iterate over each module + for module in $modules; do + modulePath=$(echo $module | sed 's/\\/\//g') + + # Navigate to the module directory + cd $(dirname $modulePath) + + # Get the groupId, artifactId, and version + groupId=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout) + artifactId=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout) + version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) + classifier=$(mvn help:evaluate -Dexpression=dependencyReportClassifier -q -DforceStdout) + artifact="${groupId}":"${artifactId}":"${version}":txt:"${classifier}" + + # Module details + echo "Module: $module" + echo "GroupId: $groupId" + echo "ArtifactId: $artifactId" + echo "Version: $version" + echo "" + + filename=${artifactId}-${version}-${classifier}.txt + absolutePath=/tmp/$filename + + # Remove previous file if any + rm -f "${absolutePath}" + + # Fetch the remote dependency report + echo "Fetch remote dependency report..." + set +e + mvn org.apache.maven.plugins:maven-dependency-plugin:3.6.0:get -DremoteRepositories=${remoteRepoUrl} -Dartifact=${artifact} -Dtransitive=false + mvn org.apache.maven.plugins:maven-dependency-plugin:3.6.0:copy -Dartifact=${artifact} -DoutputDirectory=/tmp/ -Dmdep.useBaseVersion=true + set -e + + # If no dependency report was fetched, create an empty one. + if [ ! -f "$absolutePath" ]; then + echo "Remote dependency file is not found at '${absolutePath}'. Creating an empty one and continue." + touch "${absolutePath}" + else + echo "Remote dependency file saved at '${absolutePath}'." + fi + + # Compare the 2 files. Will exit with 0 if no change, 1 if changes + echo "Compare both dependency reports..." + set +e + if [ -f ./target/"${filename}" ]; then + diff "${absolutePath}" ./target/"${filename}" + diff_rc=$? + + if [ $diff_rc -eq 0 ]; then + echo "No dependency change. Exit (0)" + elif [ $diff_rc -eq 1 ]; then + echo "One or more dependency has changed. Exit (1)" + else + echo "Unknown error occured." + fi + + # Export result code to be used in later steps of the GitHub worklow. + set +e + echo ::set-env name=hasChanged::$diff_rc + echo hasChanged=$diff_rc >> "$GITHUB_OUTPUT" + set -e + else + echo "File ./target/${filename} does not exist." + fi + set -e + + # Navigate back to the parent directory + cd - + done + + exit 0 + + build: + needs: check-for-dependency-changes + if: ${{ needs.check-for-dependency-changes.outputs.hasChanged == '1' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK ${{ inputs.java-version }} + uses: actions/setup-java@v4 + with: + java-version: ${{ inputs.java-version }} + distribution: ${{ inputs.java-distribution }} + cache: 'maven' + + - name: Set settings.xml + uses: s4u/maven-settings-action@v3.0.0 + with: + servers: | + [{ + "id": "mks-private-repo", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-public-snapshots", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-public-releases", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-private-snapshots", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }, + { + "id": "mks-nexus-private-releases", + "username": "${{ secrets.MEKOM_NEXUS_USERNAME }}", + "password": "${{ secrets.MEKOM_NEXUS_PASSWORD }}" + }] + + - name: Build + run: "mvn --batch-mode clean package ${{ inputs.maven-args }}"