Skip to content

Commit

Permalink
[ci] Skip unchanged jobs on master
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknovitski committed Apr 25, 2020
1 parent a715bff commit 120df7d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
28 changes: 26 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ orbs:
commands:
skip_unless_changed:
parameters:
workflow_name:
type: string
paths:
type: string
steps:
Expand All @@ -16,11 +18,24 @@ commands:
command: |
if [ "$CIRCLE_BRANCH" = "" ]; then
echo "Can't determine branch. Continuing the job."
elif [ "$CIRCLE_BRANCH" = "master" ]; then
echo "Branch is master. Continuing the job."
else
echo "Looking for previous successful ${CIRCLE_JOB} jobs for branch ${CIRCLE_BRANCH}"
LAST_SUCCESSFUL_COMMIT_ON_BRANCH=$(bin/circle-last-successful-commit "github/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}" "<< parameters.workflow_name >>/${CIRCLE_JOB}" "${CIRCLE_BRANCH}")
GIT_BASE_REVISION=<< pipeline.git.base_revision >>
if [[ "${LAST_SUCCESSFUL_COMMIT_ON_BRANCH}" == "" ]]; then
echo "No previous successful job detected for branch $CIRCLE_BRANCH"
echo "The job will be skipped unless this branch introduces relevant changes."
else
echo "Previous successful build found for commit $LAST_SUCCESSFUL_COMMIT_ON_BRANCH"
if TERM=xterm git show --pretty=%H -q $LAST_SUCCESSFUL_COMMIT_ON_BRANCH; then
GIT_BASE_REVISION=$LAST_SUCCESSFUL_COMMIT_ON_BRANCH
else
echo "Commit not found in repo, it might have been rebased out of the branch."
fi
fi
if [[ ${GIT_BASE_REVISION} == "" ]]; then
# If a job has never been run on master before, maybe this could happen
echo "No base revision found. Continuing the job."
Expand Down Expand Up @@ -370,6 +385,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: sdk
paths: yarn.lock packages
- update_submodules
- restore_yarn_cache
Expand All @@ -389,6 +405,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: sdk
paths: apps/bare-expo apps/test-suite yarn.lock packages
- update_submodules
- restore_yarn_cache
Expand All @@ -408,6 +425,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: client
paths: home yarn.lock
- update_submodules
- restore_yarn_cache
Expand All @@ -430,6 +448,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: client
paths: tools/expotools
# We can't use yarn_restore_and_install since it
# triggers the postinstall script which we don't
Expand All @@ -451,6 +470,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: sdk
paths: apps/bare-expo apps/test-suite yarn.lock packages Gemfile.lock
- bundle_install
- update_submodules
Expand Down Expand Up @@ -508,6 +528,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: client
paths: tools/expotools ios fastlane Gemfile.lock
- update_submodules
- git_lfs_pull
Expand Down Expand Up @@ -674,6 +695,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: client
paths: android fastlane tools-public Gemfile.lock yarn.lock
- update_submodules
- install_yarn
Expand Down Expand Up @@ -707,6 +729,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: client
paths: android fastlane tools/expotools Gemfile.lock yarn.lock
- install_yarn
- update_submodules
Expand Down Expand Up @@ -764,6 +787,7 @@ jobs:
steps:
- setup
- skip_unless_changed:
workflow_name: docs
paths: docs
- update_submodules
- install_puppeteer_dependencies
Expand Down
43 changes: 43 additions & 0 deletions bin/circle-last-successful-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env bash

set -eou pipefail

# arguments
PROJECT_SLUG=${1}
# split second parameter at '/'
WORKFLOW_NAME=${2/\/*/}
JOB_NAME=${2/*\//}
BRANCH=${3:-master}

# required tools
command -v curl &> /dev/null
command -v jq &> /dev/null

if [[ "${CIRCLE_TOKEN:-absent}" == "absent" ]]; then
>&2 echo "Environment variable CIRCLE_TOKEN is required."
exit 0
fi

circleCurl() {
curl -Ss -X GET \
-H 'Accept: application/json' \
-H "Circle-Token: $CIRCLE_TOKEN" \
"$@"
}

PIPELINES=$(circleCurl --data-urlencode "branch=${BRANCH}" "https://circleci.com/api/v2/project/${PROJECT_SLUG}/pipeline" | jq '.items')

LAST_SUCCESSFUL_PIPELINE_ID=

for pipeline_id in $(jq -r '.[].id' <<< "${PIPELINES}"); do
workflow_id=$(circleCurl "https://circleci.com/api/v2/pipeline/${pipeline_id}/workflow" | jq -r ".items | .[] | select(.name == \"${WORKFLOW_NAME}\" and .status != \"not_run\") | .id")
if [[ "${workflow_id}" != '' ]]; then
successful_job=$(circleCurl "https://circleci.com/api/v2/workflow/${workflow_id}/job" | jq -r ".items | .[] | select(.name == \"${JOB_NAME}\" and .status == \"success\")")
if [[ "${successful_job}" != '' ]]; then
LAST_SUCCESSFUL_PIPELINE_ID=$pipeline_id
break
fi
fi
done

jq -r ".[] | select(.id == \"${LAST_SUCCESSFUL_PIPELINE_ID}\") | .vcs.revision " <<< "${PIPELINES}"

0 comments on commit 120df7d

Please sign in to comment.