Remarque
Les exécuteurs hébergés sur GitHub ne sont pas pris en charge sur GitHub Enterprise Server. Vous pouvez voir plus d’informations sur le support futur planifié dans la GitHub public roadmap.
Introduction
Ce guide vous montre comment créer un workflow qui publie des packages Java sur GitHub Packages et le dépôt central Maven. Avec un seul workflow, vous pouvez publier des packages sur un dépôt unique ou sur plusieurs dépôts.
Avertissement
Les exemples utilisés dans ce guide font référence au service OSSRH hérité. Consultez Publication dans la documentation du référentiel central Maven.
Prerequisites
We recommend that you have a basic understanding of workflow files and configuration options. For more information, see Writing workflows.
For more information about creating a CI workflow for your Java project with Gradle, see Building and testing Java with Gradle.
You may also find it helpful to have a basic understanding of the following:
- Utilisation du registre Apache Maven
- Store information in variables
- Using secrets in GitHub Actions
- Automatic token authentication
About package configuration
The groupId
and artifactId
fields in the MavenPublication
section of the build.gradle file create a unique identifier for your package that registries use to link your package to a registry. This is similar to the groupId
and artifactId
fields of the Maven pom.xml file. For more information, see the Maven Publish Plugin in the Gradle documentation.
The build.gradle file also contains configuration for the distribution management repositories that Gradle will publish packages to. Each repository must have a name, a deployment URL, and credentials for authentication.
Publishing packages to the Maven Central Repository
Each time you create a new release, you can trigger a workflow to publish your package. The workflow in the example below runs when the release
event triggers with type created
. The workflow publishes the package to the Maven Central Repository if CI tests pass. For more information on the release
event, see Événements qui déclenchent des flux de travail.
You can define a new Maven repository in the publishing block of your build.gradle file that points to your package repository. For example, if you were deploying to the Maven Central Repository through the OSSRH hosting project, your build.gradle could specify a repository with the name "OSSRH"
.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "OSSRH" url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" credentials { username = System.getenv("MAVEN_USERNAME") password = System.getenv("MAVEN_PASSWORD") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "OSSRH"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to the Maven Central Repository by running the gradle publish
command. In the deploy step, you’ll need to set environment variables for the username and password or token that you use to authenticate to the Maven repository. For more information, see Using secrets in GitHub Actions.
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub. # Elles sont fournies par un tiers et régies par # des conditions d’utilisation du service, une politique de confidentialité et un support distincts. # documentation en ligne. # GitHub recommande d’épingler les actions à un SHA de commit. # Pour obtenir une version plus récente, vous devez mettre à jour le SHA. # Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement. name: Publish package to the Maven Central Repository on: release: types: [created] jobs: publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Java uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub.
# Elles sont fournies par un tiers et régies par
# des conditions d’utilisation du service, une politique de confidentialité et un support distincts.
# documentation en ligne.
# GitHub recommande d’épingler les actions à un SHA de commit.
# Pour obtenir une version plus récente, vous devez mettre à jour le SHA.
# Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement.
name: Publish package to the Maven Central Repository
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
Ce workflow effectue les étapes suivantes :
-
Extrait une copie du dépôt du projet.
-
Configure le JDK Java.
-
Configure l’environnement Gradle. L’action
gradle/actions/setup-gradle
effectue la mise en cache de l’état entre les exécutions de flux de travail et fournit un résumé détaillé de toutes les exécutions Gradle. -
Executes the Gradle
publish
task to publish to theOSSRH
Maven repository. TheMAVEN_USERNAME
environment variable will be set with the contents of yourOSSRH_USERNAME
secret, and theMAVEN_PASSWORD
environment variable will be set with the contents of yourOSSRH_TOKEN
secret.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.
Publishing packages to GitHub Packages
Each time you create a new release, you can trigger a workflow to publish your package. The workflow in the example below runs when the release
event triggers with type created
. The workflow publishes the package to GitHub Packages if CI tests pass. For more information on the release
event, see Événements qui déclenchent des flux de travail.
You can define a new Maven repository in the publishing block of your build.gradle that points to GitHub Packages. In that repository configuration, you can also take advantage of environment variables set in your CI workflow run. You can use the GITHUB_ACTOR
environment variable as a username, and you can set the GITHUB_TOKEN
environment variable with your GITHUB_TOKEN
secret.
Le secret GITHUB_TOKEN
a la valeur d’un jeton d’accès pour le dépôt chaque fois qu’un travail d’un workflow démarre. Vous devez définir les autorisations de ce jeton d’accès dans le fichier de workflow afin d’octroyer l’accès en lecture pour l’autorisation contents
et l’accès en écriture pour l’autorisation packages
. Pour plus d’informations, consultez « Automatic token authentication ».
For example, if your organization is named "octocat" and your repository is named "hello-world", then the GitHub Packages configuration in build.gradle would look similar to the below example.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "GitHubPackages" url = "https://maven.pkg.github.com/octocat/hello-world" credentials { username = System.getenv("GITHUB_ACTOR") password = System.getenv("GITHUB_TOKEN") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/octocat/hello-world"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to GitHub Packages by running the gradle publish
command.
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub. # Elles sont fournies par un tiers et régies par # des conditions d’utilisation du service, une politique de confidentialité et un support distincts. # documentation en ligne. # GitHub recommande d’épingler les actions à un SHA de commit. # Pour obtenir une version plus récente, vous devez mettre à jour le SHA. # Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement. name: Publish package to GitHub Packages on: release: types: [created] jobs: publish: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub.
# Elles sont fournies par un tiers et régies par
# des conditions d’utilisation du service, une politique de confidentialité et un support distincts.
# documentation en ligne.
# GitHub recommande d’épingler les actions à un SHA de commit.
# Pour obtenir une version plus récente, vous devez mettre à jour le SHA.
# Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement.
name: Publish package to GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Ce workflow effectue les étapes suivantes :
-
Extrait une copie du dépôt du projet.
-
Configure le JDK Java.
-
Configure l’environnement Gradle. L’action
gradle/actions/setup-gradle
effectue la mise en cache de l’état entre les exécutions de flux de travail et fournit un résumé détaillé de toutes les exécutions Gradle. -
Executes the Gradle
publish
task to publish to GitHub Packages. TheGITHUB_TOKEN
environment variable will be set with the content of theGITHUB_TOKEN
secret. Thepermissions
key specifies the access that theGITHUB_TOKEN
secret will allow.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.
Publishing packages to the Maven Central Repository and GitHub Packages
You can publish your packages to both the Maven Central Repository and GitHub Packages by configuring each in your build.gradle file.
Ensure your build.gradle file includes a repository for both your GitHub repository and your Maven Central Repository provider.
For example, if you deploy to the Central Repository through the OSSRH hosting project, you might want to specify it in a distribution management repository with the name
set to OSSRH
. If you deploy to GitHub Packages, you might want to specify it in a distribution management repository with the name
set to GitHubPackages
.
If your organization is named "octocat" and your repository is named "hello-world", then the configuration in build.gradle would look similar to the below example.
plugins { ... id 'maven-publish' } publishing { ... repositories { maven { name = "OSSRH" url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" credentials { username = System.getenv("MAVEN_USERNAME") password = System.getenv("MAVEN_PASSWORD") } } maven { name = "GitHubPackages" url = "https://maven.pkg.github.com/octocat/hello-world" credentials { username = System.getenv("GITHUB_ACTOR") password = System.getenv("GITHUB_TOKEN") } } } }
plugins {
...
id 'maven-publish'
}
publishing {
...
repositories {
maven {
name = "OSSRH"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}
}
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/octocat/hello-world"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
With this configuration, you can create a workflow that publishes your package to both the Maven Central Repository and GitHub Packages by running the gradle publish
command.
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub. # Elles sont fournies par un tiers et régies par # des conditions d’utilisation du service, une politique de confidentialité et un support distincts. # documentation en ligne. # GitHub recommande d’épingler les actions à un SHA de commit. # Pour obtenir une version plus récente, vous devez mettre à jour le SHA. # Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement. name: Publish package to the Maven Central Repository and GitHub Packages on: release: types: [created] jobs: publish: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - name: Set up Java uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin' - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Publish package run: ./gradlew publish env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Ce workflow utilise des actions qui ne sont pas certifiées par GitHub.
# Elles sont fournies par un tiers et régies par
# des conditions d’utilisation du service, une politique de confidentialité et un support distincts.
# documentation en ligne.
# GitHub recommande d’épingler les actions à un SHA de commit.
# Pour obtenir une version plus récente, vous devez mettre à jour le SHA.
# Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement.
name: Publish package to the Maven Central Repository and GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
- name: Publish package
run: ./gradlew publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Ce workflow effectue les étapes suivantes :
-
Extrait une copie du dépôt du projet.
-
Configure le JDK Java.
-
Configure l’environnement Gradle. L’action
gradle/actions/setup-gradle
effectue la mise en cache de l’état entre les exécutions de flux de travail et fournit un résumé détaillé de toutes les exécutions Gradle. -
Executes the Gradle
publish
task to publish to theOSSRH
Maven repository and GitHub Packages. TheMAVEN_USERNAME
environment variable will be set with the contents of yourOSSRH_USERNAME
secret, and theMAVEN_PASSWORD
environment variable will be set with the contents of yourOSSRH_TOKEN
secret. TheGITHUB_TOKEN
environment variable will be set with the content of theGITHUB_TOKEN
secret. Thepermissions
key specifies the access that theGITHUB_TOKEN
secret will allow.For more information about using secrets in your workflow, see Using secrets in GitHub Actions.