diff --git a/.github/workflows/modrinth-publish.yml b/.github/workflows/modrinth-publish.yml index 5bf7caef..878d4195 100644 --- a/.github/workflows/modrinth-publish.yml +++ b/.github/workflows/modrinth-publish.yml @@ -16,12 +16,50 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Checkout main branch from GitHub uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Restore last workflow commit from cache + id: cache-last-commit + uses: actions/cache@v4 + with: + path: .last_workflow_commit + key: last-workflow-commit + + - name: Get last workflow run commit + id: last_run + run: | + if [ -f .last_workflow_commit ]; then + LAST_COMMIT=$(cat .last_workflow_commit) + else + LAST_COMMIT=$(git rev-list --max-parents=0 HEAD) + fi + echo "LAST_COMMIT=$LAST_COMMIT" >> $GITHUB_ENV + + - name: Count commits since last run + id: count_commits + run: | + COMMITS=$(git rev-list $LAST_COMMIT..HEAD --count) + echo "commits_count=$COMMITS" >> $GITHUB_ENV + echo "Number of commits since last run: $COMMITS" + + - name: Use environment variables + run: | + echo "Last commit: $LAST_COMMIT" + echo "Number of commits: $commits_count" + + - name: Store current commit for next run + run: | + git rev-parse HEAD > .last_workflow_commit + echo "Storing current commit: $(cat .last_workflow_commit)" + # Runs a single command using the runners shell - name: Setting up JDK 21 uses: actions/setup-java@v4 with: distribution: 'temurin' java-version: 21 + - name: Setup Gradle uses: gradle/actions/setup-gradle@v4 @@ -30,4 +68,10 @@ jobs: MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }} run: | chmod +x gradlew - ./gradlew :modrinth \ No newline at end of file + ./gradlew :modrinth + + - name: Store current commit for next run + uses: actions/cache@v4 + with: + path: .last_workflow_commit + key: last-workflow-commit \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 9f6c044d..f66fb08b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -142,15 +142,16 @@ modrinth { } fun getChangeLog(): String { - val currentBranch = grgit.branch.current().name - val remoteBranch = "origin/$currentBranch" + val lastCommitEnv = System.getenv("LAST_COMMIT") + ?: return "The environment variable \$LAST_COMMIT is not defined." - val lastRemoteCommit = grgit.log { - range(remoteBranch, currentBranch) - }.lastOrNull()?.id ?: return "No se encontraron cambios desde el remoto." + val commitExists = grgit.log().any { it.id == lastCommitEnv } + if (!commitExists) { + return "The commit specified in \$LAST_COMMIT does not exist in this repository." + } val changeLog = grgit.log { - range(lastRemoteCommit, currentBranch) + range(lastCommitEnv, "HEAD") } return if (changeLog.isNotEmpty()) { @@ -158,9 +159,10 @@ fun getChangeLog(): String { "- ${commit.shortMessage} (${commit.id})" } } else { - "No changes found." + "There are no changes since the commit specified in \$LAST_COMMIT." } } + tasks.modrinth { dependsOn(tasks.shadowJar) dependsOn(tasks.modrinthSyncBody)