amazon-web-servicesgitaws-codebuildbuildspec

CodeBuild, get the list of folders that have changed files inside them buildspec.yml


Im trying to make my first build on aws and this is my buildspec.yml, I was just testing if my command does work on codebuild

version: 0.2

env:
  git-credential-helper: yes
phases:
  install:
    runtime-versions:
      nodejs: 16
  pre_build:
    commands:
      - aws codeartifact login ...
  build:
    commands:
      - changed_folders=$(git diff --dirstat=files,0 HEAD~1 | awk '{print $2}' | xargs -I {} dirname {} | awk -F '/' '{print $1}' | sort | uniq)
      - echo $changed_folders

this command works on local but when it is building

git diff --dirstat=files,0 HEAD~1 | awk '{print $2}' | xargs -I {} dirname {} | awk -F '/' '{print $1}' | sort | uniq

theres an error saying

fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I tried changing the HEAD~1 with $CODEBUILD_WEBHOOK_HEAD_REF it works but im getting an empty result when I echo using echo $changed_folders

Im using github as my repository

Reference: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html


Solution

  • Normally Codebuild Systems (Jenkins, Github Action, Codebuild) fetch only last commit of (triggerred ref only), so that when you run some git commits about git history you will get empty. So that by default build systems clone source code like git clone --depth 1 -b <branch> <repo_url>.

    AWS announced you can fetch full history clone in codebuild.

    When you enable artifact option "Full clone" on your pipeline, you will be able to get changed_files