239 lines
8.6 KiB
YAML
239 lines
8.6 KiB
YAML
name: CI - Test, Publish and Release
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
|
|
jobs:
|
|
run-tests:
|
|
runs-on: java26
|
|
|
|
steps:
|
|
- name: Checkout Code
|
|
run: |
|
|
SERVER_DOMAIN=$(echo "${{ github.server_url }}" | sed 's/https:\/\///')
|
|
rm -rf "$GITHUB_WORKSPACE"/*
|
|
git clone "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@${SERVER_DOMAIN}/${{ github.repository }}.git" "$GITHUB_WORKSPACE"
|
|
cd "$GITHUB_WORKSPACE"
|
|
git checkout ${{ github.sha }}
|
|
|
|
- name: Make gradlew executable
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
chmod +x ./gradlew
|
|
|
|
- name: Run JUnit tests
|
|
id: run_tests
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
./gradlew test --no-daemon --stacktrace
|
|
|
|
- name: Upload test reports
|
|
if: always()
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
if [ -d "build/reports/tests/test" ]; then
|
|
echo "Test reports available in build/reports/tests/test"
|
|
ls -la build/reports/tests/test || true
|
|
fi
|
|
if [ -d "build/test-results/test" ]; then
|
|
echo "Test result XMLs:"
|
|
ls -la build/test-results/test || true
|
|
fi
|
|
|
|
check-and-publish:
|
|
runs-on: java26
|
|
# Publish to the Maven repo after the Gitea release has been created
|
|
needs: create-release
|
|
env:
|
|
MAVEN_REPO_URL: ${{ secrets.MAVEN_REPO_URL }}
|
|
MAVEN_REPO_USER: ${{ secrets.MAVEN_REPO_USER }}
|
|
MAVEN_REPO_PASS: ${{ secrets.MAVEN_REPO_PASS }}
|
|
|
|
steps:
|
|
- name: Checkout Code
|
|
run: |
|
|
SERVER_DOMAIN=$(echo "${{ github.server_url }}" | sed 's/https:\/\///')
|
|
rm -rf "$GITHUB_WORKSPACE"/*
|
|
git clone "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@${SERVER_DOMAIN}/${{ github.repository }}.git" "$GITHUB_WORKSPACE"
|
|
cd "$GITHUB_WORKSPACE"
|
|
git checkout ${{ github.sha }}
|
|
|
|
- name: Read version from Gradle
|
|
id: get_version
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
chmod +x ./gradlew
|
|
VERSION=$(./gradlew properties | grep "^version:" | awk '{print $2}')
|
|
echo "Found local project version: $VERSION"
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
|
|
- name: Check if version exists on repository
|
|
id: check_repo
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
RAW_GROUP=$(./gradlew properties | grep "^group:" | awk '{print $2}')
|
|
GROUP_PATH=$(echo "$RAW_GROUP" | tr '.' '/')
|
|
ARTIFACT_ID=$(./gradlew properties | grep "^name:" | awk '{print $2}')
|
|
|
|
LOCAL_VERSION="${{ steps.get_version.outputs.version }}"
|
|
|
|
echo "Detected project: $RAW_GROUP:$ARTIFACT_ID:$LOCAL_VERSION"
|
|
|
|
CHECK_URL="${{ env.MAVEN_REPO_URL }}/${GROUP_PATH}/${ARTIFACT_ID}/${LOCAL_VERSION}/${ARTIFACT_ID}-${LOCAL_VERSION}.pom"
|
|
echo "Check url: $CHECK_URL"
|
|
|
|
STATUS=$(curl -o /dev/null -s -w "%{http_code}" -u "${{ env.MAVEN_REPO_USER }}:${{ env.MAVEN_REPO_PASS }}" "$CHECK_URL")
|
|
if [ "$STATUS" = "200" ]; then
|
|
echo "Version $LOCAL_VERSION already exists in repository. Skipping publishing."
|
|
echo "is_new=false" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Version $LOCAL_VERSION not found (Status $STATUS). Start deployment..."
|
|
echo "is_new=true" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Push to Maven Repository
|
|
if: steps.check_repo.outputs.is_new == 'true'
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
echo "Publishing version ${{ steps.get_version.outputs.version }} zu Repository..."
|
|
./gradlew publish
|
|
|
|
create-release:
|
|
runs-on: java26
|
|
# Create the Gitea tag/release after tests pass, before publishing
|
|
needs: run-tests
|
|
env:
|
|
API_BASE: ${{ github.server_url }}/api/v1/repos/${{ github.repository }}
|
|
|
|
steps:
|
|
- name: Checkout Code
|
|
run: |
|
|
SERVER_DOMAIN=$(echo "${{ github.server_url }}" | sed 's/https:\/\///')
|
|
rm -rf "$GITHUB_WORKSPACE"/*
|
|
git clone "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@${SERVER_DOMAIN}/${{ github.repository }}.git" "$GITHUB_WORKSPACE"
|
|
cd "$GITHUB_WORKSPACE"
|
|
git checkout ${{ github.sha }}
|
|
|
|
- name: Read version from Gradle
|
|
id: get_version
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
chmod +x ./gradlew
|
|
VERSION=$(./gradlew properties | grep "^version:" | awk '{print $2}')
|
|
echo "Found local project version: $VERSION"
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
|
|
|
|
- name: Build JAR
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
# Tests already ran in the run-tests job, so skip them here.
|
|
./gradlew jar --no-daemon --stacktrace -x test
|
|
|
|
- name: Locate built JAR
|
|
id: find_jar
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
JAR_PATH=$(ls build/libs/*.jar | grep -v -e '-sources.jar' -e '-javadoc.jar' | head -n1)
|
|
if [ -z "$JAR_PATH" ]; then
|
|
echo "No JAR found in build/libs"
|
|
exit 1
|
|
fi
|
|
echo "Found JAR: $JAR_PATH"
|
|
echo "jar_path=$JAR_PATH" >> $GITHUB_OUTPUT
|
|
echo "jar_name=$(basename "$JAR_PATH")" >> $GITHUB_OUTPUT
|
|
|
|
- name: Check if tag/release already exists
|
|
id: check_tag
|
|
run: |
|
|
TAG="${{ steps.get_version.outputs.tag }}"
|
|
CHECK_URL="${{ env.API_BASE }}/releases/tags/${TAG}"
|
|
echo "Checking for existing release: $CHECK_URL"
|
|
|
|
STATUS=$(curl -o /dev/null -s -w "%{http_code}" \
|
|
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
|
"$CHECK_URL")
|
|
|
|
if [ "$STATUS" = "200" ]; then
|
|
echo "Release for tag $TAG already exists. Skipping."
|
|
echo "is_new=false" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "No release found for tag $TAG (Status $STATUS). Creating release..."
|
|
echo "is_new=true" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Create tag and release on Gitea
|
|
id: create_release
|
|
if: steps.check_tag.outputs.is_new == 'true'
|
|
run: |
|
|
TAG="${{ steps.get_version.outputs.tag }}"
|
|
VERSION="${{ steps.get_version.outputs.version }}"
|
|
CREATE_URL="${{ env.API_BASE }}/releases"
|
|
echo "Creating release $TAG at $CREATE_URL"
|
|
|
|
# Gitea creates the tag automatically from target_commitish when it
|
|
# does not yet exist, so a separate tag-creation call is not needed.
|
|
BODY=$(cat <<EOF
|
|
{
|
|
"tag_name": "${TAG}",
|
|
"target_commitish": "${{ github.sha }}",
|
|
"name": "Release ${VERSION}",
|
|
"body": "Automated release for version ${VERSION}.",
|
|
"draft": false,
|
|
"prerelease": false
|
|
}
|
|
EOF
|
|
)
|
|
|
|
HTTP_STATUS=$(curl -s -o /tmp/release_response.json -w "%{http_code}" \
|
|
-X POST \
|
|
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$BODY" \
|
|
"$CREATE_URL")
|
|
|
|
echo "Response ($HTTP_STATUS):"
|
|
cat /tmp/release_response.json
|
|
|
|
if [ "$HTTP_STATUS" != "201" ]; then
|
|
echo "Failed to create release (HTTP $HTTP_STATUS)"
|
|
exit 1
|
|
fi
|
|
|
|
RELEASE_ID=$(grep -o '"id":[0-9]*' /tmp/release_response.json | head -n1 | cut -d':' -f2)
|
|
if [ -z "$RELEASE_ID" ]; then
|
|
echo "Could not determine release id from response"
|
|
exit 1
|
|
fi
|
|
echo "Release $TAG created successfully (id $RELEASE_ID)."
|
|
echo "release_id=$RELEASE_ID" >> $GITHUB_OUTPUT
|
|
|
|
- name: Attach JAR to release
|
|
if: steps.check_tag.outputs.is_new == 'true'
|
|
run: |
|
|
cd "$GITHUB_WORKSPACE"
|
|
JAR_PATH="${{ steps.find_jar.outputs.jar_path }}"
|
|
JAR_NAME="${{ steps.find_jar.outputs.jar_name }}"
|
|
RELEASE_ID="${{ steps.create_release.outputs.release_id }}"
|
|
UPLOAD_URL="${{ env.API_BASE }}/releases/${RELEASE_ID}/assets?name=${JAR_NAME}"
|
|
echo "Uploading $JAR_NAME to $UPLOAD_URL"
|
|
|
|
HTTP_STATUS=$(curl -s -o /tmp/asset_response.json -w "%{http_code}" \
|
|
-X POST \
|
|
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
|
-H "Content-Type: multipart/form-data" \
|
|
-F "attachment=@${JAR_PATH}" \
|
|
"$UPLOAD_URL")
|
|
|
|
echo "Response ($HTTP_STATUS):"
|
|
cat /tmp/asset_response.json
|
|
|
|
if [ "$HTTP_STATUS" != "201" ]; then
|
|
echo "Failed to upload JAR asset (HTTP $HTTP_STATUS)"
|
|
exit 1
|
|
fi
|
|
|
|
echo "JAR $JAR_NAME attached to release successfully."
|