|
1 |
| -jobs: |
2 |
| - verify-ci-should-run: |
3 |
| - docker: |
4 |
| - - image: cimg/node:current |
5 |
| - resource_class: small |
6 |
| - steps: |
| 1 | +orbs: |
| 2 | + continuation: circleci/[email protected] |
| 3 | + circleci-cli: circleci/[email protected] |
| 4 | +setup: true |
| 5 | +version: 2.1 |
| 6 | + |
| 7 | +workflows: |
| 8 | + setup-workflow: |
| 9 | + jobs: |
| 10 | + - pack-workflows |
| 11 | + - launch-primary-workflow: |
| 12 | + requires: |
| 13 | + - pack-workflows |
| 14 | + |
| 15 | +commands: |
| 16 | + persist: |
| 17 | + steps: |
| 18 | + - persist_to_workspace: |
| 19 | + root: . |
| 20 | + paths: |
| 21 | + - .circleci/packed |
| 22 | + persist-to-workspace-and-exit-early: |
| 23 | + steps: |
| 24 | + - run: |
| 25 | + name: Check cache status and exit early if needed |
| 26 | + command: | |
| 27 | + if [[ -d ".circleci/packed" ]] && [[ "$(ls -A .circleci/packed 2>/dev/null)" ]]; then |
| 28 | + echo "📦 Cache hit - packed configurations exist" |
| 29 | + echo "✅ Persisting packed configurations to workspace and halting job" |
| 30 | + echo "CACHE_HIT=true" >> $BASH_ENV |
| 31 | + else |
| 32 | + echo "📦 Cache miss - continuing with packaging" |
| 33 | + echo "CACHE_HIT=false" >> $BASH_ENV |
| 34 | + mkdir -p .circleci/packed # persist empty dir to workspace |
| 35 | + fi |
| 36 | + - persist |
| 37 | + - run: |
| 38 | + name: End job if cache hit |
| 39 | + command: | |
| 40 | + if [[ "$CACHE_HIT" == "true" ]] && [[ "$(ls -A .circleci/packed 2>/dev/null)" ]]; then |
| 41 | + echo "✅ Cache hit - packed configurations exist" |
| 42 | + circleci-agent step halt |
| 43 | + else |
| 44 | + echo "📦 Cache miss - continuing with packaging" |
| 45 | + fi |
| 46 | + pack-workflows: |
| 47 | + steps: |
| 48 | + - circleci-cli/install |
| 49 | + - run: |
| 50 | + name: Pack workflows |
| 51 | + command: | |
| 52 | + ./scripts/pack-ci.sh --all |
| 53 | + validate-workflows: |
| 54 | + steps: |
| 55 | + - run: |
| 56 | + name: Validate workflows |
| 57 | + command: | |
| 58 | + ./scripts/pack-ci.sh --validate --pack=false |
| 59 | +
|
| 60 | + # Retrieving and saving packed workflows based on the checksum of the hash |
| 61 | + # of the yml files in ./.circleci/src |
| 62 | + calc-src-checksum: |
| 63 | + steps: |
| 64 | + - run: |
| 65 | + name: Generate config checksum for cache lookup |
| 66 | + # this is saved to a file, so that we can use {{ checksum }} - which |
| 67 | + # can only operate on the contents of a single file, not a list of |
| 68 | + # files or a directory. |
| 69 | + command: | |
| 70 | + find .circleci/src -name "*.yml" -type f | \ |
| 71 | + sort | \ |
| 72 | + xargs cat | \ |
| 73 | + md5sum | \ |
| 74 | + cut -d' ' -f1 > /tmp/config_checksum.txt |
| 75 | + restore-src-checksum-cache: |
| 76 | + steps: |
| 77 | + - calc-src-checksum |
| 78 | + - restore_cache: |
| 79 | + keys: |
| 80 | + - circleci-packed-pipelines-{{ checksum "/tmp/config_checksum.txt" }} |
| 81 | + paths: |
| 82 | + - .circleci/packed |
| 83 | + save-src-checksum-cache: |
| 84 | + steps: |
| 85 | + - calc-src-checksum |
| 86 | + - save_cache: |
| 87 | + key: circleci-packed-pipelines-{{ checksum "/tmp/config_checksum.txt" }} |
| 88 | + paths: |
| 89 | + - .circleci/packed |
| 90 | + |
| 91 | + cancel-draft-prs: |
| 92 | + steps: |
| 93 | + - when: |
| 94 | + condition: |
| 95 | + not: |
| 96 | + or: |
| 97 | + - equal: [<<pipeline.trigger_source>>, 'api'] |
| 98 | + - equal: [<<pipeline.git.branch>>, 'develop'] |
| 99 | + - matches: |
| 100 | + pattern: /^release\/.*/ |
| 101 | + value: <<pipeline.git.branch>> |
| 102 | + steps: |
7 | 103 | - run:
|
| 104 | + name: Cancel CI |
8 | 105 | command: |
|
9 |
| - # run CI when manually triggers via CircleCi Dashboard |
10 |
| - if [ <<pipeline.trigger_source>> == 'api' ]; then |
11 |
| - echo "Always run CI when manually triggered from the UI." |
12 |
| - exit 0 |
13 |
| - fi |
14 |
| -
|
15 |
| - if [[ "$CIRCLE_BRANCH" == "develop" || "$CIRCLE_BRANCH" == "release/"* ]]; then |
16 |
| - echo "Always run CI for develop and for release candidate branches." |
17 |
| - exit 0 |
18 |
| - fi |
| 106 | + cancel_build() { |
| 107 | + circleci-agent step halt |
| 108 | + } |
19 | 109 |
|
20 |
| - LAST_COMMIT_MESSAGE=$(curl --silent "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commits/${CIRCLE_BRANCH}" | jq '.commit.message') |
| 110 | + if [ ! -z "${CIRCLE_PULL_REQUEST##*/}" ]; then |
| 111 | + DRAFT=$(curl --silent "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PULL_REQUEST##*/}" | jq '.draft') |
21 | 112 |
|
22 |
| - if [[ "$LAST_COMMIT_MESSAGE" =~ "run ci" ]]; then |
23 |
| - echo "Always run CI when the commit message includes 'run ci'." |
24 |
| - exit 0 |
| 113 | + if [[ "${DRAFT}" == true ]]; then |
| 114 | + echo "Skipping CI; PR is in draft - to trigger CI , click the 'Trigger Pipeline' button in the CircleCI UI." |
| 115 | + cancel_build |
25 | 116 | fi
|
26 | 117 |
|
27 |
| - cancel_build () { |
28 |
| - echo "Canceling the CI build..." |
29 |
| - circleci-agent step halt |
30 |
| - } |
| 118 | + echo "Always run CI for PR that is ready for review." |
| 119 | + exit 0 |
| 120 | + fi |
31 | 121 |
|
32 |
| - TRIGGER_INSTRUCTIONS="to trigger CI , include 'run ci' in the commit message or click the 'Trigger Pipeline' button in the CircleCI UI." |
| 122 | + # otherwise, the build is not a PR so we cancel it |
| 123 | + cancel_build |
33 | 124 |
|
34 |
| - if [ ! -z "${CIRCLE_PULL_REQUEST##*/}" ]; then |
35 |
| - DRAFT=$(curl --silent "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PULL_REQUEST##*/}" | jq '.draft') |
| 125 | +jobs: |
| 126 | + pack-workflows: |
| 127 | + docker: |
| 128 | + - image: cimg/base:stable |
| 129 | + resource_class: small |
| 130 | + steps: |
| 131 | + - cancel-draft-prs |
| 132 | + - checkout |
| 133 | + - restore-src-checksum-cache |
| 134 | + - persist-to-workspace-and-exit-early |
| 135 | + - pack-workflows |
| 136 | + - save-src-checksum-cache |
| 137 | + - persist |
36 | 138 |
|
37 |
| - if [[ "${DRAFT}" == true ]]; then |
38 |
| - echo "Skipping CI; PR is in draft - $TRIGGER_INSTRUCTIONS" |
39 |
| - cancel_build |
40 |
| - fi |
| 139 | + launch-primary-workflow: |
| 140 | + docker: |
| 141 | + - image: cimg/base:stable |
| 142 | + resource_class: small |
| 143 | + steps: |
| 144 | + - cancel-draft-prs |
| 145 | + - attach_workspace: |
| 146 | + at: . |
| 147 | + - run: |
| 148 | + name: Check for primary workflow definition |
| 149 | + command: | |
| 150 | + if [ ! -f .circleci/packed/pipeline.yml ]; then |
| 151 | + echo "❌ Primary workflow definition not found in .circleci/packed/pipeline.yml!" |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | + - continuation/continue: |
| 155 | + configuration_path: .circleci/packed/pipeline.yml |
41 | 156 |
|
42 |
| - echo "Always run CI for PR that is ready for review." |
43 |
| - exit 0 |
44 |
| - fi |
45 | 157 |
|
46 |
| - echo "Skipping CI; branch in progress - $TRIGGER_INSTRUCTIONS" |
47 |
| - cancel_build |
48 |
| - name: Verify CI should run |
49 |
| - - checkout |
50 |
| - - continuation/continue: |
51 |
| - configuration_path: .circleci/workflows.yml |
52 |
| -orbs: |
53 |
| - continuation: circleci/[email protected] |
54 |
| -setup: true |
55 |
| -version: 2.1 |
56 |
| -workflows: |
57 |
| - setup-workflow: |
58 |
| - jobs: |
59 |
| - - verify-ci-should-run |
| 158 | + |
60 | 159 |
|
0 commit comments