尝试发布到包时,Github Actions 工作流(私人仓库)挂起

Posted

技术标签:

【中文标题】尝试发布到包时,Github Actions 工作流(私人仓库)挂起【英文标题】:Github Actions workflow (private repo) hangs when trying to publish to packages 【发布时间】:2021-10-03 11:36:02 【问题描述】:

私有存储库的 Github Actions 工作流程:

在 IntelliJ 项目中,OrionDAO 是 Orion 项目的一个方面,因此必须首先运行。

OrionDAO Pom 的一部分如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>uk.co.xxxx.orionDAO</groupId>
   <artifactId>orionDAO</artifactId>
   <packaging>jar</packaging>
   <name>Orion Model</name>
   <description>Orion </description>
   <version>25.1.3</version>

……….


   <distributionManagement>
      <repository>
         <id>github</id>
         <name>Apache Maven Packages</name>
         <url>https://maven.pkg.github.com/xxxx/Orion_CICD</url>
      </repository>
   </distributionManagement>
</project>

Orion Pom 的一部分如下:

4.0.0

<groupId>uk.co.xxxx.orion</groupId>
<artifactId>orion</artifactId>
<version>25.1.3</version>
<packaging>war</packaging>

<name>Orion</name>
<description>Orion </description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>15</java.version>
    <maven.compiler.source>$java.version</maven.compiler.source>
    <maven.compiler.target>$java.version</maven.compiler.target>
    <hapi-fhir-version>4.2.0</hapi-fhir-version>

</properties>

<dependencies>
    <dependency>
        <groupId>uk.co.xxxx.orionDAO</groupId>
        <artifactId>orionDAO</artifactId>
        <version>25.1.3</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
    <finalName>orion</finalName>
</build>

<distributionManagement>
    <repository>
        <id>github</id>
        <name>GitHub ReedInPartnership Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/xxxx/Orion_CICD</url>
    </repository>
</distributionManagement>

在部署步骤之前一切正常。

然后它就坐在那里。

我添加了权限:step 但仍然挂起。

# This workflow will trigger when a PR is closed and will put out a message
# dependant upon Orion being built and deployed to the Master branch

name: Master Workflow - PR message output and build and deploy
on:
  pull_request:
    types: closed
    branches:
      - master

permissions:
  contents: read
  packages: write

jobs:
  merge_job:
    # this job will only run if the PR has been merged
    if: github.event.pull_request.merged == true
    runs-on: windows-latest
    steps:
      - run: |
          echo PR #$ github.event.number  has been merged

  close_job:
    # this job will only run if the PR has been closed without being merged
    if: github.event.pull_request.merged == false
    runs-on: windows-latest
    steps:
      - run: |
          echo PR #$ github.event.number  has been closed without being merged

  deploy_job:
    # this job will only run if the PR has been merged i.e merge-job completed successfully
    needs: merge_job
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 16 for now
        uses: actions/setup-java@v2
        with:
          java-version: '16'
          distribution: 'adopt'

      - name: Set up Maven
        uses: stCarolas/setup-maven@v4
        with:
          maven-version: 3.6.3

      - name: Cache Maven packages
        uses: actions/cache@v2
        with:
          path: ~/.m2
          key: $ runner.os -m2-$ hashFiles('**/pom.xml') 
          restore-keys: $ runner.os -m2

      - name: 'Create settings.xml'
        uses: whelk-io/maven-settings-xml-action@v4
        with:
          repositories: '[ "id": "github", "url": "https://maven.pkg.github.com/ReedInPartnership/Orion_CICD" ]'
          servers: '["id": "github", "username": "$ secrets.GITHUB_USERNAME ", "password": "$ secrets.GITHUB_PASSWORD "]'

      - name: Install Jespa
        run: mvn install:install-file "-Dfile=Orion\jespa-1.2.6.jar" "-DgroupId=jespa" "-DartifactId=jespa" "-Dversion=1.2.6" "-Dpackaging=jar" "-DgeneratePom=true"

      - name: Build with Maven (Orion DAO)
        run: mvn -B install --file Orion_DAO/pom.xml

      - name: Build with Maven (Orion)
        run: mvn -B install --file Orion/pom.xml

      - name: Publish to GitHub Packages Orion
        env:
          GITHUB_TOKEN: $ secrets.GITHUB_TOKEN 
        run: mvn --batch-mode --file Orion/pom.xml deploy

