Skip to content

Commit

Permalink
Merge pull request #307 from Andrapyre/adding-sonatype-central-support
Browse files Browse the repository at this point in the history
feat: adding automated support for sonatype central
  • Loading branch information
eed3si9n authored Sep 20, 2024
2 parents 0037074 + a4b4e59 commit 0447c5a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
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") ::
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

0 comments on commit 0447c5a

Please sign in to comment.