maven - 如何在 Github Actions 工作流中从 Github 包访问 Maven 依赖项?
问题描述
我的构建通过直接在pom.xml
<repository>
元素上使用 User + PAT(个人访问令牌)在本地工作:
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://[USER]:[PAT]@maven.pkg.github.com/myaccount/myrepo</url>
</repository>
从github下载:https://[USER]:[PAT]@maven.pkg.github.com/myaccount/myrepo/org/springframework/flex/spring-flex-core/1.6.1.BUILD-SNAPSHOT/maven- metadata.xml(796 B,592 B/s)
我没有settings.xml
配置。
但是,它在 Github Actions 工作流程上有所突破:
警告:无法将元数据 org.springframework.flex:spring-flex-core:1.6.1.BUILD-SNAPSHOT/maven-metadata.xml 从/到 github (***maven.pkg.github.com/myaccount/myrepo ): https://maven.pkg.github.com/myaccount/myrepo/org/springframework/flex/spring-flex-core/1.6.1.BUILD-SNAPSHOT/maven-metadata.xml 401 未经授权的身份验证失败
无法在 org.springframework.flex:spring-flex-core:jar:1.6.1.BUILD-SNAPSHOT 收集依赖项:无法读取 org.springframework.flex:spring-flex-core:jar:1.6.1 的工件描述符.BUILD-SNAPSHOT
我的工作流程是这样的:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: mvn -B package --file dev/server/pom.xml
为什么它会在 Github 工作流程上中断?
解决方案
根据你的问题,我想:
- 你已经部署了 maven 项目
GitHub Package
,我们称之为library
- 你有另一个 maven 项目,它使用
library
包作为 pom.xml 中的依赖项,我们称这个项目为你的app
- 您想使用
GitHub Actions
inapp
存储库添加自动化构建工作流程
如果您library
甚至是公共包,目前不幸的是,GitHub 不支持maven
对公共包进行未经授权的访问。因此,您应该执行以下操作:
转到
app
存储库的设置部分,并在Secrets
创建两个名为的环境机密小节中USER_NAME
,其值包含您的 GitHub 用户名(或library
包所有者的用户名);并ACCESS_TOKEN
指向上一步创建的 PAT 令牌的值。现在,
maven-settings.xml
在app
存储库中创建一个,例如,您可以在workflow.yml
文件旁边创建它。该文件包含:
<?xml version="1.0" encoding="UTF-8"?>
<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>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/owner_username/package_name</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>${env.USER_NAME}</username>
<password>${env.ACCESS_TOKEN}</password>
</server>
</servers>
</settings>
- 并且,最后使用这些设置文件,在工作流中运行 maven 命令。例如,该
workflow.yaml
文件可以包含:
name: Java CI with Maven
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Build with Maven
run: mvn -s $GITHUB_WORKSPACE/.github/workflows/maven-settings.xml -B package --file pom.xml
env:
USER_NAME: ${{ secrets.USER_NAME }}
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
推荐阅读
- python-2.7 - 我需要在 Nao v1.14.5 上安装 Image 模块吗?
- mongodb - bulkwrite 不支持使用 mongo-go-driver 进行多文档事务
- sql - 通过 WHERE 条件将 SELECT 分成多列,而不会创建由添加到 GROUP BY 引起的多行
- scala - 如何在逆变位置使用协变类型参数的解决方法
- assembly - 组装 - 查询字符串
- java - 我为我的 java 库添加了 CLI 支持,如何方便地将其公开给我的库用户?
- c# - 构造和填充二维数组
- python - 如何在pandas python中转换列中的行并执行操作
- python - 向 Django 2.1 管理站点添加忘记密码功能
- python - 如何在 Komodo 中使用替换正则表达式?