|
| 1 | +description: Cancel the current job based on various conditions |
| 2 | +parameters: |
| 3 | + run_if_tag_in_commit: |
| 4 | + description: Cancel the job if this tag is not in the current commit message |
| 5 | + type: string |
| 6 | + default: "" |
| 7 | + cancel_if_tag_in_commit: |
| 8 | + description: Cancel the job if this tag is in the current commit message |
| 9 | + type: string |
| 10 | + default: "" |
| 11 | + cancel_if_tag_in_pr_title: |
| 12 | + description: Cancel the job if this tag is in the pull request title |
| 13 | + type: string |
| 14 | + default: "" |
| 15 | + run_if_paths_changed: |
| 16 | + description: Cancel the job if these file paths did not change (space-separated list) |
| 17 | + type: string |
| 18 | + default: "" |
| 19 | + run_only_if_all_paths_changed_match_pattern: |
| 20 | + description: Cancel the job if any files changed besides the ones matched by this pattern (grep regex) |
| 21 | + type: string |
| 22 | + default: "" |
| 23 | + cancel_if_paths_changed: |
| 24 | + description: Cancel the job if these file paths did change (space-separated list) |
| 25 | + type: string |
| 26 | + default: "" |
| 27 | + cancel_only_if_all_paths_changed_match_pattern: |
| 28 | + description: Cancel the job if the only files changed match this pattern (grep regex) |
| 29 | + type: string |
| 30 | + default: "" |
| 31 | + gitref: |
| 32 | + default: $CIRCLE_BRANCH |
| 33 | + type: string |
| 34 | + method: |
| 35 | + description: | |
| 36 | + Method of cancellation; Either cancel the job or just halt the step so the job |
| 37 | + still succeeds |
| 38 | + type: enum |
| 39 | + enum: |
| 40 | + - cancel |
| 41 | + - halt |
| 42 | + default: cancel |
| 43 | +steps: |
| 44 | + - run: |
| 45 | + name: Set git diff commit range |
| 46 | + command: | |
| 47 | + COMMIT_RANGE="origin/master..origin/<< parameters.gitref >>" |
| 48 | + echo "export COMMIT_RANGE=\"$COMMIT_RANGE\"" >> $BASH_ENV |
| 49 | + echo $COMMIT_RANGE |
| 50 | + - when: |
| 51 | + condition: << parameters.run_if_tag_in_commit >> |
| 52 | + steps: |
| 53 | + - run: |
| 54 | + name: | |
| 55 | + Cancel the job if << parameters.run_if_tag_in_commit >> is not in the current |
| 56 | + commit message (case insensitive) |
| 57 | + command: | |
| 58 | + shopt -s nocasematch |
| 59 | + MESSAGE="$(git log -1 --pretty=%B)" |
| 60 | + if [[ "$MESSAGE" != *'<< parameters.run_if_tag_in_commit >>'* ]]; then |
| 61 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 62 | + fi |
| 63 | + shopt -u nocasematch |
| 64 | + - when: |
| 65 | + condition: << parameters.cancel_if_tag_in_commit >> |
| 66 | + steps: |
| 67 | + - run: |
| 68 | + name: | |
| 69 | + Cancel the job if << parameters.cancel_if_tag_in_commit >> is in the current |
| 70 | + commit message (case insensitive) |
| 71 | + command: | |
| 72 | + shopt -s nocasematch |
| 73 | + MESSAGE="$(git log -1 --pretty=%B)" |
| 74 | + if [[ "$MESSAGE" == *'<< parameters.cancel_if_tag_in_commit >>'* ]]; then |
| 75 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 76 | + fi |
| 77 | + shopt -u nocasematch |
| 78 | + - when: |
| 79 | + condition: << parameters.cancel_if_tag_in_pr_title >> |
| 80 | + steps: |
| 81 | + - run: |
| 82 | + name: | |
| 83 | + Cancel the job if << parameters.cancel_if_tag_in_pr_title >> is in the pull request |
| 84 | + title (case insensitive) |
| 85 | + command: | |
| 86 | + shopt -s nocasematch |
| 87 | + PR_NUMBER="$(echo "$CIRCLE_PULL_REQUEST" | sed 's/.*\/pull\///')" |
| 88 | + URL="https://api.github.com/repos/NarrativeScience/talos/pulls/$PR_NUMBER" |
| 89 | + PR_TITLE="$(curl --user "$GITHUB_USERNAME:$GITHUB_PASSWORD" "$URL" | jq '.title')" |
| 90 | + if [[ "$PR_TITLE" == *'<< parameters.cancel_if_tag_in_pr_title >>'* ]]; then |
| 91 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 92 | + fi |
| 93 | + shopt -u nocasematch |
| 94 | + - when: |
| 95 | + condition: << parameters.run_if_paths_changed >> |
| 96 | + steps: |
| 97 | + - run: |
| 98 | + name: Cancel the job if << parameters.run_if_paths_changed >> did not change |
| 99 | + command: | |
| 100 | + FILES="$(git diff --name-only $COMMIT_RANGE << parameters.run_if_paths_changed >>)" |
| 101 | + if [[ ${#FILES} -eq 0 ]]; then |
| 102 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 103 | + fi |
| 104 | + - when: |
| 105 | + condition: << parameters.run_only_if_all_paths_changed_match_pattern >> |
| 106 | + steps: |
| 107 | + - run: |
| 108 | + name: Cancel the job if any files beside << parameters.run_only_if_all_paths_changed_match_pattern >> changed |
| 109 | + command: | |
| 110 | + GREP_PATTERN="<< parameters.run_only_if_all_paths_changed_match_pattern >>" |
| 111 | + FILES="$(git --no-pager diff --name-only $COMMIT_RANGE)" |
| 112 | + if [[ ${#FILES} -gt 0 ]]; then |
| 113 | + FILTERED_FILES=$(echo "$FILES" | grep "$GREP_PATTERN") || true |
| 114 | + if [[ "$FILES" != "$FILTERED_FILES" ]]; then |
| 115 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 116 | + fi |
| 117 | + fi |
| 118 | + - when: |
| 119 | + condition: << parameters.cancel_if_paths_changed >> |
| 120 | + steps: |
| 121 | + - run: |
| 122 | + name: Cancel the job if << parameters.cancel_if_paths_changed >> did change |
| 123 | + command: | |
| 124 | + FILES="$(git diff --name-only $COMMIT_RANGE << parameters.cancel_if_paths_changed >>)" |
| 125 | + if [[ ${#FILES} -gt 0 ]]; then |
| 126 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 127 | + fi |
| 128 | + - when: |
| 129 | + condition: << parameters.cancel_only_if_all_paths_changed_match_pattern >> |
| 130 | + steps: |
| 131 | + - run: |
| 132 | + name: Cancel the job if the only files changed match << parameters.cancel_only_if_all_paths_changed_match_pattern >> |
| 133 | + command: | |
| 134 | + GREP_PATTERN="<< parameters.cancel_only_if_all_paths_changed_match_pattern >>" |
| 135 | + FILES="$(git --no-pager diff --name-only $COMMIT_RANGE)" |
| 136 | + if [[ ${#FILES} -gt 0 ]]; then |
| 137 | + FILTERED_FILES=$(echo "$FILES" | grep -E "$GREP_PATTERN") || true |
| 138 | + if [[ "$FILES" == "$FILTERED_FILES" ]]; then |
| 139 | + echo 'export CANCEL_JOB=1' >> $BASH_ENV |
| 140 | + fi |
| 141 | + fi |
| 142 | + - cancel-job: |
| 143 | + method: << parameters.method >> |
0 commit comments