Skip to content

Commit

Permalink
Merge pull request #80 from buildkite-plugins/toote_cancel_skip
Browse files Browse the repository at this point in the history
Skip on exit codes
  • Loading branch information
pzeballos authored Jan 12, 2023
2 parents f47ef51 + 7af505c commit 840b952
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
steps:
- label: "🤡 :hammer:"
plugins:
- docker-compose#v4.5.0:
- docker-compose#v4.9.0:
run: tests
- label: ":sparkles: lint"
plugins:
Expand Down
52 changes: 41 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This functionality duplicates the [artifact_paths](https://buildkite.com/docs/pi
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload: "log/**/*.log"
```
Expand All @@ -20,7 +20,7 @@ You can specify multiple files/globs to upload as artifacts:
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload: [ "log/**/*.log", "debug/*.error" ]
```
Expand All @@ -30,7 +30,7 @@ And even rename them before uploading them (can not use globs here though, sorry
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload:
- from: log1.log
to: log2.log
Expand All @@ -47,7 +47,7 @@ eg: uploading a public file when using S3
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload: "coverage-report/**/*"
s3-upload-acl: public-read
```
Expand All @@ -57,7 +57,7 @@ eg: uploading a private file when using GS
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload: "coverage-report/**/*"
gs-upload-acl: private
```
Expand All @@ -70,7 +70,7 @@ This downloads artifacts matching globs to the local filesystem. See [downloadin
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
download: "log/**/*.log"
```
Expand All @@ -80,7 +80,7 @@ You can specify multiple files/patterns:
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
download: [ "log/**/*.log", "debug/*.error" ]
```
Expand All @@ -90,7 +90,7 @@ Rename particular files after downloading them:
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
download:
- from: log1.log
to: log2.log
Expand All @@ -102,7 +102,7 @@ And even do so from different builds/steps:
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
step: UUID-DEFAULT
build: UUID-DEFAULT-2
download:
Expand Down Expand Up @@ -144,7 +144,7 @@ When uploading, globs specified in the `upload` option will be compressed in a s
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
upload: "log/*.log"
compressed: logs.zip
```
Expand All @@ -155,7 +155,7 @@ When downloading, this option states the actual name of the artifact to be downl
steps:
- command: ...
plugins:
- artifacts#v1.8.0:
- artifacts#v1.9.0:
download: "log/*.log"
compressed: logs.tgz
```
Expand All @@ -164,6 +164,36 @@ steps:

If set to `true`, it will ignore errors caused when calling `buildkite-agent artifact` to prevent failures if you expect artifacts not to be present in some situations.

### `skip-on-status` (optional, integer or array of integers, uploads only)

You can set this to the exit codes or array of exit codes of the command step (as defined by the `BUILDKITE_COMMAND_EXIT_STATUS` variable) that will cause the plugin to avoid trying to upload artifacts.

This should allow you to specify known failure conditions that you want to avoid uploading artifacts. For example, because you know logs will be huge or not useful.

Skip uploading if the main command failed with exit code 147:

```yml
steps:
- command: ...
plugins:
- artifacts#v1.9.0:
upload: "log/*.log"
skip-on-status: 147
```

Alternatively, skip artifact uploading on exit codes 1 and 5:

```yml
steps:
- command: ...
plugins:
- artifacts#v1.9.0:
upload: "log/*.log"
skip-on-status:
- 1
- 5
```

## Developing

To run testing, shellchecks and plugin linting use use `bk run` with the [Buildkite CLI](https://github.com/buildkite/cli).
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '2'
services:
tests:
image: buildkite/plugin-tester:v3.0.1
image: buildkite/plugin-tester:v4.0.0
volumes:
- ".:/plugin:ro"
9 changes: 9 additions & 0 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_DEBUG:-false}" =~ (true|on|1) ]] ; then
set -x
fi

while IFS='=' read -r EXIT_VAR _ ; do
if [[ $EXIT_VAR =~ ^(BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS(|_[0-9]+))$ ]]; then
if [ "${BUILDKITE_COMMAND_EXIT_STATUS}" -eq "${!EXIT_VAR}" ]; then
echo "Command exit status matches ${!EXIT_VAR}, skipping upload"
exit 0
fi
fi
done < <(env | sort)

args=("upload")

if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_JOB:-}" ]] ; then
Expand Down
8 changes: 8 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ configuration:
pattern: '.*(.zip|.tgz)$'
ignore-missing:
type: boolean
skip-on-status:
oneOf:
- type: integer
- type: array
items:
type: integer
oneOf:
- required:
- upload
- required:
- download
dependencies:
skip-on-status: [ upload ]
additionalProperties: false
4 changes: 2 additions & 2 deletions tests/download-compressed.bats
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ load '/usr/local/lib/bats/load.bash'
"artifact download \* \* : echo downloaded artifact \$3 to \$4"

stub unzip \
"\* \* : echo extracted \$2 from \$1"
"\* \* \* \* : echo extracted \$2, \$3 and \$4 from \$1"

run "$PWD/hooks/pre-command"

Expand Down Expand Up @@ -276,7 +276,7 @@ load '/usr/local/lib/bats/load.bash'
"artifact download \* \* : echo downloaded artifact \$3 to \$4"

stub tar \
"xzf \* \* : echo extracted \$3 from \$2"
"xzf \* \* \* \* : echo extracted \$3, \$4 and \$5 from \$2"

run "$PWD/hooks/pre-command"

Expand Down
60 changes: 60 additions & 0 deletions tests/upload.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,64 @@ load '/usr/local/lib/bats/load.bash'
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_FROM
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_TO
}

@test "Single skip exit status does not match still uploads" {
export BUILDKITE_COMMAND_EXIT_STATUS="100"
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="*.log"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS="10"

stub buildkite-agent \
"artifact upload \* : echo uploaded \$3"

run "$PWD/hooks/post-command"

assert_success
assert_output --partial "Uploading artifacts"

unstub buildkite-agent
}

@test "Single skip exit status matches, skips upload" {
export BUILDKITE_COMMAND_EXIT_STATUS="10"
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="*.log"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS="10"

run "$PWD/hooks/post-command"

assert_success
refute_output --partial "Uploading artifacts"
assert_output --partial "skipping upload"
}

@test "Multiple skip exit status does not match still uploads" {
export BUILDKITE_COMMAND_EXIT_STATUS="100"
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="*.log"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_0="10"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_1="1"

stub buildkite-agent \
"artifact upload \* : echo uploaded \$3"

run "$PWD/hooks/post-command"

assert_success
assert_output --partial "Uploading artifacts"

unstub buildkite-agent
}

@test "Multiple skip exit status matches, skips upload" {
export BUILDKITE_COMMAND_EXIT_STATUS="10"
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="*.log"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_0="1"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_1="100"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_2="10"
export BUILDKITE_PLUGIN_ARTIFACTS_SKIP_ON_STATUS_3="2"

run "$PWD/hooks/post-command"

assert_success
refute_output --partial "Uploading artifacts"
assert_output --partial "skipping upload"
}

0 comments on commit 840b952

Please sign in to comment.