Skip to content

Commit

Permalink
Updated readme for the latest changes/testing and also improved the w…
Browse files Browse the repository at this point in the history
…arning process slightly
  • Loading branch information
djspiewak committed Dec 8, 2019
1 parent 6e91eb2 commit e831e32
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# sbt-github-packages [![Build Status](https://travis-ci.com/djspiewak/sbt-github-packages.svg?branch=master)](https://travis-ci.com/djspiewak/sbt-github-packages)

Configures your project for publication to the [GitHub Package Registry](https://help.github.com/en/articles/about-github-package-registry) using its Apache Maven support. Note that you probably shouldn't use this with plugins, only libraries. Probably won't delete your source code, but no promises. Also provides some convenience functionality for *depending* upon artifacts which have been published to the Package Registry. **Has not yet been tested with private repos.**
Configures your project for publication to the [GitHub Package Registry](https://help.github.com/en/articles/about-github-package-registry) using its Apache Maven support. Note that you probably shouldn't use this with plugins, only libraries. Probably won't delete your source code, but no promises. Also provides some convenience functionality for *depending* upon artifacts which have been published to the Package Registry.

## Usage

Expand All @@ -12,6 +12,22 @@ addSbtPlugin("com.codecommit" % "sbt-github-packages" % "<version>")

Published for sbt 1.x. No dependencies.

In order to *publish* artifacts via this plugin, you will need to override the `githubOwner` and `githubRepository` setting keys to the relevant values for your project. For example:

```sbt
ThisBuild / githubOwner := "djspiewak"
ThisBuild / githubRepository := "sbt-github-packages"
```

The `ThisBuild` scoping is significant. In the event that you do *not* override these values, you will see a warning like the following:

```
[warn] undefined keys `ThisBuild / githubOwner` and `ThisBuild / githubRepository`
[warn] retaining pre-existing publication settings
```

The reason this functionality is disabled by default is you may wish to utilize the `Resolver` syntax (described below) *without* overriding your default publication mechanism. In general, I wouldn't necessarily recommend this, since it means that your publication will not be self-contained within a single artifact realm, but that's a relatively common problem anyway these days so it's probably not a big deal.

### Resolvers

If you're consuming packages that were published in the GitHub Package Registry, this plugin defines some convenience syntax for adding resolvers:
Expand All @@ -22,12 +38,29 @@ resolvers += Resolver.githubPackagesRepo("OWNER", "REPOSITORY")

This works for both public and private repositories, though if you use it with a private repository, you will also need to ensure that either `credentials` or `githubTokenSource` are appropriately configured.

#### Multiple Resolvers

A not-unusual requirement may be to support resolution from several different repositories. For example:

```sbt
resolvers += Resolver.githubPackagesRepo("djspiewak", "sbt-github-packages")
resolvers += Resolver.githubPackagesRepo("djspiewak", "shims")
```

Unfortunately, this will yield the following warning in sbt:

```
[warn] Multiple resolvers having different access mechanism configured with same name 'GitHub Maven Packages'. To avoid conflict, Remove duplicate project resolvers (`resolvers`) or rename publishing resolver (`publishTo`).
```

There isn't *really* a good workaround to this right now, and it's also not clear exactly how badly it degrades functionality. Consider this a work-in-progress. The problem stems from the fact that the credentials handling can only set a single realm: `GitHub Maven Packages`. In theory, the solution to this would be if sbt gave us the ability to set credentials which apply to *any* realm with a given host, but that doesn't appear to be possible at the current time.

### Keys

The following setting keys are defined:

- `githubOwner : String` (*required*) Defines the organization or user name that owns the package registry to which this project will be published
- `githubRepository : String` (*required*) The repository which hosts this project under the organization/user defined in the other setting
- `githubOwner : String` Defines the organization or user name that owns the package registry to which this project will be published
- `githubRepository : String` The repository which hosts this project under the organization/user defined in the other setting
- `githubUser : String` (*defaults to `git config github.user`*) *Your* GitHub username. This should almost never be specified in the build itself, but rather read from some external source. By default, it will read from the `git config` (by shelling out to the `git` command), but it's easy to override this to use an environment variable (e.g. `githubUser := sys.env("GITHUB_USER")`). To be extremely clear, this is the user who ran the `sbt` command, it is not *necessarily* the repository owner!
- `githubTokenSource : Option[TokenSource]` (*defaults to `None`*) Where the plugin should go to read the GitHub API token to use in authentication. `TokenSource` has two possible values: `Environment(variable: String)` and `GitConfig(key: String)`. This is mostly just a convenience. You're free to do whatever you want. Just don't, like, put it in your build.

Expand All @@ -47,7 +80,7 @@ credentials +=
For the love of all that is holy, do not put this into your **build.sbt**. The `token` is a password. Treat it as such. Best practice is generally to use an encrypted environment variable in your CI and a necessarily unencrypted environment variable locally. For example:

```sbt
githubTokenSource := Some(TokenSource.Environment("GITHUB_TOKEN"))
ThisBuild / githubTokenSource := Some(TokenSource.Environment("GITHUB_TOKEN"))
```

Then, if using Travis, you should be able to do something like:
Expand Down
13 changes: 11 additions & 2 deletions src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import scala.sys.process._
import scala.util.control.NonFatal

object GitHubPackagesPlugin extends AutoPlugin {
@volatile
private[this] var alreadyWarned = false

override def requires = plugins.JvmPlugin
override def trigger = allRequirements
Expand Down Expand Up @@ -61,14 +63,21 @@ object GitHubPackagesPlugin extends AutoPlugin {
},

publishTo := {
val log = streams.value.log
val back = for {
owner <- githubOwner.?.value
repo <- githubRepository.?.value
} yield "GitHub Package Registry" at s"https://maven.pkg.github.com/$owner/$repo"

back orElse {
streams.value.log.info("undefined keys `ThisBuild / githubOwner` and `ThisBuild / githubRepository`")
streams.value.log.info("retaining pre-existing publishTo settings")
GitHubPackagesPlugin synchronized {
if (!alreadyWarned) {
log.warn("undefined keys `ThisBuild / githubOwner` and `ThisBuild / githubRepository`")
log.warn("retaining pre-existing publication settings")
alreadyWarned = true
}
}

publishTo.value
}
},
Expand Down

0 comments on commit e831e32

Please sign in to comment.