Introduction
Esta guía te muestra cómo crear un flujo de trabajo que publique paquetes de Java en Registro del paquete de GitHub y en el Repositorio Central de Maven. Con un solo flujo de trabajo, puedes publicar los paquetes en un solo repositorio o en varios repositorios.
Prerequisites
We recommend that you have a basic understanding of workflow files and configuration options. For more information, see "Learn GitHub Actions."
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:
- "Working with the npm registry"
- "Environment variables"
- "Encrypted secrets"
- "Authentication in a workflow"
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 "Events that trigger workflows."
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")
}
}
}
}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 "Creating and using encrypted secrets."
# This workflow uses actions that are not certified by GitHub.
# Estas las proporcionan entidades terceras y las gobiernan
# condiciones de servicio, políticas de privacidad y documentación de soporte
# documentación.
name: Publish package to the Maven Central Repository
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Publish package
run: gradle publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}Este flujo de trabajo realiza los siguientes pasos:
-
Verifica una copia del repositorio del proyecto.
-
Configura el JDK de Java.
-
Valida las sumas de comprobación de cualquier archivo JAR Wrapper de Gradle en el repositorio.
-
Runs the
gradle publishcommand to publish to theOSSRHMaven repository. TheMAVEN_USERNAMEenvironment variable will be set with the contents of yourOSSRH_USERNAMEsecret, and theMAVEN_PASSWORDenvironment variable will be set with the contents of yourOSSRH_TOKENsecret.For more information about using secrets in your workflow, see "Creating and using encrypted secrets."
Publishing packages to Registro del paquete de GitHub
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 Registro del paquete de GitHub if CI tests pass. For more information on the release event, see "Events that trigger workflows."
You can define a new Maven repository in the publishing block of your build.gradle that points to Registro del paquete de GitHub. 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.
El secreto de GITHUB_TOKEN se configuro para un token de acceso para el repositorio cada vez que comienza un job en un flujo de trabajo. Debes configurar los permisos para este token de acceso en el archivo del flujo de trabajo para otorgar acceso de lectura para el alcance contents y acceso de escritura para el de packages. Para obtener más información, consulta la sección "Autenticarte con el GITHUB_TOKEN".
For example, if your organization is named "octocat" and your repository is named "hello-world", then the Registro del paquete de GitHub 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")
}
}
}
}With this configuration, you can create a workflow that publishes your package to Registro del paquete de GitHub by running the gradle publish command.
# This workflow uses actions that are not certified by GitHub.
# Estas las proporcionan entidades terceras y las gobiernan
# condiciones de servicio, políticas de privacidad y documentación de soporte
# documentación.
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@v2
- uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Publish package
run: gradle publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}Este flujo de trabajo realiza los siguientes pasos:
-
Verifica una copia del repositorio del proyecto.
-
Configura el JDK de Java.
-
Valida las sumas de comprobación de cualquier archivo JAR Wrapper de Gradle en el repositorio.
-
Runs the
gradle publishcommand to publish to Registro del paquete de GitHub. TheGITHUB_TOKENenvironment variable will be set with the content of theGITHUB_TOKENsecret. Thepermissionskey specifies the access that theGITHUB_TOKENsecret will allow.For more information about using secrets in your workflow, see "Creating and using encrypted secrets."
Publishing packages to the Maven Central Repository and Registro del paquete de GitHub
You can publish your packages to both the Maven Central Repository and Registro del paquete de GitHub 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 Registro del paquete de GitHub, 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")
}
}
}
}With this configuration, you can create a workflow that publishes your package to both the Maven Central Repository and Registro del paquete de GitHub by running the gradle publish command.
# This workflow uses actions that are not certified by GitHub.
# Estas las proporcionan entidades terceras y las gobiernan
# condiciones de servicio, políticas de privacidad y documentación de soporte
# documentación.
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@v2
- name: Set up Java
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Publish package
run: gradle publish
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}Este flujo de trabajo realiza los siguientes pasos:
-
Verifica una copia del repositorio del proyecto.
-
Configura el JDK de Java.
-
Valida las sumas de comprobación de cualquier archivo JAR Wrapper de Gradle en el repositorio.
-
Runs the
gradle publishcommand to publish to theOSSRHMaven repository and Registro del paquete de GitHub. TheMAVEN_USERNAMEenvironment variable will be set with the contents of yourOSSRH_USERNAMEsecret, and theMAVEN_PASSWORDenvironment variable will be set with the contents of yourOSSRH_TOKENsecret. TheGITHUB_TOKENenvironment variable will be set with the content of theGITHUB_TOKENsecret. Thepermissionskey specifies the access that theGITHUB_TOKENsecret will allow.For more information about using secrets in your workflow, see "Creating and using encrypted secrets."