Skip to content

Commit 20b31fd

Browse files
committed
Set up publishing of SNAPSHOT builds to GitHub Packages
* Added workflow for publishing `SNAPSHOT` builds. * Added workflow for removing `SNAPSHOT` builds once a pull request has been merged.
1 parent a7bc483 commit 20b31fd

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

.github/workflows/build.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,57 @@ jobs:
4646
4747
- name: Build and test
4848
run: ./gradlew build --stacktrace
49+
50+
publish-snapshot:
51+
name: Publish snapshot to GitHub Packages
52+
runs-on: ubuntu-latest
53+
needs: build
54+
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
55+
permissions:
56+
contents: read
57+
packages: write
58+
59+
steps:
60+
- name: Checkout
61+
uses: actions/checkout@v3
62+
63+
- name: Get base version
64+
id: get-version
65+
run: |
66+
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
67+
# Strip any existing -SNAPSHOT suffix so we can re-apply it cleanly
68+
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
69+
echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
70+
71+
- name: Determine snapshot version
72+
id: snapshot-version
73+
run: |
74+
if [ "${{ github.event_name }}" == "pull_request" ]; then
75+
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
76+
else
77+
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-SNAPSHOT"
78+
fi
79+
echo "snapshot_version=${SNAPSHOT_VERSION}" >> $GITHUB_OUTPUT
80+
echo "Publishing snapshot version: ${SNAPSHOT_VERSION}"
81+
82+
- name: Setup JDK
83+
uses: actions/setup-java@v3
84+
with:
85+
java-version: 8
86+
distribution: 'temurin'
87+
88+
- name: Setup Gradle
89+
uses: gradle/gradle-build-action@v2
90+
with:
91+
gradle-version: wrapper
92+
gradle-home-cache-cleanup: true
93+
gradle-home-cache-includes: |
94+
caches
95+
notifications
96+
jdks
97+
98+
- name: Publish snapshot to GitHub Packages
99+
env:
100+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
101+
GITHUB_ACTOR: ${{ github.actor }}
102+
run: ./gradlew publishReleasePublicationToGitHubPackagesRepository -Pversion=${{ steps.snapshot-version.outputs.snapshot_version }} --stacktrace
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Clean up PR snapshot
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup-snapshot:
9+
name: Delete PR snapshot from GitHub Packages
10+
runs-on: ubuntu-latest
11+
# Only run when the PR was actually merged, not just closed.
12+
if: github.event.pull_request.merged == true
13+
permissions:
14+
contents: read
15+
packages: write
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v3
20+
21+
- name: Get base version
22+
id: get-version
23+
run: |
24+
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
25+
# Strip any existing -SNAPSHOT suffix so we can re-apply it cleanly
26+
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
27+
echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
28+
29+
- name: Delete PR snapshot from GitHub Packages
30+
env:
31+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
run: |
33+
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
34+
echo "Looking for package version: ${SNAPSHOT_VERSION}"
35+
36+
# Retrieve the numeric ID of any published version matching this snapshot.
37+
# --paginate ensures all pages are scanned; --jq filters on each page.
38+
VERSION_IDS=$(gh api \
39+
--paginate \
40+
-H "Accept: application/vnd.github+json" \
41+
"/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare/versions" \
42+
--jq ".[] | select(.name == \"${SNAPSHOT_VERSION}\") | .id" 2>/dev/null || true)
43+
44+
if [ -z "$VERSION_IDS" ]; then
45+
echo "No published version found for ${SNAPSHOT_VERSION} — nothing to delete."
46+
exit 0
47+
fi
48+
49+
while IFS= read -r VERSION_ID; do
50+
echo "Deleting version ID ${VERSION_ID} (${SNAPSHOT_VERSION}) …"
51+
gh api --method DELETE \
52+
-H "Accept: application/vnd.github+json" \
53+
"/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare/versions/${VERSION_ID}"
54+
echo "Deleted version ID ${VERSION_ID}."
55+
done <<< "$VERSION_IDS"
56+
echo "Done cleaning up ${SNAPSHOT_VERSION}."

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,54 @@ If you are using the kotlin style `build.gradle.kts`:
5252
implementation("com.uploadcare:uploadcare:3.5.1")
5353
```
5454

55+
## Snapshot Builds (Pre-release)
56+
57+
Snapshot builds are published to [GitHub Packages](https://github.com/uploadcare/uploadcare-java/packages) automatically:
58+
59+
- **From pull requests**: version format `{version}-PR-{pr-number}-SNAPSHOT` (e.g. `3.5.3-PR-42-SNAPSHOT`)
60+
- **From the `master` branch**: version format `{version}-SNAPSHOT` (e.g. `3.5.3-SNAPSHOT`)
61+
62+
### Maven (snapshot)
63+
64+
Add to your `pom.xml`:
65+
66+
```xml
67+
<repositories>
68+
<repository>
69+
<id>uploadcare-snapshots</id>
70+
<url>https://maven.pkg.github.com/uploadcare/uploadcare-java</url>
71+
<snapshots>
72+
<enabled>true</enabled>
73+
</snapshots>
74+
</repository>
75+
</repositories>
76+
77+
<dependencies>
78+
<dependency>
79+
<groupId>com.uploadcare</groupId>
80+
<artifactId>uploadcare</artifactId>
81+
<version>3.5.3-SNAPSHOT</version>
82+
</dependency>
83+
</dependencies>
84+
```
85+
86+
### Gradle (snapshot)
87+
88+
Add to your `build.gradle` (or `build.gradle.kts`):
89+
90+
```groovy
91+
repositories {
92+
maven {
93+
name = "GitHubPackages"
94+
url = uri("https://maven.pkg.github.com/uploadcare/uploadcare-java")
95+
}
96+
}
97+
98+
dependencies {
99+
implementation("com.uploadcare:uploadcare:3.5.3-SNAPSHOT")
100+
}
101+
```
102+
55103
## Examples
56104

57105
Get your [API keys](https://uploadcare.com/docs/start/settings/#keys) to proceed with the examples below.

build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,26 @@ dependencies {
3131

3232
// Setup global publishing repository settings.
3333
signing {
34+
setRequired({ isReleaseVersion })
3435
useGpgCmd()
3536
sign(publishing.publications)
3637
}
3738

39+
// Skip signing entirely for non-release (snapshot) builds.
40+
tasks.withType<Sign>().configureEach {
41+
onlyIf { isReleaseVersion }
42+
}
43+
3844
publishing {
3945
repositories {
46+
maven {
47+
name = "GitHubPackages"
48+
url = uri("https://maven.pkg.github.com/uploadcare/uploadcare-java")
49+
credentials {
50+
username = System.getenv("GITHUB_ACTOR") ?: ""
51+
password = System.getenv("GITHUB_TOKEN") ?: ""
52+
}
53+
}
4054
maven {
4155
// Dynamically select either Maven Central or na Internal repository depending on the value of uploadcare.publish.type / UPLOADCARE_PUBLISH_TYPE
4256
name = "selected"

0 commit comments

Comments
 (0)