【问题标题】:Installing Linux Homebrew in AWS CodeBuild container在 AWS CodeBuild 容器中安装 Linux Homebrew
【发布时间】:2020-05-06 02:57:11
【问题描述】:

我有一个 AWS CodeBuild 项目,我需要在我的 CodeBuild 容器中调用 SAM CLI。在build 阶段,我添加了一个安装 Linux Homebrew 的命令,以便我可以根据文档从 AWS Homebrew 水龙头安装 SAM CLI。

但是,在运行此命令时,我收到以下错误。

[Container] 2020/01/20 05:29:26 Running command bash -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
-e:196: warning: Insecure world writable dir /go/bin in PATH, mode 040777
Don't run this as root!

[Container] 2020/01/20 05:29:28 Command did not exit successfully bash -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" exit status 1
[Container] 2020/01/20 05:29:28 Phase complete: BUILD State: FAILED
[Container] 2020/01/20 05:29:28 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: bash -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)". Reason: exit status 1

我正在使用 AWS 提供的 Ubuntu 标准“3.0”构建环境。

buildspec.yml

version: 0.2
phases:
  install:
    runtime-versions:
      docker: 18
      nodejs: 10
      python: 3.8
  build:
    commands:
      - echo Installing SAM CLI
      - sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
      - brew tap aws/tap
      - brew install aws-sam-cli
      - sam version

问题:如何在 AWS CodeBuild 项目中成功安装 Linux Homebrew?

【问题讨论】:

  • 我认为sam 已经在 Codebuild 容器中(使用python: 3.8 图像确定),您可以通过brew 跳过安装aws-sam-cli,只需使用sam --version 命令检查sam 版本

标签: amazon-web-services aws-codebuild aws-serverless


【解决方案1】:

第一个也是推荐的选项是使用 CodeBuild 带来您自己的构建映像,例如使用 [1] 这是包含 aws sam cli 的图像。

第二个也是更困难的选择是自己安装 SAM CLI。由于 brew 不能以任何方式用作 root 并且 CodeBuild 构建容器以 root 身份运行,这变得很棘手。以下是我测试过的构建规范,可以确认将安装 aws sam cli:

构建规范:

version: 0.2

phases:
    install:
        commands:
            - curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh > /tmp/install.sh
            - cat /tmp/install.sh
            - chmod +x /tmp/install.sh
            - useradd -m brewuser
            - echo "brewuser:brewuser" | chpasswd 
            - adduser brewuser sudo
            - /bin/su -c /tmp/install.sh - brewuser
            - /bin/su -c '/home/brewuser/.linuxbrew/bin/brew tap aws/tap' - brewuser
            - /bin/su -c '/home/brewuser/.linuxbrew/bin/brew install aws-sam-cli' - brewuser

build:
    commands:
        - PATH=/home/brewuser/.linuxbrew/bin:$PATH
        - sam --version

注意:根据我的测试,Python 3.8 不包含 sam cli。

【讨论】:

    【解决方案2】:

    在@shariqmaws 回答的基础上,我使用了一个包含 AWS SAM 和 Node.js 10 的公共 ECR 映像:public.ecr.aws/sam/build-nodejs10.x:latest。你可以在这里找到更多信息:https://gallery.ecr.aws/sam/build-nodejs10.x

    CloudFormation 模板:

      CodeBuildIntegrationProject:
        Type: AWS::CodeBuild::Project
        Properties:
          Artifacts:
            Type: CODEPIPELINE
          Environment:
            Type: LINUX_CONTAINER
            Image: public.ecr.aws/sam/build-nodejs10.x:latest
            ImagePullCredentialsType: CODEBUILD
            ComputeType: BUILD_GENERAL1_SMALL
          LogsConfig:
            CloudWatchLogs:
              Status: ENABLED
          Name: !Sub ${GitHubRepositoryName}-integration
          ServiceRole: !Sub ${CodeBuildRole.Arn}
          Source:
            Type: CODEPIPELINE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2018-03-16
      • 2017-06-17
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多