163 lines
6.1 KiB
YAML
163 lines
6.1 KiB
YAML
name: CI/CD
|
|
|
|
on:
|
|
pull_request:
|
|
branches:
|
|
- master
|
|
types:
|
|
- closed
|
|
|
|
jobs:
|
|
build-deploy:
|
|
runs-on: act_runner_java
|
|
if: ${{ github.event.pull_request.merged == true }}
|
|
env:
|
|
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
|
|
steps:
|
|
- name: Checkout code
|
|
run: |
|
|
git clone ${{ github.server_url }}/${{ github.repository }}.git .
|
|
git checkout ${{ github.sha }}
|
|
- name: Set up environment
|
|
run: |
|
|
echo "PR #${{ github.event.number }} merged into master"
|
|
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
|
|
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
|
|
- name: Run tests
|
|
run: |
|
|
echo "Running test suite..."
|
|
- name: Build project
|
|
run: |
|
|
mvn -B -DskipTests clean package source:jar javadoc:jar
|
|
- name: Deploy to Nexus
|
|
if: success()
|
|
run: |
|
|
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
|
|
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
|
|
exit 1
|
|
fi
|
|
mkdir -p ~/.m2
|
|
cat > ~/.m2/settings.xml <<EOF
|
|
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
|
<servers>
|
|
<server>
|
|
<id>timi-nexus</id>
|
|
<username>${{ secrets.NEXUS_USERNAME }}</username>
|
|
<password>${{ secrets.NEXUS_PASSWORD }}</password>
|
|
</server>
|
|
</servers>
|
|
</settings>
|
|
EOF
|
|
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
|
|
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
|
|
main_jar="target/${artifact_id}-${version}.jar"
|
|
sources_jar="target/${artifact_id}-${version}-sources.jar"
|
|
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
|
|
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
|
|
echo "Missing build artifacts in target"
|
|
exit 1
|
|
fi
|
|
mvn -B deploy:deploy-file \
|
|
-Dfile="$main_jar" \
|
|
-Dsources="$sources_jar" \
|
|
-Djavadoc="$javadoc_jar" \
|
|
-DpomFile="./pom.xml" \
|
|
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
|
|
-DrepositoryId="timi-nexus" \
|
|
-Dhttps.protocols=TLSv1.2 \
|
|
-Djdk.tls.client.protocols=TLSv1.2
|
|
- name: Create release
|
|
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
|
|
GITEA_SERVER_URL: ${{ github.server_url }}
|
|
GITEA_REPOSITORY: ${{ github.repository }}
|
|
RELEASE_TAG: ${{ github.event.pull_request.title }}
|
|
RELEASE_TARGET: ${{ github.sha }}
|
|
run: |
|
|
if [ -z "$GITEA_TOKEN" ]; then
|
|
echo "Missing secrets.RUNNER_TOKEN"
|
|
exit 1
|
|
fi
|
|
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
|
|
payload=$(cat <<EOF
|
|
{
|
|
"tag_name": "$RELEASE_TAG",
|
|
"name": "$RELEASE_TAG",
|
|
"target_commitish": "$RELEASE_TARGET",
|
|
"draft": false,
|
|
"prerelease": false
|
|
}
|
|
EOF
|
|
)
|
|
echo "Creating release with tag: $RELEASE_TAG"
|
|
echo "API URL: $api_url"
|
|
echo "Target commit: $RELEASE_TARGET"
|
|
|
|
http_code=$(curl -sS -w "%{http_code}" -o /tmp/release_response.json -X POST "$api_url" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload")
|
|
|
|
response=$(cat /tmp/release_response.json)
|
|
echo "HTTP Status: $http_code"
|
|
echo "Response: $response"
|
|
|
|
if [ "$http_code" -ne 201 ]; then
|
|
echo "Failed to create release (HTTP $http_code)"
|
|
if echo "$response" | grep -q "already exists"; then
|
|
echo "Release with tag $RELEASE_TAG already exists"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
release_id=$(echo "$response" | grep -oP '"id":\K[0-9]+' | head -n 1 || true)
|
|
if [ -z "$release_id" ]; then
|
|
echo "Failed to extract release ID from response"
|
|
exit 1
|
|
fi
|
|
echo "Release created: id=$release_id"
|
|
|
|
echo "Listing jar files in target directory:"
|
|
ls -lh target/*.jar || echo "No jar files found"
|
|
|
|
upload_count=0
|
|
for asset_path in target/*.jar; do
|
|
if [ ! -f "$asset_path" ]; then
|
|
echo "Skipping non-existent file: $asset_path"
|
|
continue
|
|
fi
|
|
asset_name=$(basename "$asset_path")
|
|
file_size=$(stat -c%s "$asset_path" 2>/dev/null || echo "unknown")
|
|
echo "Uploading asset: $asset_name (size: $file_size bytes)"
|
|
|
|
upload_url="$api_url/$release_id/assets?name=$asset_name"
|
|
echo "Upload URL: $upload_url"
|
|
|
|
set +e
|
|
http_code=$(curl -sS -w "%{http_code}" -o /tmp/asset_response.json -X POST "$upload_url" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/octet-stream" \
|
|
--data-binary @"$asset_path" 2>/dev/null)
|
|
curl_exit=$?
|
|
set -e
|
|
|
|
if [ $curl_exit -ne 0 ]; then
|
|
echo "✗ Curl failed with exit code $curl_exit for $asset_name"
|
|
cat /tmp/asset_response.json 2>/dev/null || echo "No response file"
|
|
continue
|
|
fi
|
|
|
|
if [ "$http_code" = "201" ]; then
|
|
echo "✓ Successfully uploaded: $asset_name"
|
|
upload_count=$((upload_count + 1))
|
|
else
|
|
echo "✗ Failed to upload $asset_name (HTTP $http_code)"
|
|
cat /tmp/asset_response.json 2>/dev/null || echo "No response body"
|
|
fi
|
|
done
|
|
|
|
echo "Upload complete: $upload_count file(s) uploaded"
|