diff --git a/.gitignore b/.gitignore index 5209403..bd9e393 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,15 @@ target/ +project/project/ # Metals / bloop .metals/ .bloop/ +project/metals.sbt + # Intellij .idea/ +#vscode +.vscode/ + diff --git a/README.md b/README.md index 92afb07..5a91dff 100644 --- a/README.md +++ b/README.md @@ -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","")) + +// 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= +``` + +In order to publish to ### Testing Run `test` for regular unit tests. diff --git a/build.sbt b/build.sbt index d9cce3a..a6a3399 100644 --- a/build.sbt +++ b/build.sbt @@ -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 diff --git a/src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala b/src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala index c7070fb..8a164e0 100644 --- a/src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala +++ b/src/main/scala/com/gilcloud/sbt/gitlab/GitlabPlugin.scala @@ -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 { @@ -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, @@ -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),