12

I would like to use GitHub packages to store Maven artifacts for several repositories in a GitHub organization. Currently, it appears that for each project, a separate (Maven) repository configuration entry is required to point to that (GitHub) repository's Maven repository:

<repository>
  <id>github</id>
  <name>GitHub OWNER Apache Maven Packages</name>
  <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>

The corresponding configuration for the Maven project that would be published is:

<distributionManagement>
   <repository>
     <id>github</id>
     <name>GitHub OWNER Apache Maven Packages</name>
     <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
   </repository>
</distributionManagement>

Is there a way to configure the packages to all go to a single REPOSITORY? Setting the REPOSITORY to either a different existing or non-existing (GitHub) repository in the organization fails the build, as does removing the /REPOSITORY entirely

Ivan G.
  • 700
  • 8
  • 19
  • If you really want to have it available to the public i would recommend to go to central repository instead of github package https://central.sonatype.org/ – khmarbaise Jul 22 '20 at 19:33
  • What'st the content of your workflow? Have you created a PAT with read and write access to the organization's packages and are you using this in your workflow? – riQQ Jul 22 '20 at 19:46
  • @riQQ currently, we have GitHub actions that publish the artifacts to GitHub packages, and wish to consume them for local development as well as within GitHub packages. Have generated a PAT for local read access, using the built in token in Actions to publish – Ivan G. Jul 22 '20 at 20:04
  • The built in token only has access to the repository the workflow is running in. Try using a PAT with access to the organization's packages. – riQQ Jul 23 '20 at 08:24
  • @riQQ using a PAT I am able to _access_ the org's packages, but cannot publish with the PAT to any other REPOSITORY, or to the root (without the /REPOSITORY qualifier). I can however publish with the PAT to the current repository – Ivan G. Jul 23 '20 at 20:36
  • It looks like the Packages tab on an organization just aggregates the packages of the organization's repositories (see https://github.blog/2019-05-10-introducing-github-package-registry/) and there's nothing in the documentation hinting at "organization wide" packages. – riQQ Jul 24 '20 at 09:23
  • 1
    I have been trying this myself by uploading from another Maven Java project to upload to another Github repo. It does not work whether I use a Pat or Github token. – Katlock Jul 29 '20 at 17:00
  • 1
    @IvanG did you get it working in the end? – Jakub Zalas Feb 04 '21 at 21:48

3 Answers3

6

For people who are just looking to avoid specifying the <repository> tag multiple times:

Apparently, GitHub DOES NOT care about what repository you set when pulling packages from the Registry. When pulling, the package index is based on the organization level, not the repository level.

You can just do this if you want to pull packages from multiple repositories that are within the same organization:

<repository>
  <id>github</id>
  <name>GitHub OWNER Apache Maven Packages</name>
  <url>https://maven.pkg.github.com/Your-Orgnazation/JUST-ENTER-ANYTHING-HERE</url>
</repository>

NOTE: You still need the Personal Token to pull the packages across repositories. You just don't have to specify the <repository> tag multiple times.

Sola
  • 131
  • 1
  • 4
  • >JUST-ENTER-ANYTHING-HERE indeed, thanks, just make sure that you (or that user/bot whose token you use for build) have access to the package you are trying to pull – Serhii Povísenko Nov 22 '22 at 16:52
4

Personal Access Token

secrets.GITHUB_TOKEN is defined by default but it is only sufficient to deploy to the current repository.

To make it work across repositories you'll need to define a new Personal Access Token in:

Select write:packages for the scope and all the repo scopes should be automatically selected for you.

enter image description here

Repository / Organisation secrets

Next, define a secret in your organisation or each of the repositories you need to publish packages from.

Give it a name (i.e. DEPLOY_GITHUB_TOKEN) and set its value to the Personal Access Token created in the previous step.

enter image description here

Repository secrets are defined in repository Settings > Secrets. There's a similar section for the organisation.

GitHub Action

Finally, make sure you pass your Personal Access Token to the deployment step as an environment variable called GITHUB_TOKEN.

In the example below, it's set to the value of the DEPLOY_GITHUB_TOKEN secret defined in the previous step.

name: Build

on:
  release:
    types: [created]
