首页 > 解决方案 > 无法通过 GitHub 操作使用 AWS CodeArtifact 进行身份验证

问题描述

我无法从 GitHub 操作中向 AWS CodeArtifact 进行身份验证。AWS 响应始终为 401。

我正在执行以下步骤:

    steps:
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{ secrets.AWS_REGION }}

    - run: aws configure --profile my-custom-profile set region ${{ secrets.AWS_REGION }}
    - run: aws configure --profile my-custom-profile set role_arn ${{ secrets.AWS_ARN }}
    - run: aws configure --profile my-custom-profile set source_profile default
    - run: dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
    - run: dotnet codeartifact-creds install
    - run: dotnet codeartifact-creds configure set profile my-custom-profile

    - uses: actions/checkout@v2
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.100

    - name: Restore dependencies
      run: dotnet restore

它一直在线上死去dotnet restore

在此处输入图像描述

谁能建议我错误地执行了哪些步骤 - 或者 - 丢失了?

旁注:在这之前,花了一些时间,但我最终还是让它在我的 localhost Windows 开发机器上运行。因此,我存档的凭据似乎有效。

标签: .netamazon-web-servicesnugetdotnet-restoreaws-codeartifact

解决方案


以下是在 GitHub 操作中使用 AWS CodeArtifact 进行身份验证的步骤。

高级步骤

  • ./aws/credentials使用[default]个人资料/信用创建一些。
  • config使用一些特定的 AWS CodeArtifact 凭据创建一个文件。
  • 从 AWS CodeArtifact 获取身份验证令牌
  • 将此身份验证令牌保存到环境变量
  • 拉下所有代码。这需要在您开始使用“nuget 源”之前进行。
  • 使用身份验证令牌将 AWS CodeArtifact nuget 源手动添加到您的 nuget 源。
  • 检查 AWS CodeArtifact 现在是否在 nuget 源列表中。
  • 点网还原。

GitHub 操作代码

注意:用您自己的自定义 AWS 设置等替换诸如<domain>or之类的东西。<some-id>

    - run: |
        echo '[default]' >> ~/.aws/credentials
        echo 'aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}' >> ~/.aws/credentials
        echo 'aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}' >> ~/.aws/credentials

    - run: |
        aws configure --profile nuget-read set region us-east-1
        aws configure --profile nuget-read set role_arn arn:aws:iam::<some-id>:role/nuget-read
        aws configure --profile nuget-read set source_profile default
        aws configure list

    - run: aws codeartifact get-authorization-token --domain <some domain> --profile nuget-read > at.json
    - run: echo "AUTH_TOKEN= $(jq '.authorizationToken' at.json)" >> $GITHUB_ENV

    - uses: actions/checkout@v2

    - run: dotnet nuget add source https://<domain>-<id>.d.codeartifact.<aws region>.amazonaws.com/nuget/cosmos-nuget/v3/index.json --name <name of this nuget source. It can be anything> --password ${{ env.AUTH_TOKEN }} --username aws --store-password-in-clear-text

    - run: dotnet nuget list source

    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.100
    
    - name: Restore dependencies
      run: dotnet restore

请注意--store-password-in-clear-text手动添加 nuget 源时的情况。这是废话,但至少需要在 linux 机器上工作。否则,它无法添加源,因为它不知道如何加密它,或者其他什么。


所以可能有更好的方法来做到这一点,但至少现在可以了!


推荐阅读