在 CodeBuild 中使用 Python 程序包 - CodeArtifact

在 CodeBuild 中使用 Python 程序包

以下步骤已使用 CodeBuild 提供的 Docker 映像中列出的操作系统进行了测试。

使用 IAM 角色设置权限

在 CodeBuild 中使用来自 CodeArtifact 的 Python 程序包时,需要执行这些步骤。

  1. 登录 AWS 管理控制台,然后通过以下网址打开 IAM 控制台:https://console.aws.amazon.com/iam/

  2. 在导航窗格中,选择角色。在角色页面上,编辑 CodeBuild 构建项目所使用的角色。此角色必须具有以下权限。

    JSON
    { "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "codeartifact:GetAuthorizationToken", "codeartifact:GetRepositoryEndpoint", "codeartifact:ReadFromRepository" ], "Resource": "*" }, { "Effect": "Allow", "Action": "sts:GetServiceBearerToken", "Resource": "*", "Condition": { "StringEquals": { "sts:AWSServiceName": "codeartifact.amazonaws.com" } } } ] }
    重要

    如果您还想使用 CodeBuild 来发布程序包,请添加 codeartifact:PublishPackageVersion 权限。

    有关信息,请参阅《IAM 用户指南》中的修改角色

登录并使用 pip 或 twine

要使用 CodeBuild 中的 Python 程序包,请运行项目 buildspec.yaml 文件的 pre-build 部分的 login 命令来配置 pip,以便从 CodeArtifact 提取程序包。有关更多信息,请参阅 将 CodeArtifact 与 Python 结合使用

成功运行 login 后,您可以运行 build 部分中的 pip 命令来安装或发布 Python 程序包。

Linux

注意

仅当您使用的是较旧的 CodeBuild 映像时,才需要使用 pip3 install awscli --upgrade --user 来升级 AWS CLI。如果您使用的是最新映像版本,则可以删除该行。

要使用 pip 安装 Python 程序包,请执行以下操作:

pre_build: commands: - pip3 install awscli --upgrade --user - aws codeartifact login --tool pip --domain my_domain --domain-owner 111122223333 --repository my_repo build: commands: - pip install requests

要使用 twine 发布 Python 程序包,请执行以下操作:

pre_build: commands: - pip3 install awscli --upgrade --user - aws codeartifact login --tool twine --domain my_domain --domain-owner 111122223333 --repository my_repo build: commands: - twine upload --repository codeartifact mypackage

Windows

要使用 pip 安装 Python 程序包,请执行以下操作:

version: 0.2 phases: install: commands: - '[Net.ServicePointManager]::SecurityProtocol = "Tls12"; Invoke-WebRequest https://awscli.amazonaws.com/AWSCLIV2.msi -OutFile $env:TEMP/AWSCLIV2.msi' - Start-Process -Wait msiexec "/i $env:TEMP\AWSCLIV2.msi /quiet /norestart" pre_build: commands: - '&"C:\Program Files\Amazon\AWSCLIV2\aws" codeartifact login --tool pip --domain my_domain --domain-owner 111122223333 --repository my_repo' build: commands: - pip install requests

要使用 twine 发布 Python 程序包,请执行以下操作:

version: 0.2 phases: install: commands: - '[Net.ServicePointManager]::SecurityProtocol = "Tls12"; Invoke-WebRequest https://awscli.amazonaws.com/AWSCLIV2.msi -OutFile $env:TEMP/AWSCLIV2.msi' - Start-Process -Wait msiexec "/i $env:TEMP\AWSCLIV2.msi /quiet /norestart" pre_build: commands: - '&"C:\Program Files\Amazon\AWSCLIV2\aws" codeartifact login --tool twine --domain my_domain --domain-owner 111122223333 --repository my_repo' build: commands: - twine upload --repository codeartifact mypackage