pre-command 1.1 KB

1234567891011121314151617181920212223242526
  1. #!/bin/bash
  2. # This script is executed by Buildkite on the host machine.
  3. # In contrast, our build jobs are run in Docker containers.
  4. # This means that even though our build jobs write to
  5. # `/artifact-mount`, the directory on the host machine is
  6. # actually `/tmp/artifacts`.
  7. # We clean up the artifacts directory before any command and
  8. # after uploading artifacts to make sure no stale artifacts
  9. # remain on the node when a Buildkite runner is re-used.
  10. set -ex
  11. if [ -d "/tmp/artifacts" ]; then
  12. echo "Cleaning up old artifacts before command."
  13. echo "Artifact directory contents before cleanup:"
  14. find /tmp/artifacts -print || true
  15. if [ "$(ls -A /tmp/artifacts)" ]; then
  16. echo "Directory not empty, cleaning up..."
  17. # Need to run in docker to avoid permission issues
  18. docker run --rm -v /tmp/artifacts:/artifact-mount alpine:latest /bin/sh -c 'rm -rf /artifact-mount/*; rm -rf /artifact-mount/.[!.]*' || true
  19. else
  20. echo "Directory already empty, no need to clean up."
  21. fi
  22. echo "Artifact directory contents after cleanup:"
  23. find /tmp/artifacts -print || true
  24. fi