GitHub packages: Single Maven repository for GitHub organization

Viewed 1310

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

3 Answers

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

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.

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.

Related