输出:上传 jar 和 pom 到 github 时挂起

***** 更新 ***** ***** 更新 ***** ***** 更新 *****

这是由我自己作为合并请求的创建者以及合并它的人完成的,我想这可能就是它挂起的原因。所以我让其他人(审阅者)进行合并,现在它没有挂起,但我收到以下错误

Error:  Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project orion: Failed to deploy artifacts: Could not transfer artifact uk.co.reed.orion:orion:war:25.1.3 from/to github (https://maven.pkg.github.com/xxxx/Orion_CICD): Transfer failed for https://maven.pkg.github.com/xxxx/Orion_CICD/uk/co/reed/orion/orion/25.1.3/orion-25.1.3.war 401 Unauthorized -> [Help 1]
Error:  
Error:  To see the full stack trace of the errors, re-run Maven with the -e switch.
Error:  Re-run Maven using the -X switch to enable full debug logging.
Error:  
Error:  For more information about the errors and possible solutions, please read the following articles:
Error:  [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Error: Process completed with exit code 1.

***** 更新2 ***** ***** 更新2 ***** ***** 更新2 *****

我现在已经获得了另一位具有管理员权限的审阅者来合并它,我们回到了刚刚挂在 maven 部署步骤中的工作流!!

【问题讨论】:

它可能需要更改 maven 的 settings.xml 才能将代码上传到 mvnrepo? @Samridh Tuladhar 我想将工件发布到 Github,以便在发布步骤中使用它们。 【参考方案1】:

最后我创建了一个发布工作流,然后它创建了包。似乎软件包仅在发布事件类型上创建,尽管我在阅读的所有文档中都没有提到这一点。

# This workflow will build and publish to Github Packages on release

name: Master Workflow - Build and Publish 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: '16'
          distribution: 'adopt'

      - name: Install Jespa
        run: mvn install:install-file "-Dfile=Orion\jespa-1.2.6.jar" "-DgroupId=jespa" "-DartifactId=jespa" "-Dversion=1.2.6" "-Dpackaging=jar" "-DgeneratePom=true"

      - name: Build with Maven (Orion DAO)
        run: mvn -B install --file Orion_DAO/pom.xml

      - name: Build with Maven (Orion)
        run: mvn -B install --file Orion/pom.xml

      - name: Publish package
        run: mvn --batch-mode --file Orion/pom.xml deploy
        env:
          GITHUB_TOKEN: $ secrets.GITHUB_TOKEN 

【讨论】:

【参考方案2】:

您可以将settings.xml添加到与pom.xml相同的项目根文件夹中

然后你可以像这样添加步骤:

- name: Build with Maven
   env:
      USENAME: $github.actor
      PASSWORD: $ secrets.GITHUB_TOKEN 
   run: mvn -B clean package --settings settings.xml

settings.xml 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">

    <servers>
        <server>
            <id>github</id>
            <username>$env.USERNAME</username>
            <password>$env.PASSWORD</password>
        </server>

    </servers>

</settings>

【讨论】:

以上是关于尝试发布到包时,Github Actions 工作流(私人仓库)挂起的主要内容,如果未能解决你的问题,请参考以下文章

在另一个工作流成功运行后手动触发 Github Actions 工作流

将 GitHub Actions 工作流程中的输入值绕过到 terraform 变量文件

如何在 GitHub Actions 工作流程中进行 apt-get install?

为除 main 之外的所有分支触发 GitHub Actions,其中唯一的工作流定义文件是

在 GitHub Actions 中添加基于日期的标签

GitHub Actions:如何通过终端访问当前构建的日志