deploy to Github Package Registry from Github Action

后端 未结 5 1604
暗喜
暗喜 2020-12-16 16:10

I would like to deploy to a GitHub Package Registry from a GitHub Action of a public repo.

I have a yml file for a workflow:

name: My CI

on: [push]
         


        
相关标签:
5条回答
  • 2020-12-16 16:12

    There is an easier way in 2020.

    First, add distribution configuration to your pom.xml:

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

    The id must be github.

    Second, use actions/setup-java@v1 in action

    steps:
      - uses: actions/checkout@v2
    
      - uses: actions/setup-java@v1
        with:
          java-version: 1.8
    
      - name: Publish to GitHub Packages
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: mvn deploy
    
    0 讨论(0)
  • 2020-12-16 16:15

    Well, According to:

    • maven command line how to point to a specific settings.xml for a single command?
    • GitHub package registry as Maven repo - trouble uploading artifact

    I think you could simply do like this:

    1. Add below to your workflow
    - name: Deploy to Github Package Registry
        env:
          GITHUB_USERNAME: x-access-token
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run:
          mvn --settings settings.xml deploy
    
    1. And then add settings.xml to your project
    <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
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
        <activeProfiles>
            <activeProfile>github</activeProfile>
        </activeProfiles>
    
        <profiles>
            <profile>
                <id>github</id>
                <repositories>
                    <repository>
                        <id>central</id>
                        <url>https://repo1.maven.org/maven2</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>github</id>
                        <name>GitHub OWNER Apache Maven Packages</name>
                        <url>https://maven.pkg.github.com/OWNER </url>
                    </repository>
                </repositories>
            </profile>
        </profiles>
    
        <servers>
            <server>
                <id>github</id>
                <username>${env.GITHUB_USERNAME}</username>
                <password>${env.GITHUB_TOKEN}</password>
            </server>
        </servers>
    </settings>
    

    It works for me, I hope this will help.

    0 讨论(0)
  • 2020-12-16 16:19

    TL;DR: Just commit the following to .github/workflows/mavenpublish.yml and create a release via the GitHub web page to trigger the process:

    name: Maven Package
    
    on:
      release:
        types: [created]
    
    jobs:
      build:
    
        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: Deploy to Github Package Registry
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            run: |
              mkdir -p ~/.m2
              echo "<settings><servers><server><id>gh</id><username>$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $1}')</username><password>\${env.GITHUB_TOKEN}</password></server></servers></settings>" > ~/.m2/settings.xml
              REPO="gh::default::https://maven.pkg.github.com/${GITHUB_REPOSITORY}"
              mvn deploy -DaltReleaseDeploymentRepository="${REPO}" -DaltSnapshotDeploymentRepository="${REPO}"
    

    Some more info:

    I have built the same thing before for Jenkins and can tell you that you don't need to create a settings.xml nor adapt your pom.xml in your repo.

    You can even avoid writing your GitHub Token into the settings.xml (which is more secure).

    Also, you don't need to manually add your repo and username, these can be read from the environment.

    If you want it to build on push, just change the lines behind on: to [push].

    Here`s a real-life example.

    0 讨论(0)
  • 2020-12-16 16:22

    I had a similar issue for my project. Every time I ran mvn deploy, it failed with:

    Could not transfer metadata ... from/to github (https://maven.pkg.github.com/.../...): 400

    However, on a whim, I changed the version number of my project from 0.0.3-SNAPSHOT to 0.0.4 and after that, it worked.

    Maybe it will work for you too.

    0 讨论(0)
  • 2020-12-16 16:24

    To make it work, you need to do two things:

    1. Add the following to your pom.xml:
    <distributionManagement>
       <repository>
         <id>github</id>
         <name>GitHub OWNER Apache Maven Packages</name>
         <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
       </repository>
    </distributionManagement>
    

    source: https://help.github.com/en/articles/configuring-apache-maven-for-use-with-github-package-registry#publishing-a-package

    1. Setup a Maven settings file with the username/password from within your build action. In my case I did something like this:
    name: Java CI
    
    on: [push]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v1
        - name: Set up JDK 1.8
          uses: actions/setup-java@v1
          with:
            java-version: 1.8
        - name: Deploy to Github Package Registry
          env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          run: |
            mkdir ~/.m2
            echo "<settings><servers><server><id>github</id><username>OWNER</username><password>${GITHUB_TOKEN}</password></server></servers></settings>" > ~/.m2/settings.xml
            mvn deploy
    

    Unfortunately, I don't think you can pass the username/password as arguments to Maven and so you need to set up the settings file instead. source: Is it possible to pass a password in Maven Deploy in the command line?

    Lastly, I confirm that this only works for non-SNAPSHOT artifacts. When I try deploying a SNAPSHOT version it fails with a 400 error as described.

    0 讨论(0)
提交回复
热议问题