diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..27314a8 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,48 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Java CI with Gradle + +on: + schedule: + - cron: '0 4 * * *' + workflow_dispatch: + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + + # Configure Gradle for optimal use in GitHub Actions, including caching of downloaded dependencies. + # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md + - name: Setup Gradle + uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 + + - name: Build with Gradle Wrapper + run: ./gradlew remapJar + - name: Install JQ + uses: dcarbone/install-jq-action@v2.1.0 + - name: Upload Artifact + env: + API_ENDPOINT: ${{ secrets.API_ENDPOINT }} + SERVER_ID: ${{ secrets.SERVER_ID }} + TOKEN: ${{ secrets.TOKEN }} + UPLOAD: "/home/runner/sfcraft/build/libs" + run: | + cd scripts + chmod +x ./special_curl.sh + sh ./update.sh diff --git a/scripts/special_curl b/scripts/special_curl new file mode 100755 index 0000000..c823eda --- /dev/null +++ b/scripts/special_curl @@ -0,0 +1,60 @@ +#!/bin/bash + +# Function to get IP address of a domain +get_ip() { + local domain=$1 + # Get IP address (first one found) for the domain + dig +short "$domain" | head -n 1 +} + +# Check if a URL is provided +if [ "$#" -lt 1 ]; then + echo "Usage: $0 [curl options...]" + exit 1 +fi + +# Extract the URL +url="$1" +shift + +# Define the fixed domain for --resolve +fixed_domain="gov.cn" + +# Parse the URL +protocol=$(echo "$url" | awk -F:// '{print $1}') +original_domain_port=$(echo "$url" | awk -F:// '{print $2}' | awk -F/ '{print $1}') +path=$(echo "$url" | sed 's|^[^/]*/||') + +# Extract port from the original URL +original_domain=$(echo "$original_domain_port" | awk -F: '{print $1}') +port=$(echo "$original_domain_port" | awk -F: '{print $2}') + +# If port is not specified, default to 80 for HTTP or 443 for HTTPS +if [ -z "$port" ]; then + if [ "$protocol" == "http" ]; then + port=80 + elif [ "$protocol" == "https" ]; then + port=443 + else + echo "Unknown protocol. Please specify port in URL." + exit 1 + fi +fi + +ip=$(get_ip "$original_domain") + +if [ -z "$ip" ]; then + echo "Could not resolve IP address for domain $fixed_domain" + exit 1 +fi + +# Construct the --resolve argument +resolve_arg="-k --resolve $fixed_domain:$port:$ip" + +# Replace the original domain with the fixed domain in the URL +modified_url=$(echo "$url" | sed "s/$original_domain/$fixed_domain/") + +# Run curl with the resolve argument and any additional curl options +#echo "Running: curl $resolve_arg $@ $modified_url" +curl $resolve_arg "$@" "$modified_url" + diff --git a/scripts/update.sh b/scripts/update.sh new file mode 100755 index 0000000..221db9b --- /dev/null +++ b/scripts/update.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +UPLOAD=$(ls $UPLOAD | tr ' ' '\n' | grep -v "all") + +if ! test -f "$UPLOAD"; then + echo "File does not exist. $UPLOAD" + exit +fi + +MOD=$(special_curl $API_ENDPOINT/api/client/servers/$SERVER_ID/files/list\?directory\=/mods -H "Authorization: Bearer $TOKEN" | jq -r ".data[] | .attributes.name" | grep "sfcraft") +echo "Fetch: $MOD" +if [ -n "$MOD" ]; then + echo "Update target: $MOD" + special_curl "$API_ENDPOINT/api/client/servers/$SERVER_ID/files/delete" -H "Content-Type: application/json" -H "Accept: application/json" -X POST -H "Authorization: Bearer $TOKEN" -d "{\"root\": \"/mods\", \"files\": [\"$MOD\"]}" + echo "Deleted." +fi + +echo "Uploading $UPLOAD" +SIGNED_URL=$(special_curl $API_ENDPOINT/api/client/servers/$SERVER_ID/files/upload -H "Authorization: Bearer $TOKEN" | jq -r ".attributes.url") + +if [ -z "$SIGNED_URL" ]; then + echo "!!! CANNOT FETCH SIGNED URL !!!" + exit +fi + +special_curl "$SIGNED_URL&directory=/mods" -X POST -F "files=@$UPLOAD" +echo "Uploaded!" +