labs42io / circleci-monorepo

An example of monorepo with CircleCI using conditional workflows and pipeline parameters.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changes in master never get detected

joseph-skytv opened this issue · comments

Hi

I got a weird issue where the script works perfectly fine when I am on a branch, all changes were detected correctly.

But then when I merge the branch back to master, the script doesn't seem to detect any changes hence master is not being built.

All I got is this, could you please help?

`There are no completed CI builds in branch master.

Searching for CI builds in branch 'master' ...

No CI builds for branch master. Using master.

Searching for changes since commit [cd3bf62] ...

Workflows currently in failed status: ().

[-] cart-web

[-] myaccount-web

[-] shared

[-] style

No changes detected in packages. Skip triggering workflows.`

@joseph-skytv Did you find a fix for this? If not i'm going to try take a look at this tomorrow if i get time.

Thanks @Ashwalls93 , no I haven't found a fix for this. But I found a different approach which is using Lerna to detect the changes and calling the API to trigger the job which has been working great for me.

Thanks :)

Hey @joseph-skytv. I found the problem. When a PR is merged onto master then LAST_COMPLETED_BUILD_SHA has the same value as CIRCLE_SHA1 on line 78 of circle_trigger.sh.

This leads to the if condition on line 80 evaluating to true and skipping the else clause that would normally detect the change asLATEST_COMMIT_SINCE_LAST_BUILD has a value of null.

While it's definitely not the nicest fix i got around the issue by modifying the if statement to the following:

for PACKAGE in ${PACKAGES[@]}
do
  PACKAGE_PATH=${ROOT#.}/$PACKAGE
  LATEST_COMMIT_SINCE_LAST_BUILD=$(git log -1 $LAST_COMPLETED_BUILD_SHA..$CIRCLE_SHA1 --format=format:%H --full-diff ${PACKAGE_PATH#/})

  if [[ -z "$LATEST_COMMIT_SINCE_LAST_BUILD" ]]; then
    INCLUDED=0
    for FAILED_BUILD in ${FAILED_WORKFLOWS[@]}
    do
      if [[ "$PACKAGE" == "$FAILED_BUILD" ]]; then
        INCLUDED=1
        PARAMETERS+=", \"$PACKAGE\":true"
        COUNT=$((COUNT + 1))
        echo -e "\e[36m  [+] ${PACKAGE} \e[21m (included because failed since last build)\e[0m"
        break
      fi
    done

    if [[ "$INCLUDED" == "0" ]]; then
      echo -e "\e[90m  [-] $PACKAGE \e[0m"
    fi
  elif [[ "$LAST_COMPLETED_BUILD_SHA" == "$CIRCLE_SHA1" ]] && [[ $CIRCLE_BRANCH == "master" ]]; then
    PARAMETERS+=", \"$PACKAGE\":true"
    COUNT=$((COUNT + 1))
    echo -e "\e[36m  [+] ${PACKAGE} \e[21m (Merge detected, changed in [${LATEST_COMMIT_SINCE_LAST_BUILD:0:7}])\e[0m"
  else
    PARAMETERS+=", \"$PACKAGE\":true"
    COUNT=$((COUNT + 1))
    echo -e "\e[36m  [+] ${PACKAGE} \e[21m (changed in [${LATEST_COMMIT_SINCE_LAST_BUILD:0:7}])\e[0m"
  fi
done

There's definitely better solutions and I haven't full tested this, but it gets the workflows triggering on master when a PR has been merged. I've yet to test if a direct change on master within one of the packages would trigger the workflow but as I can't push directly to master i'm not too concerned for my use case.

Glad you for found another solution that worked for you but just in case you were curious i thought i'd leave this here 😄