GitHub Actions Deploy

Package Registry

The project’s Maven and NPM packages are stored in GitHub Package Registry.

Deploy step

Maven POM and JAR projects are pushed to the Maven Repository, where executables are handled using GitHub releases.

Maven Repository

The Maven deploy goal requires write access to the Maven Repository, which is normally setup using the Maven settings.xml file. This file should contain a <server> entry containing the credentials used for the <repository> entry in the project’s pom.xml file. For example:

<servers>
	<server>
		<id>maven</id>
		<username>xxxx</username>
		<password>yyyy</password>
	</server>
</servers>

GitHub Actions provides the setup-java action to configure the settings.xml file which is used during the workflow. In the setup-java step add the server- tags to create an <server> entry in the settings.xml file:

steps:
- name: Set up JDK 11
  uses: actions/setup-java@v3
  with:
    server-id: maven (1)
    server-username: MAVEN_USERNAME (2)
    server-password: MAVEN_TOKEN (2)
1 The value of the repositories.repository.id XML tag in the project’s 'pom.xml' file
2 The environment variable which will be used later to populate the repositories.repository.username and repositories.repository.password XML tags in the to be generated settings.xml file.

In subsequent steps, where Maven requires read or write access to the private Maven Repository, set the MAVEN_USERNAME and MAVEN_PASSWORD enviroment variables. For instance:

steps:

- name: Package with Maven
  run: mvn --batch-mode --no-transfer-progress package
  env:
    MAVEN_USERNAME: xxxx
    MAVEN_TOKEN: ${{ secrets.EVENT_PACKAGE_REPO_TOKEN }}

- name: Publish to GitHub Packages using Apache Maven
  run: mvn --batch-mode --no-transfer-progress deploy
  env:
    MAVEN_USERNAME: xxxx
    MAVEN_TOKEN: ${{ secrets.EVENT_PACKAGE_REPO_TOKEN }}

In the above example the secret EVENT_PACKAGE_REPO_TOKEN is configured as a repository secret in the repository where the workflow is run.

The settings.xml file created from the above example look like the following:

settings.xml file created for the deploy to a Maven Repository
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>maven</id>
      <username>${env.MAVEN_USERNAME}</username>
      <password>${env.MAVEN_CENTRAL_TOKEN}</password>
    </server>
  </servers>
</settings>
The settings.xml file is created in the Actions $HOME/.m2 directory. If an existing settings.xml file exist at that location, it will be overwritten. The settings-path can be set to change the settings.xml file location.

To prevent overwriting the settings.xml file, set overwrite-settings: false.