Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug when running on upstream branch, and add some tests #1

Merged
merged 3 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
version: 2.1

refs:
test_filter: &test_filter
filters:
branches:
only: testing

workflows:
version: 2.0

# publish-dev:
# jobs:
# - publish

ci:
jobs:
- test_orb_file_changed: *test_filter
- test_orb_no_file_changed: *test_filter
- test_orb_on_upstream: *test_filter

orbs:
multirepo: dnephin/multirepo@dev:testing

executors:
bash:
docker:
- image: bash:4

commands:
# temporary command while the official circleci/cli orb dependencies are cleaned up.
install-cli:
steps:
- run:
name: download
command: |
export releases=https://github.com/CircleCI-Public/circleci-cli/releases
export version=0.1.5432
wget -qO- $releases/download/v${version}/circleci-cli_${version}_linux_amd64.tar.gz | \
tar -zx circleci-cli_${version}_linux_amd64/circleci
mv circleci*/* /bin

install-git:
steps:
- run:
name: "Install git"
command: |
command -v git && exit
command -v apk && apk add --no-cache --no-progress git

setup:
steps:
- install-git
- checkout
- run: test/bin/fake-circleci-agent

jobs:
publish:
executor: bash
parameters:
tag:
type: string
default: "dev:testing"
steps:
- install-cli
- install-git
- run: circleci orb validate orb.yaml
- run: circleci orb publish orb.yaml dnephin/multirepo@<< parameters.tag >>
- run: |
git co -b local
git push origin local:stage

test_orb_file_changed:
executor: bash
environment:
TESTDIR: /tmp/testdir
steps:
- setup
- multirepo/run-job-for-paths:
paths: orb.yaml
- run: test/bin/expect-no-halt

test_orb_no_file_changed:
executor: bash
environment:
TESTDIR: /tmp/testdir
steps:
- setup
- multirepo/run-job-for-paths:
paths: testdata/no-changes-here
- run: test/bin/expect-halted

test_orb_on_upstream:
executor: bash
environment:
TESTDIR: /tmp/testdir
steps:
- setup
- multirepo/run-job-for-paths:
paths: orb.yaml
upstream_branch: $CIRCLE_SHA1
- run: test/bin/expect-no-halt
20 changes: 15 additions & 5 deletions orb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ description: |
commands:
run-job-for-paths:
description: |
Continue running the job if any files in the paths have been modified relative
to the last commit shared with the upstream git branch.
Continue running the job if any file in paths has been modified since
the last commit shared with the upstream git branch (default: origin/master).

This command must be run after `checkout`.

This command allows you to skip all of the subsequent steps in a job so that any
repository with multiple workflows, or multiple flows within a single
Expand Down Expand Up @@ -36,12 +38,22 @@ commands:
default: "$PWD"
steps:
- run:
name: Check if task should run
name: Check if job should run
command: |
cd << parameters.working_directory >>

upstream="$(git merge-base HEAD << parameters.upstream_branch >>)"
echo "Comparing to upstream commit $upstream"

if [[ "$(git rev-parse HEAD)" == "$upstream" ]]; then
echo "HEAD matches upstream, running with no diff"
exit 0
fi

if [[ -z "$upstream" ]]; then
echo "git merge-base failed"
exit 1
fi

# Print the diff
PAGER=cat git diff --stat=80 $upstream -- << parameters.paths >>
Expand All @@ -51,5 +63,3 @@ commands:
echo "Skipping job, no files modified"
circleci-agent step halt
fi


12 changes: 12 additions & 0 deletions test/bin/expect-halted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eu -o pipefail
testdir="${TESTDIR-/tmp/testdir}"

if [[ -f "$testdir/halted" ]]; then
echo "Task was halted"
rm "$testdir/halted"
exit 0
fi

echo "Task was not halted"
exit 1
12 changes: 12 additions & 0 deletions test/bin/expect-no-halt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eu -o pipefail
testdir="${TESTDIR-/tmp/testdir}"

if [[ ! -f "$testdir/halted" ]]; then
echo "Task was not halted"
exit 0
fi

rm "$testdir/halted"
echo "Task was halted"
exit 1
12 changes: 12 additions & 0 deletions test/bin/fake-circleci-agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eu -o pipefail
testdir="${TESTDIR-/tmp/testdir}"
mkdir -p $testdir ~/bin

fake=~/bin/circleci-agent
cat > $fake <<FAKE
touch "$testdir/halted"
FAKE
chmod +x $fake

echo "export PATH=$(dirname $fake):$PATH" >> $BASH_ENV