| 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 | cf9a48b | 2020-12-16 19:17:48 +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 | cf9a48b | 2020-12-16 19:17:48 +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 " \ |
| 20 | "\$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 21 | else |
| 22 | echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 23 | exit 1 |
| 24 | fi |
| 25 | # --- end runfiles.bash initialization --- |
| 26 | IMAGE="{registry}/{image}:{tag}" |
| giolekva | cf9a48b | 2020-12-16 19:17:48 +0400 | [diff] [blame] | 27 | DOCKERFILE=\$$(rlocation __main__/{package}/{dockerfile}) |
| 28 | docker build \ |
| 29 | --tag=\$$IMAGE \ |
| 30 | --file=\$$DOCKERFILE \ |
| 31 | \$$(dirname "\$$DOCKERFILE") |
| 32 | docker push \$$IMAGE |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 33 | """ |
| 34 | |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 35 | def docker_image(name, registry, image, tag, dockerfile, srcs, **kwargs): |
| 36 | native.genrule( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame] | 37 | name = "%s.sh" % name, |
| 38 | executable = False, |
| 39 | outs = ["build_and_push.sh"], |
| 40 | cmd = """cat > $@ <<EOM |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 41 | %s |
| 42 | EOM |
| 43 | """ % __PUSH_TO_DEV_TMPL.format( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame] | 44 | registry = registry, |
| 45 | image = image, |
| 46 | tag = tag, |
| 47 | dockerfile = dockerfile, |
| 48 | package = native.package_name(), |
| 49 | ), |
| 50 | ) |
| giolekva | 1ef9885 | 2020-12-14 20:32:09 +0400 | [diff] [blame] | 51 | native.sh_binary( |
| giolekva | 0c08337 | 2020-12-16 14:54:27 +0400 | [diff] [blame] | 52 | name = name, |
| 53 | srcs = ["build_and_push.sh"], |
| 54 | data = srcs + [dockerfile], |
| 55 | deps = ["@bazel_tools//tools/bash/runfiles"], |
| 56 | ) |