Skip to content

Instantly share code, notes, and snippets.

@sinsoku
Created December 7, 2023 07:29
Show Gist options
  • Save sinsoku/7b4201787d36f8ef669abd38395a28db to your computer and use it in GitHub Desktop.
Save sinsoku/7b4201787d36f8ef669abd38395a28db to your computer and use it in GitHub Desktop.
Example code to skip tests if source trees are the same.
version: 2.1
# this allows you to use CircleCI's dynamic configuration feature
setup: true
parameters:
tree_sha_path:
type: string
default: ".git_tree_sha"
tree_status_path:
type: string
default: ".gh_tree_status.json"
# the continuation orb is required in order to use dynamic configuration
orbs:
continuation: circleci/[email protected]
gh: circleci/[email protected]
commands:
save_previous_tree_status:
steps:
- run:
name: Save previous tree sha
command: git rev-parse @^^{tree} > << pipeline.parameters.tree_sha_path >>
# An access token with `statuses:read` permission must be set in the GITHUB_TOKEN environment variable.
- run:
name: Save previous commit status as tree status
command: gh api "/repos/{owner}/{repo}/commits/$(git rev-parse @^)/status" > << pipeline.parameters.tree_status_path >>
- save_cache:
key: tree_status_{{ checksum "<< pipeline.parameters.tree_sha_path >>" }}
paths:
- << pipeline.parameters.tree_status_path >>
- run:
name: Remove previous files
command: rm << pipeline.parameters.tree_sha_path >> << pipeline.parameters.tree_status_path >>
restore_current_tree_status:
steps:
- run:
name: Save current tree sha
command: git rev-parse @^{tree} > << pipeline.parameters.tree_sha_path >>
- restore_cache:
key: tree_status_{{ checksum "<< pipeline.parameters.tree_sha_path >>" }}
- run:
name: Put empty file if tree status does not exist
command: |
if [ ! -e "<< pipeline.parameters.tree_status_path >>" ]; then
touch << pipeline.parameters.tree_status_path >>
fi
# our defined job, and its steps
jobs:
setup:
executor: continuation/default
steps:
- checkout # checkout code
- gh/install
- save_previous_tree_status
- restore_current_tree_status
- run:
name: Embed previous status
command: |
PREVIOUS_STATE=$(cat << pipeline.parameters.tree_status_path >> | jq -r '.statuses[] | select(.context == "ci/circleci: test") | .state')
sed -i "s/_PREVIOUS_STATE/$PREVIOUS_STATE/" .circleci/continue_config.yml
- run: cat .circleci/continue_config.yml
- continuation/continue:
configuration_path: .circleci/continue_config.yml
# our single workflow, that triggers the setup job defined above
workflows:
setup:
jobs:
- setup
version: 2.1
parameters:
previous_state:
type: string
default: "_PREVIOUS_STATE"
jobs:
test:
docker:
- image: cimg/base:stable
steps:
- checkout
- unless:
condition:
equal: [ success, << pipeline.parameters.previous_state >> ]
steps:
- run: echo "Run tests"
- when:
condition:
equal: [ success, << pipeline.parameters.previous_state >> ]
steps:
- run: echo "Skip tests"
workflows:
main:
jobs:
- test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment