Got docker push working

This commit is contained in:
Ben Shiller 2022-09-14 20:07:51 -05:00
parent 77a4545496
commit b0d66f2929
No known key found for this signature in database
GPG Key ID: DC46F01400846797
5 changed files with 92 additions and 26 deletions

2
.bazelrc Normal file
View File

@ -0,0 +1,2 @@
build --incompatible_use_python_toolchains
test --incompatible_use_python_toolchains

51
BUILD
View File

@ -1,9 +1,19 @@
load("@io_bazel_rules_docker//container:container.bzl", "container_push") load(
"@io_bazel_rules_docker//container:container.bzl",
"container_image",
"container_push",
)
#load("@docker_config//:push.bzl", my_container_push = "container_push")
load(
"@io_bazel_rules_docker//python3:image.bzl",
"py3_image",
)
load("@rules_python//python:defs.bzl", "py_library", "py_test") load("@rules_python//python:defs.bzl", "py_library", "py_test")
load("@my_deps//:requirements.bzl", "requirement") load("@my_deps//:requirements.bzl", "requirement")
# python library with my website code
py_library( py_library(
name = "shillerben-homepage", name = "shillerben-homepage-lib",
srcs = ["my_homepage/__init__.py"], srcs = ["my_homepage/__init__.py"],
data = glob(["my_homepage/templates/*"]), data = glob(["my_homepage/templates/*"]),
deps = [ deps = [
@ -17,21 +27,48 @@ py_library(
] ]
) )
# test my website with pytest
py_test( py_test(
name = "website_test", name = "website-test",
main = "test_website.py", main = "my_homepage/test/run_pytest.py",
srcs = ["tests/test_website.py"], args = ["-s", "-v", "my_homepage"],
srcs = [
"my_homepage/test/run_pytest.py",
"my_homepage/test/test_website.py",
],
deps = [ deps = [
":shillerben-homepage", ":shillerben-homepage-lib",
requirement("pytest"), requirement("pytest"),
], ],
) )
# custom base image layer to set envs and cmd
container_image(
name = "website-base-py3",
base = "@py3_image_base//image",
ports = ["4040"],
)
py3_image(
name = "shillerben-homepage-img",
base = ":website-base-py3",
main = "main/__main__.py",
args = ["-b", "0.0.0.0:4040", "my_homepage:create_app()"],
srcs = [
"main/__main__.py",
],
deps = [
":shillerben-homepage-lib",
],
)
# push new website image
container_push( container_push(
name = "push-image", name = "push-image",
image = ":shillerben-homepage", image = ":shillerben-homepage-img",
format = "Docker", format = "Docker",
registry = "docker.shillerben.com", registry = "docker.shillerben.com",
repository = "shillerben-homepage", repository = "shillerben-homepage",
tag = "latest", tag = "latest",
#extract_config = ":.docker-config.json",
) )

View File

@ -1,19 +1,6 @@
#workspace("my-homepage")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
load("@python310//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_install")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
docker_toolchain_configure="toolchain_configure"
)
load(
"@io_bazel_rules_docker//repositories:repositories.bzl",
container_repositories = "repositories",
)
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
# Python stuff
http_archive( http_archive(
name = "rules_python", name = "rules_python",
sha256 = "b593d13bb43c94ce94b483c2858e53a9b811f6f10e1e0eedc61073bd90e58d9c", sha256 = "b593d13bb43c94ce94b483c2858e53a9b811f6f10e1e0eedc61073bd90e58d9c",
@ -21,28 +8,53 @@ http_archive(
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.12.0.tar.gz", url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.12.0.tar.gz",
) )
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
load("@rules_python//python:pip.bzl", "pip_install")
python_register_toolchains( python_register_toolchains(
name = "python310", name = "python39",
python_version = "3.10.6", python_version = "3.9.12",
) )
load("@python39//:defs.bzl", "interpreter")
pip_install( pip_install(
name = "my_deps", name = "my_deps",
python_interpreter_target = interpreter, python_interpreter_target = interpreter,
requirements = "//:requirements.txt" requirements = "//:requirements.txt"
) )
# Docker stuff
http_archive( http_archive(
name = "io_bazel_rules_docker", name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
) )
docker_toolchain_configure( # load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
name = "docker_config", # docker_toolchain_configure = "toolchain_configure"
client_config = ":.docker-config.json", # )
# docker_toolchain_configure(
# name = "docker_config",
# client_config = "@//:.docker-config.json",
# )
load(
"@io_bazel_rules_docker//repositories:repositories.bzl",
container_repositories = "repositories",
) )
container_repositories() container_repositories()
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
container_deps() container_deps()
load(
"@io_bazel_rules_docker//python3:image.bzl",
_py_image_repos = "repositories",
)
_py_image_repos()

9
main/__main__.py Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
from gunicorn.app.wsgiapp import run
run()
# from my_homepage import create_app
# app = create_app()
# app.run("0.0.0.0", "4040")

View File

@ -0,0 +1,6 @@
import sys
import pytest
# if using 'bazel test ...'
if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:]))