11name : Build and test
22on :
3- # Build PRs and branches.
4- pull_request_target :
5- types : [ opened, synchronize, reopened ]
3+ pull_request :
4+ types : [opened, synchronize, reopened, closed]
65 paths-ignore :
76 - .github/workflows/deploy-tagged.yml
7+
88 push :
99 branches :
10- - ' ** '
10+ - " ** "
1111 tags-ignore :
12- - ' ** '
12+ - " ** "
1313 paths-ignore :
1414 - .github/workflows/deploy-tagged.yml
1515
@@ -19,41 +19,49 @@ jobs:
1919 runs-on : ubuntu-latest
2020 strategy :
2121 matrix :
22- # https://en.wikipedia.org/wiki/Java_version_history
23- java : [ '8', '11' ] # LTS
22+ java : ["8", "11"]
2423
2524 steps :
2625 - name : Checkout
2726 uses : actions/checkout@v6
2827 with :
29- ref : ${{ github.event.pull_request.head.sha }}
28+ # For pull_request, github.sha is a merge commit; we want the PR head SHA.
29+ # For push, github.sha is correct.
30+ ref : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
31+
3032 - name : Setup JDK
3133 uses : actions/setup-java@v5
3234 with :
3335 java-version : ${{ matrix.java }}
34- distribution : ' temurin'
36+ distribution : temurin
3537
3638 - name : Setup Gradle
3739 uses : gradle/gradle-build-action@v2
3840 with :
39- # The Gradle wrapper's version (already the default, putting it here to clarity)
4041 gradle-version : wrapper
41- # Removing unused files from Gradle User Home before saving to cache (i.e. older versions of gradle)
4242 gradle-home-cache-cleanup : true
43- # Cache downloaded JDKs in addition to the default directories.
4443 gradle-home-cache-includes : |
4544 caches
4645 notifications
4746 jdks
4847
4948 - name : Build and test
49+ env :
50+ UPLOADCARE_PUBLIC_KEY : ${{ secrets.UPLOADCARE_PUBLIC_KEY }}
51+ UPLOADCARE_SECRET_KEY : ${{ secrets.UPLOADCARE_SECRET_KEY }}
5052 run : ./gradlew build --stacktrace
5153
52- publish-snapshot :
53- name : Publish snapshot to GitHub Packages
54+ publish-pr- snapshot :
55+ name : Publish PR snapshot to GitHub Packages
5456 runs-on : ubuntu-latest
5557 needs : build
56- if : github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
58+
59+ # Only publish for PRs from the same repo (forks won't have permissions to publish packages).
60+ if : >
61+ github.event_name == 'pull_request' &&
62+ github.event.action != 'closed' &&
63+ github.event.pull_request.head.repo.full_name == github.repository
64+
5765 permissions :
5866 contents : read
5967 packages : write
@@ -63,30 +71,26 @@ jobs:
6371 uses : actions/checkout@v6
6472 with :
6573 ref : ${{ github.event.pull_request.head.sha }}
74+
6675 - name : Get base version
6776 id : get-version
6877 run : |
6978 BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
70- # Strip any existing -SNAPSHOT suffix so we can re-apply it cleanly
7179 BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
7280 echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
7381
74- - name : Determine snapshot version
82+ - name : Determine PR snapshot version
7583 id : snapshot-version
7684 run : |
77- if [ "${{ github.event_name }}" == "pull_request_target" ]; then
78- SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
79- else
80- SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-SNAPSHOT"
81- fi
85+ SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
8286 echo "snapshot_version=${SNAPSHOT_VERSION}" >> $GITHUB_OUTPUT
83- echo "Publishing snapshot version: ${SNAPSHOT_VERSION}"
87+ echo "Publishing PR snapshot version: ${SNAPSHOT_VERSION}"
8488
8589 - name : Setup JDK
8690 uses : actions/setup-java@v5
8791 with :
88- java-version : 8
89- distribution : ' temurin'
92+ java-version : " 8 "
93+ distribution : temurin
9094
9195 - name : Setup Gradle
9296 uses : gradle/gradle-build-action@v2
@@ -98,8 +102,122 @@ jobs:
98102 notifications
99103 jdks
100104
101- - name : Publish snapshot to GitHub Packages
105+ - name : Publish PR snapshot to GitHub Packages
102106 env :
103107 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
104108 GITHUB_ACTOR : ${{ github.actor }}
105109 run : ./gradlew publishReleasePublicationToGitHubPackagesRepository -Pversion=${{ steps.snapshot-version.outputs.snapshot_version }} --stacktrace
110+
111+ publish-master-snapshot :
112+ name : Publish master snapshot to GitHub Packages
113+ runs-on : ubuntu-latest
114+ needs : build
115+
116+ # Publish on direct pushes to master (including merge commits).
117+ if : github.event_name == 'push' && github.ref == 'refs/heads/master'
118+
119+ permissions :
120+ contents : read
121+ packages : write
122+
123+ steps :
124+ - name : Checkout
125+ uses : actions/checkout@v6
126+
127+ - name : Get base version
128+ id : get-version
129+ run : |
130+ BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
131+ BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
132+ echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
133+
134+ - name : Determine master snapshot version
135+ id : snapshot-version
136+ run : |
137+ SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-SNAPSHOT"
138+ echo "snapshot_version=${SNAPSHOT_VERSION}" >> $GITHUB_OUTPUT
139+ echo "Publishing master snapshot version: ${SNAPSHOT_VERSION}"
140+
141+ - name : Setup JDK
142+ uses : actions/setup-java@v5
143+ with :
144+ java-version : " 8"
145+ distribution : temurin
146+
147+ - name : Setup Gradle
148+ uses : gradle/gradle-build-action@v2
149+ with :
150+ gradle-version : wrapper
151+ gradle-home-cache-cleanup : true
152+ gradle-home-cache-includes : |
153+ caches
154+ notifications
155+ jdks
156+
157+ - name : Publish master snapshot to GitHub Packages
158+ env :
159+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
160+ GITHUB_ACTOR : ${{ github.actor }}
161+ run : ./gradlew publishReleasePublicationToGitHubPackagesRepository -Pversion=${{ steps.snapshot-version.outputs.snapshot_version }} --stacktrace
162+
163+ cleanup-merged-pr-snapshot :
164+ name : Delete merged PR snapshot from GitHub Packages
165+ runs-on : ubuntu-latest
166+
167+ # Triggered by pull_request.closed (merge) into master
168+ if : >
169+ github.event_name == 'pull_request' &&
170+ github.event.action == 'closed' &&
171+ github.event.pull_request.merged == true &&
172+ github.event.pull_request.base.ref == 'master'
173+
174+ permissions :
175+ packages : write
176+
177+ steps :
178+ - name : Compute PR snapshot version
179+ id : prver
180+ run : |
181+ # Must match the same scheme used in publish-pr-snapshot
182+ # We need base version too:
183+ BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
184+ BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
185+ echo "version=${BASE_VERSION}-PR-${{ github.event.pull_request.number }}-SNAPSHOT" >> $GITHUB_OUTPUT
186+
187+ - name : Delete package version via GitHub API
188+ uses : actions/github-script@v8
189+ with :
190+ github-token : ${{ secrets.GITHUB_TOKEN }}
191+ script : |
192+ const owner = context.repo.owner;
193+ const repo = context.repo.repo;
194+
195+ const package_type = "maven";
196+
197+ // TODO: IMPORTANT:
198+ // Set this to the exact "Package name" as shown in the repo's Packages UI.
199+ // For Maven packages this is often the artifactId, but verify.
200+ const package_name = "YOUR_PACKAGE_NAME_HERE";
201+
202+ const versionName = "${{ steps.prver.outputs.version }}";
203+
204+ const versions = await github.paginate(
205+ github.rest.packages.getAllPackageVersionsForPackageOwnedByRepo,
206+ { owner, repo, package_type, package_name, per_page: 100 }
207+ );
208+
209+ const match = versions.find(v => v.name === versionName);
210+ if (!match) {
211+ core.warning(`No package version found named "${versionName}" in ${package_type}/${package_name}. Nothing to delete.`);
212+ return;
213+ }
214+
215+ await github.rest.packages.deletePackageVersionForRepoOwnedByOwner({
216+ owner,
217+ repo,
218+ package_type,
219+ package_name,
220+ package_version_id: match.id,
221+ });
222+
223+ core.info(`Deleted ${package_type}/${package_name}@${versionName} (id=${match.id}).`);
0 commit comments