Upload CodeBuild artifacts *if* they exist

℡╲_俬逩灬. 提交于 2021-01-24 09:39:06

问题


I have a simple CodeBuild spec that defines artifacts to be uploaded after tests run:

artifacts:
  files:
    - cypress/**/*.png
  discard-paths: yes

These artifacts are only generated if the test-action fails (a screenshot is captured of the failing test screen) and are being successfully uploaded to S3.

In the case that tests succeed, no .png files will be generated and the CodeBuild action fails:

[Container] 2018/09/21 20:06:34 Expanding cypress/**/*.png
[Container] 2018/09/21 20:06:34 Phase complete: UPLOAD_ARTIFACTS Success: false
[Container] 2018/09/21 20:06:34 Phase context status code: CLIENT_ERROR Message: no matching artifact paths found

Is there a way to conditionally upload files if they exist in the buildspec?

Alternatively I could use the s3 cli -- in which case I would need a way to easily access the bucket name and artifact key.


回答1:


To get around this, I'm creating a placeholder file that matches the glob pattern if build succeeds:

  post_build:
    commands:
      - if [ -z "$CODEBUILD_BUILD_SUCCEEDING" ]; then echo "Build failing, no need to create placeholder image"; else touch cypress/0.png; fi
artifacts:
  files:
    - cypress/**/*.png
  discard-paths: yes



回答2:


If any one still looking for solution base on tgk answer. in my cas I wanna upload the artifact only in master ENV , so other than master I create a place holder and upload in a TMP folder.

 post_build:
    commands:
      #creating a fake file to workaround fail upload in non prod build
      - |
        if [ "$ENV" = "master" ]; then
         export FOLDERNAME=myapp-$(date +%Y-%m-%d).$((BUILD_NUMBER))
        else
         touch myapp/0.tmp;
         export FOLDERNAME="TMP"
        fi
artifacts:
  files:
    - myapp/build/outputs/apk/prod/release/*.apk
    - myapp/*.tmp
  discard-paths: yes
  name: $FOLDERNAME


来源:https://stackoverflow.com/questions/52450561/upload-codebuild-artifacts-if-they-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!