AWS CodeBuild 本地缓存无法实际缓存?

Posted

技术标签:

【中文标题】AWS CodeBuild 本地缓存无法实际缓存?【英文标题】:AWS CodeBuild local cache failing to actually cache? 【发布时间】:2020-03-06 16:31:38 【问题描述】:

我一直在尝试让 AWS CodeBuild 的本地缓存正常工作,但在我的一生中,我什至无法让最基本的缓存正常工作。我的最终目标是缓存 Gradle 工件,正如 here 所讨论的那样。

但是因为我无法让它工作,我尝试了一个更简单的测试,我尝试使用文件counter.txt 缓存目录/root/foo,我会在每次构建时递增。我的期望是,如果我在彼此的几分钟内运行后续构建,我会在日志中看到“2”、“3”等。但现实情况是,尽管建立了符号链接,但下一个构建永远不会看到以前的 counter.txt 文件,这表明我有些东西非常损坏。

谁能确认他们的本地缓存确实在 CodeBuild 中工作?我开始怀疑该功能目前是否已损坏!还是我完全误解了它应该做什么?

buildspec.yml:

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto8
  build:
    commands:
      - pwd
      - ls -l /root/
      - ls -l /root/foo/
      - ./cache-test.sh
      - ls -l /root/
      - ls -l /root/foo/

cache:
  paths:
    - '/root/foo/*'

cache-test.sh:

#!/bin/bash
if [ -d "/root/foo" ]; then
  C=$(cat /root/foo/count.txt)
  C=$((C + 1))
  echo "*********************************"
  echo "*********************************"
  echo "Incrementing counter to $C"
  echo $C > /root/foo/count.txt
  echo "*********************************"
  echo "*********************************"
else
  mkdir /root/foo
  echo "*********************************"
  echo "*********************************"
  echo "File not found, starting count at 1"
  echo "*********************************"
  echo "*********************************"
  echo 1 > /root/foo/count.txt
fi

CodeBuild 输出:(即使在快速连续执行时输出相同)

[Container] 2019/11/10 22:35:08 Waiting for agent ping 
[Container] 2019/11/10 22:35:10 Waiting for DOWNLOAD_SOURCE 
[Container] 2019/11/10 22:35:10 Phase is DOWNLOAD_SOURCE 
[Container] 2019/11/10 22:35:10 CODEBUILD_SRC_DIR=/codebuild/output/src905503483/src 
[Container] 2019/11/10 22:35:10 YAML location is /codebuild/output/src905503483/src/buildspec.yml 
[Container] 2019/11/10 22:35:10 No commands found for phase name: INSTALL 
[Container] 2019/11/10 22:35:10 Processing environment variables 
[Container] 2019/11/10 22:35:10 Moving to directory /codebuild/output/src905503483/src 
[Container] 2019/11/10 22:35:10 MkdirAll: /codebuild/local-cache/custom/de68c9f22ae028d4e4dfb0d11bbb481053d28b1373db0d6a56ebee0416bf13b2/root/foo 
[Container] 2019/11/10 22:35:10 Symlinking: /root/foo => /codebuild/local-cache/custom/de68c9f22ae028d4e4dfb0d11bbb481053d28b1373db0d6a56ebee0416bf13b2/root/foo 
[Container] 2019/11/10 22:35:10 Registering with agent 
[Container] 2019/11/10 22:35:10 Phases found in YAML: 2 
[Container] 2019/11/10 22:35:10  BUILD: 6 commands 
[Container] 2019/11/10 22:35:10  INSTALL: 0 commands 
[Container] 2019/11/10 22:35:10 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED 
[Container] 2019/11/10 22:35:10 Phase context status code:  Message:  
[Container] 2019/11/10 22:35:11 Entering phase INSTALL 
[Container] 2019/11/10 22:35:11 Running command echo "Installing corretto(OpenJDK) version 8 ..." 
Installing corretto(OpenJDK) version 8 ... 

[Container] 2019/11/10 22:35:11 Running command export JAVA_HOME="$JAVA_8_HOME" 

[Container] 2019/11/10 22:35:11 Running command export JRE_HOME="$JRE_8_HOME" 

[Container] 2019/11/10 22:35:11 Running command export JDK_HOME="$JDK_8_HOME" 