jobs:
  build:
    name: Build & Deploy
    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: Build with Maven
        run: mvn --batch-mode --update-snapshots install

      - name: Deploy to GitHub
        run: mvn --batch-mode -DskipTests -DuseGitHubPackages=true deploy
        env:
          GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}

Since I used a dedicated Maven profile for the GitHub package repository distribution management, I also activated it with -DuseGitHubPackages=true.

Maven profile

In the profile example below, I configured distribution management to use the external/shared repository vlingo/vlingo-platform just like suggested in @Danny Varod's answer.

<!-- pom.xml -->
<project>
  <!-- ... -->

  <profiles>
    <profile>
      <id>github</id>
      <activation>
        <property>
          <name>useGitHubPackages</name>
          <value>true</value>
        </property>
      </activation>
      <distributionManagement>
        <repository>
          <id>github</id>
          <name>GitHub Packages</name>
          <url>https://maven.pkg.github.com/vlingo/vlingo-platform</url>
        </repository>
      </distributionManagement>
    </profile>
  </profiles>
</project>

Cross posted from: https://dev.to/jakub_zalas/how-to-publish-maven-packages-to-a-single-github-repository-3lkc

A working example can be found in vlingo repositories: https://github.com/vlingo/vlingo-platform/packages

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • Hi @Jakub Zalas, I followed the same approach as mentioned in that post. But I'm getting this error -> ```Error: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project demoproject: Failed to deploy artifacts: Could not find artifact com.example:demoproject:jar:3.0.1 in github (https://maven.pkg.github.com/******/test-platform) -> [Help 1]```. Could you please help with this? Thank you. – Mani May 09 '22 at 15:58
  • I also added the Personal Access Token to the secrets of repo with the same scope as you mentioned. – Mani May 10 '22 at 15:40
1

I solved this a while back by creating an empty repository named maven-packages or something like that and publishing all the Maven packages from all the organization's repositories to this one repository.

This enabled configuring a single Maven repository (in addition to Maven-central) locally, on build machine and in deployment environment for pulling Maven-library dependencies.

I don't have this code (was at a previous workplace and now I use Python), however, I recall using the mvn deploy command line for this as documented here:

mvn deploy:deploy-file -DpomFile=<path-to-pom> \
  -Dfile=<path-to-file> \
  -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
  -Durl=<url-of-the-repository-to-deploy>

and I modified the pom.xml during the build to insert the correct deploy repository's details, so the publishing could only be done via the build (and to add pre-release details to the pre-releases' version number). This also enabled adding an additional repository for external stable releases (to be published to Maven-central).

See configuration here.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • This doesn't seem to work. Deployment works fine within a single repository, but as soon as I change the url to an external one (not the current repo), all I get is "422 Unprocessable Entity". Even if the personal access token was set up with all the repo and package permissions. – Jakub Zalas Feb 04 '21 at 21:50
  • 1
    I last used this about a year ago and don't have access to the workflow. Did you try the configuration answer link I added? Github also has good support. – Danny Varod Feb 04 '21 at 23:59
  • In the end, I deleted the repository, created it again, and everything started to work... Seems that my initial attempts messed something up and it wasn't possible to resolve without re-creating the repo. – Jakub Zalas Feb 05 '21 at 08:58
  • @JakubZalas I did not get this working yet. Can you share your solution as a separate answer if it is significantly different than Danny's, or otherwise explain what needed to happen for it to work? – Ivan G. Feb 05 '21 at 16:01
  • @IvanG. The solution is very similar. For now, I've got it on a fork of the project I want to implement it for. Packages are published to https://github.com/jakzal/vlingo-platform/packages. One of the commits that introduced github publishing: https://github.com/jakzal/vlingo-actors/commit/14978fc71b6da7f8c4fa5666d60e36dc7d483d9a – Jakub Zalas Feb 05 '21 at 16:42
  • @JakubZalas did you define the `env: GITHUB_TOKEN: ${{ github.token }}`? – Danny Varod Feb 05 '21 at 17:29
  • 1
    @DannyVarod the default token will only work if you need to publish to the current repo. If you want to publish to a different one you'll need to define a new user access token. I called it `DEPLOY_GITHUB_TOKEN`, defined as a secret, and passed to the step: `env: GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}`. – Jakub Zalas Feb 05 '21 at 18:11
  • 1
    @JakubZalas either add this as an answer or edit mine. – Danny Varod Feb 07 '21 at 10:34