如何在 Github Actions 中从 Artifactory 安装范围内的私有 npm 包

bar*_*iro 3 artifactory npm-registry github-actions

这个问题包括一个特定的用例:

  1. 我有一个私有范围的包:@myscope/mypackage
  2. 它托管在 Artifactory NPM 注册表中:https://company.jfrog.io/artifactory/api/npm/my-npm-registry/
  3. 我需要使用我的凭据来使用它。
  4. 我想在 Github Actions 中使用它。

我怎样才能做到这一点?

bar*_*iro 8

.npmrc

首先,您需要在本地.npmrc文件中配置您的访问权限。您可以将此文件放在源根文件夹中。

always-auth = true

# First, set a different registry URL for your scope
@myscope:registry=https://company.jfrog.io/artifactory/api/npm/my-npm-registry/
# Then, for this scope, you need to set the token
//company.jfrog.io/artifactory/api/npm/my-npm-registry/:_auth = {{your token - see below}}
Run Code Online (Sandbox Code Playgroud)

代币

您需要从 Artifactory 获取 NPM 令牌(请注意,这不是您的 API 密钥。

  1. 从 Artifactory 个人资料中获取Artifactory API 密钥: https://company.jfrog.io/ui/admin/artifactory/user_profile
  2. 在 Linux 终端上运行以下命令:curl -u {{ ARTIFACTORY_USERNAME }}:{{ ARTIFACTORY_API_KEY }} https://company.jfrog.io/artifactory/api/npm/auth/
    • 电源外壳:
      $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f {{ ARTIFACTORY_USERNAME }},{{ ARTIFACTORY_API_KEY }})))
      Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} https://company.jfrog.io/artifactory/api/npm/auth/
      
      Run Code Online (Sandbox Code Playgroud)
  3. 您应该收到此信息:
    _auth = {{ YOUR_NPM_TOKEN }}
    always-auth = true
    
    Run Code Online (Sandbox Code Playgroud)
  4. 所以现在你可以把这个Token放到.npmrc上面的文件中。

Github 操作

如何在 Github Actions 中完成这一切?

  1. 首先,将您的 Jfrog 用户名和 API 密钥保存在 Github Secrets: JFROG_USER&中JFROG_PAT
  2. 您可以在结帐之后和之前 将下一步添加到工作流程中yarn/npm install
    always-auth = true
    
    # First, set a different registry URL for your scope
    @myscope:registry=https://company.jfrog.io/artifactory/api/npm/my-npm-registry/
    # Then, for this scope, you need to set the token
    //company.jfrog.io/artifactory/api/npm/my-npm-registry/:_auth = {{your token - see below}}
    
    Run Code Online (Sandbox Code Playgroud)