[Container] 2019/11/10 22:35:11 Running command for tool_path in "$JAVA_8_HOME"/bin/* "$JRE_8_HOME"/bin/*; 
 do tool=`basename "$tool_path"`; 
  if [ $tool != 'java-rmi.cgi' ]; 
  then 
   rm -f /usr/bin/$tool /var/lib/alternatives/$tool \ 
    && update-alternatives --install /usr/bin/$tool $tool $tool_path 20000; 
  fi; 
done 

[Container] 2019/11/10 22:35:11 Phase complete: INSTALL State: SUCCEEDED 
[Container] 2019/11/10 22:35:11 Phase context status code:  Message:  
[Container] 2019/11/10 22:35:11 Entering phase PRE_BUILD 
[Container] 2019/11/10 22:35:11 Phase complete: PRE_BUILD State: SUCCEEDED 
[Container] 2019/11/10 22:35:11 Phase context status code:  Message:  
[Container] 2019/11/10 22:35:11 Entering phase BUILD 
[Container] 2019/11/10 22:35:11 Running command pwd 
/codebuild/output/src905503483/src 

[Container] 2019/11/10 22:35:11 Running command ls -l /root/ 
total 4 
lrwxrwxrwx 1 root root 103 Nov 10 22:35 foo -> /codebuild/local-cache/custom/de68c9f22ae028d4e4dfb0d11bbb481053d28b1373db0d6a56ebee0416bf13b2/root/foo 

[Container] 2019/11/10 22:35:11 Running command ls -l /root/foo/ 
total 0 

[Container] 2019/11/10 22:35:11 Running command ./cache-test.sh 
cat: /root/foo/count.txt: No such file or directory 
********************************* 
********************************* 
Incrementing counter to 1 
********************************* 
********************************* 

[Container] 2019/11/10 22:35:11 Running command ls -l /root/ 
total 4 
lrwxrwxrwx 1 root root 103 Nov 10 22:35 foo -> /codebuild/local-cache/custom/de68c9f22ae028d4e4dfb0d11bbb481053d28b1373db0d6a56ebee0416bf13b2/root/foo 

[Container] 2019/11/10 22:35:11 Running command ls -l /root/foo/ 
total 4 
-rw-r--r-- 1 root root 2 Nov 10 22:35 count.txt 

[Container] 2019/11/10 22:35:11 Phase complete: BUILD State: SUCCEEDED 
[Container] 2019/11/10 22:35:11 Phase context status code:  Message:  
[Container] 2019/11/10 22:35:11 Entering phase POST_BUILD 
[Container] 2019/11/10 22:35:11 Phase complete: POST_BUILD State: SUCCEEDED 
[Container] 2019/11/10 22:35:11 Phase context status code:  Message:  

CodeBuild 项目 JSON:


    "projects": [
        
            "name": "test-project",
            "arn": "arn:aws:codebuild:us-east-2:xxx:project/xxx",
            "source": 
                "type": "CODEPIPELINE",
                "insecureSsl": false
            ,
            "secondarySourceVersions": [],
            "artifacts": 
                "type": "CODEPIPELINE",
                "name": "test-project",
                "packaging": "NONE",
                "encryptionDisabled": false
            ,
            "secondaryArtifacts": [],
            "cache": 
                "type": "LOCAL",
                "modes": [
                    "LOCAL_SOURCE_CACHE",
                    "LOCAL_CUSTOM_CACHE"
                ]
            ,
            "environment": 
                "type": "LINUX_CONTAINER",
                "image": "aws/codebuild/amazonlinux2-x86_64-standard:1.0",
                "computeType": "BUILD_GENERAL1_SMALL",
                "environmentVariables": [],
                "privilegedMode": false,
                "imagePullCredentialsType": "CODEBUILD"
            ,
            "serviceRole": "arn:aws:iam::xxx:role/service-role/xxx",
            "timeoutInMinutes": 60,
            "queuedTimeoutInMinutes": 480,
            "encryptionKey": "arn:aws:kms:us-east-2:xxx:alias/aws/s3",
            "tags": [],
            "created": 1573364156.631,
            "lastModified": 1573423155.674,
            "badge": 
                "badgeEnabled": false
            ,
            "logsConfig": 
                "cloudWatchLogs": 
                    "status": "ENABLED",
                    "groupName": "xxx",
                    "streamName": "xxx"
                ,
                "s3Logs": 
                    "status": "DISABLED",
                    "encryptionDisabled": false
                
            
        
    ],
    "projectsNotFound": []

【问题讨论】:

看来我不是唯一一个:forums.aws.amazon.com/thread.jspa?threadID=312569&tstart=0 【参考方案1】:

文档并不完全清楚,但 AWS CodeBuild 本地缓存可以only cache directories(截至撰写本文时)。这有点令人困惑,因为 AWS CodeBuild 构建规范 specification allows the path to be individual files or a wildcard,但实际上指定文件会产生错误。

Unable to initialize cache download: only directories can be cached locally: ...

在您的示例中,您使用将缓存指定为

cache:
  paths:
    - '/root/foo/*'

其中 * 将引用 foo 中的所有单个文件和文件夹,但只会缓存文件夹。

指定整个目录应该可以工作

cache:
  paths:
    - /root/foo/

【讨论】:

【参考方案2】:

我一直在尝试让缓存自己工作,但效果有限。

并非来自任何公共来源,但以下是一些观察结果:

缓存只有在构建时间超过 5 分钟时才可用。

如果新构建成功放置在同一构建主机上,则可以使用缓存。

如果新构建在上次构建的 5-15 分钟内运行,则可以使用缓存。缓存可能会根据上次构建时间保持可用,最长为 15 分钟。

尽管构建时间超过 5 分钟,但缓存可能并不总是有效,这可能是因为构建放置在不同的构建主机上。

此外,如果缓存将新构建加速到 5 分钟以下,则不会缓存该构建,从而导致后续未命中。

虽然我相信 CodeBuild 工程师有充分的理由以这种方式设计它,但在我看来,上述限制使这种本地缓存功能的使用受到了限制。

【讨论】:

超级有趣的观察。谢谢分享! 谢谢。如果构建之间的时间少于 15 分钟,我还经历过缓存消失。就我而言,它使缓存毫无意义。 AWS 似乎没有在任何地方记录这一点。 AWS Codebuild 工程师已在此处确认本地缓存上有一个 TTL ***.com/a/56711832/4985580

以上是关于AWS CodeBuild 本地缓存无法实际缓存?的主要内容,如果未能解决你的问题,请参考以下文章

AWS CodeBuild 不适用于 Yarn 工作区

Puppeteer 无法在 AWS CodeBuild 上运行 Chrome

无法将更改从 AWS CodeBuild 推送到 AWS CodeCommit

无法在 AWS CodeBuild 映像中使用 amazon-linux-extras

ImportError:无法从 AWS CodeBuild 中的“botocore.docs.bcdoc”导入名称“docevents”

AWS codebuild 没有构建我的 github 拉取请求