Skip to content

Instantly share code, notes, and snippets.

@davideicardi
Created September 12, 2020 21:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davideicardi/89b6d0f816a2dc61ba1c3f5a75268a92 to your computer and use it in GitHub Desktop.
Save davideicardi/89b6d0f816a2dc61ba1c3f5a75268a92 to your computer and use it in GitHub Desktop.
Publish Maven library to Github packages with SBT and github actions

Publish

Add the following configuration to your build.sbt:

  // publish to github packages settings
  publishTo := Some("GitHub <GITHUB_OWNER> Apache Maven Packages" at "https://maven.pkg.github.com/<GITHUB_OWNER>/<GITHUB_PROJECT>"),
  publishMavenStyle := true,
  credentials += Credentials(
    "GitHub Package Registry",
    "maven.pkg.github.com",
    "<GITHUB_OWNER>",
    System.getenv("GITHUB_TOKEN")
  ),

Set the GITHUB_TOKEN environment variable to a valid github personal access token with write package permissions.

Create a workflow inside .github/workflows/publish.yml

name: Publish package to GitHub Packages
on:
  release:
    types: [created]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Publish package
      run: sbt test publish
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Usage

Set the GITHUB_TOKEN environment variable to a valid github personal access token.

externalResolvers += "GitHub davideicardi Apache Maven Packages" at "https://maven.pkg.github.com/davideicardi/kaa"
libraryDependencies += "com.davideicardi" %% "kaa" % "<version>"
credentials += Credentials(
    "GitHub Package Registry",
    "maven.pkg.github.com",
    "<GITHUB_OWNER>",
    System.getenv("GITHUB_TOKEN")
  )
@alexdobry
Copy link

thanks for your instructions. how and where should I store the environment variable to make it work with System.getenv?

@davideicardi
Copy link
Author

@alexdobry In general you should set any env variable inside the Github Secrets for your specific repository. But in this specific case I think that the default GITHUB_TOKEN should be fine. See https://docs.github.com/en/actions/security-guides/automatic-token-authentication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment