Skip to content

Commit

Permalink
Removed allRequirements on publishTrigger (#8)
Browse files Browse the repository at this point in the history
* Removed allRequirements on publishTrigger

* Updated gitlabDomain to utilize gitlab CI env var

* added some documentation and updated .gitignore

* re-enabled autoTrigger and scoped plugin settings to the publish command only

* Minor refacotring to make change more readable
  • Loading branch information
listba authored Sep 7, 2020
1 parent 78030c9 commit d9ffb55
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
target/
project/project/

# Metals / bloop
.metals/
.bloop/
project/metals.sbt


# Intellij
.idea/

#vscode
.vscode/

40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,51 @@
Gitlab dependency resolution and artifact publishing for sbt

```scala
addSbtPlugin("com.gilcloud" % "sbt-gitlab" % "0.0.4")
addSbtPlugin("com.gilcloud" % "sbt-gitlab" % "0.0.6") // in your project/plugins.sbt file
```


## Usage

This plugin requires sbt 1.0.0+

### Publishing to Gitlab via Gitlab CI/CD

Utilizing the sbt publish command within GitLab CI/CD should require no additional configuration. This plugin automatically pulls the following GitLab environment variables which should always be provided by default within GitLab Pipelines

```shell
$CI_JOB_TOKEN # Access Token to authorize read/writes to the gitlab pakcage registry
$CI_PROJECT_ID # Project ID for active project pipeline. Used so Gitlab knows what project to publish the artifact under
$CI_GROUP_ID # Gitlab Groupt ID. Used for fetching Artifacts published under the specified Group.
# In a pipeline this would be set to the id of the group the project is under (when applicable)
$CI_SERVER_HOST # The host name for gitlab defaults to gitlab.com
```

Any of these 'defaults' can be overwritten in your build.sbt file

```scala
import com.gilcloud.sbt.gitlab.{GitlabCredentials,GitlabPlugin}

GitlabPlugin.autoImport.gitlabGroupId := Some(12345)
GitlabPlugin.autoImport.gitlabProjectId := Some(12345)
GitlabPlugin.autoImport.gitlabDomain := "my-gitlab-host.com"
GitlabPlugin.autoImport.GitlabCredentials := Some(GitlabCredentials("Private-Token","<API-KEY>"))

// Alternatively for credential managment
// ideal for pulling artifacts locally and keeping tokens out of your source control
// see below for sample .credentials file
credentials += Credentials(Path.userHome / ".sbt" / ".credentials.gitlab"),

```
> ~/.sbt/.credentials.gitlab
```.credentials
realm=gitlab
host=my-git-lab-host
user=Private-Token
password=<API-KEY>
```

In order to publish to
### Testing

Run `test` for regular unit tests.
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sbt-gitlab"
organization := "com.gilcloud"
version := "0.0.5"
version := "0.0.6"
description := "publishing and dependency resolution for gitlab both private and hosted using header auth"
sbtPlugin := true

Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import org.apache.ivy.util.url.{
import sbt.Keys._
import sbt.internal.CustomHttp
import sbt.internal.librarymanagement.ivyint.GigahorseUrlHandler
import sbt.{Credentials, Def, _}
import sbt._

import scala.util.Try
object GitlabPlugin extends AutoPlugin {


lazy val headerAuthHandler =
taskKey[Unit]("perform auth using header credentials")

// This plugin will load automatically
override def trigger: PluginTrigger = allRequirements

object autoImport {

Expand All @@ -32,13 +35,6 @@ object GitlabPlugin extends AutoPlugin {
}
import autoImport._

override def trigger: PluginTrigger = allRequirements

override def globalSettings =
Seq(
gitlabDomain := "gitlab.com"
)

def headerEnrichingClientBuilder(
existingBuilder: OkHttpClient.Builder,
domain: String,
Expand All @@ -65,10 +61,14 @@ object GitlabPlugin extends AutoPlugin {
downloader: URLHandler
): Unit = {}
}

override def projectSettings: Seq[Def.Setting[_]] =
inScope(publish.scopedKey.scope)(gitLabProjectSettings)

val gitLabProjectSettings : Seq[Def.Setting[_]] =
Seq(
publishMavenStyle := true,
gitlabDomain := sys.env.getOrElse("CI_SERVER_HOST", "gitlab.com"),
gitlabProjectId := sys.env
.get("CI_PROJECT_ID")
.flatMap(str => Try(str.toInt).toOption),
Expand Down

0 comments on commit d9ffb55

Please sign in to comment.