Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding automated support for sonatype central #307

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions plugin/src/main/scala/com/geirsson/CiReleasePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ object CiReleasePlugin extends AutoPlugin {
publishArtifact.in(Test) := false,
publishMavenStyle := true,
commands += Command.command("ci-release") { currentState =>
val shouldDeployToSonatypeCentral = isDeploySetToSonatypeCentral(currentState)
val isSnapshot = isSnapshotVersion(currentState)
if (!isSecure) {
println("No access to secret variables, doing nothing")
currentState
Expand All @@ -145,27 +147,45 @@ object CiReleasePlugin extends AutoPlugin {
// https://github.com/olafurpg/sbt-ci-release/issues/64
val reloadKeyFiles =
"; set pgpSecretRing := pgpSecretRing.value; set pgpPublicRing := pgpPublicRing.value"
if (!isTag) {
if (isSnapshotVersion(currentState)) {
println(s"No tag push, publishing SNAPSHOT")

if (shouldDeployToSonatypeCentral) {
if (isSnapshot) {
println(s"Sonatype Central does not accept snapshots, only official releases. Aborting release.")
currentState
} else if (!isTag) {
println(s"No tag published. Cannot publish an official release without a tag and Sonatype Central does not accept snapshot releases. Aborting release.")
currentState
} else {
println("Tag push detected, publishing a stable release")
reloadKeyFiles ::
sys.env.getOrElse("CI_SNAPSHOT_RELEASE", "+publish") ::
sys.env.getOrElse("CI_CLEAN", "; clean ; sonatypeBundleClean") ::
sys.env.getOrElse("CI_RELEASE", "+publishSigned") ::
sys.env.getOrElse("CI_SONATYPE_RELEASE", "sonatypeCentralRelease") ::
eed3si9n marked this conversation as resolved.
Show resolved Hide resolved
currentState
} else {
// Happens when a tag is pushed right after merge causing the master branch
// job to pick up a non-SNAPSHOT version even if TRAVIS_TAG=false.
println(
"Snapshot releases must have -SNAPSHOT version number, doing nothing"
)
currentState
}
} else {
println("Tag push detected, publishing a stable release")
reloadKeyFiles ::
sys.env.getOrElse("CI_CLEAN", "; clean ; sonatypeBundleClean") ::
sys.env.getOrElse("CI_RELEASE", "+publishSigned") ::
sys.env.getOrElse("CI_SONATYPE_RELEASE", "sonatypeBundleRelease") ::
currentState
if (!isTag) {
if (isSnapshot) {
println(s"No tag push, publishing SNAPSHOT")
reloadKeyFiles ::
sys.env.getOrElse("CI_SNAPSHOT_RELEASE", "+publish") ::
currentState
} else {
// Happens when a tag is pushed right after merge causing the master branch
// job to pick up a non-SNAPSHOT version even if TRAVIS_TAG=false.
println(
"Snapshot releases must have -SNAPSHOT version number, doing nothing"
)
currentState
}
} else {
println("Tag push detected, publishing a stable release")
reloadKeyFiles ::
sys.env.getOrElse("CI_CLEAN", "; clean ; sonatypeBundleClean") ::
sys.env.getOrElse("CI_RELEASE", "+publishSigned") ::
sys.env.getOrElse("CI_SONATYPE_RELEASE", "sonatypeBundleRelease") ::
currentState
}
}
}
}
Expand All @@ -179,6 +199,15 @@ object CiReleasePlugin extends AutoPlugin {
publishTo := sonatypePublishToBundle.value
)

def isDeploySetToSonatypeCentral(state: State): Boolean = {
sonatypeCredentialHost.in(ThisBuild).get(Project.extract(state).structure.data) match {
case Some(value) if value == Sonatype.sonatypeCentralHost => {
true
}
case _ => false
}
}

def isSnapshotVersion(state: State): Boolean = {
version.in(ThisBuild).get(Project.extract(state).structure.data) match {
case Some(v) => v.endsWith("-SNAPSHOT")
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ Enjoy 👌

## FAQ

### How do I publish to Sonatype Central?

As of February 2024, Sonatype has released a new portal, called Sonatype Central. Users can configure their libraries to be published via this portal by adding the following to `build.sbt`:

```sbt
import xerial.sbt.Sonatype.sonatypeCentralHost

ThisBuild / sonatypeCredentialHost := sonatypeCentralHost
```

Users can generate a two-part token, containing username and password values, in their [account](https://central.sonatype.com/account) and then set these to the _SONATYPE_USERNAME_ and _SONATYPE_PASSWORD_ environment variables. All other steps should then work as documented.



### How do I disable publishing in certain projects?

Add the following to the project settings (works only in sbt 1)
Expand Down