Skip to content

Check for dependency changes #22

Check for dependency changes

Check for dependency changes #22

Workflow file for this run

name: Check for dependency changes
on:
workflow_dispatch:
schedule:
- cron: '*/60 * * * *'
jobs:
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/[email protected]
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 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/[email protected]
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 }}"