| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 1 | __PUSH_TO_DEV_TMPL = """ |
| 2 | #!/bin/sh |
| 3 | set -eE -o functrace |
| 4 | # --- begin runfiles.bash initialization --- |
| 5 | # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). |
| 6 | set -euo pipefail |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 7 | if [[ ! -d "\\$${{RUNFILES_DIR:-/dev/null}}" && ! -f "\\$${{RUNFILES_MANIFEST_FILE:-/dev/null}}" ]]; then |
| 8 | if [[ -f "\\$$0.runfiles_manifest" ]]; then |
| 9 | export RUNFILES_MANIFEST_FILE="\\$$0.runfiles_manifest" |
| 10 | elif [[ -f "\\$$0.runfiles/MANIFEST" ]]; then |
| 11 | export RUNFILES_MANIFEST_FILE="\\$$0.runfiles/MANIFEST" |
| 12 | elif [[ -f "\\$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 13 | export RUNFILES_DIR="\\$$0.runfiles" |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 14 | fi |
| 15 | fi |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 16 | if [[ -f "\\$${{RUNFILES_DIR:-/dev/null}}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 17 | source "\\$${{RUNFILES_DIR}}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
| 18 | elif [[ -f "\\$${{RUNFILES_MANIFEST_FILE:-/dev/null}}" ]]; then |
| 19 | source "\\$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \t "\\$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 20 | else |
| 21 | echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 22 | exit 1 |
| 23 | fi |
| 24 | # --- end runfiles.bash initialization --- |
| 25 | IMAGE="{registry}/{image}:{tag}" |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 26 | DOCKERFILE=\\$$(rlocation __main__/{package}/{dockerfile}) |
| 27 | docker build --tag=\\$$IMAGE --file=\\$$DOCKERFILE \\$$(dirname "\\$$DOCKERFILE") |
| 28 | docker push \\$$IMAGE |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 29 | """ |
| 30 | |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 31 | def docker_image(name, registry, image, tag, dockerfile, srcs, **kwargs): |
| 32 | native.genrule( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 33 | name = "%s.sh" % name, |
| 34 | executable = False, |
| 35 | outs = ["build_and_push.sh"], |
| 36 | cmd = """cat > $@ <<EOM |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 37 | %s |
| 38 | EOM |
| 39 | """ % __PUSH_TO_DEV_TMPL.format( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 40 | registry = registry, |
| 41 | image = image, |
| 42 | tag = tag, |
| 43 | dockerfile = dockerfile, |
| 44 | package = native.package_name(), |
| 45 | ), |
| 46 | ) |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 47 | native.sh_binary( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame^] | 48 | name = name, |
| 49 | srcs = ["build_and_push.sh"], |
| 50 | data = srcs + [dockerfile], |
| 51 | deps = ["@bazel_tools//tools/bash/runfiles"], |
| 52 | ) |