From b0d66f2929aa75d7f676c121a63b86bc82441e2d Mon Sep 17 00:00:00 2001 From: shillerben Date: Wed, 14 Sep 2022 20:07:51 -0500 Subject: [PATCH] Got docker push working --- .bazelrc | 2 ++ BUILD | 51 +++++++++++++++++++++++++++++----- WORKSPACE | 50 ++++++++++++++++++++------------- main/__main__.py | 9 ++++++ my_homepage/test/run_pytest.py | 6 ++++ 5 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 .bazelrc create mode 100755 main/__main__.py create mode 100644 my_homepage/test/run_pytest.py diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..ff5febf --- /dev/null +++ b/.bazelrc @@ -0,0 +1,2 @@ +build --incompatible_use_python_toolchains +test --incompatible_use_python_toolchains \ No newline at end of file diff --git a/BUILD b/BUILD index 10d3efe..1d8ef03 100644 --- a/BUILD +++ b/BUILD @@ -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("@my_deps//:requirements.bzl", "requirement") +# python library with my website code py_library( - name = "shillerben-homepage", + name = "shillerben-homepage-lib", srcs = ["my_homepage/__init__.py"], data = glob(["my_homepage/templates/*"]), deps = [ @@ -17,21 +27,48 @@ py_library( ] ) +# test my website with pytest py_test( - name = "website_test", - main = "test_website.py", - srcs = ["tests/test_website.py"], + name = "website-test", + main = "my_homepage/test/run_pytest.py", + args = ["-s", "-v", "my_homepage"], + srcs = [ + "my_homepage/test/run_pytest.py", + "my_homepage/test/test_website.py", + ], deps = [ - ":shillerben-homepage", + ":shillerben-homepage-lib", 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( name = "push-image", - image = ":shillerben-homepage", + image = ":shillerben-homepage-img", format = "Docker", registry = "docker.shillerben.com", repository = "shillerben-homepage", tag = "latest", + #extract_config = ":.docker-config.json", ) diff --git a/WORKSPACE b/WORKSPACE index f394fdf..634186f 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,19 +1,6 @@ -#workspace("my-homepage") - 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( name = "rules_python", sha256 = "b593d13bb43c94ce94b483c2858e53a9b811f6f10e1e0eedc61073bd90e58d9c", @@ -21,28 +8,53 @@ http_archive( 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( - name = "python310", - python_version = "3.10.6", + name = "python39", + python_version = "3.9.12", ) +load("@python39//:defs.bzl", "interpreter") + pip_install( name = "my_deps", python_interpreter_target = interpreter, requirements = "//:requirements.txt" ) + +# Docker stuff http_archive( name = "io_bazel_rules_docker", sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], ) -docker_toolchain_configure( - name = "docker_config", - client_config = ":.docker-config.json", +# load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl", +# docker_toolchain_configure = "toolchain_configure" +# ) + +# docker_toolchain_configure( +# name = "docker_config", +# client_config = "@//:.docker-config.json", +# ) + +load( + "@io_bazel_rules_docker//repositories:repositories.bzl", + container_repositories = "repositories", ) container_repositories() +load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps") + container_deps() + +load( + "@io_bazel_rules_docker//python3:image.bzl", + _py_image_repos = "repositories", +) + +_py_image_repos() diff --git a/main/__main__.py b/main/__main__.py new file mode 100755 index 0000000..2b454c3 --- /dev/null +++ b/main/__main__.py @@ -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") \ No newline at end of file diff --git a/my_homepage/test/run_pytest.py b/my_homepage/test/run_pytest.py new file mode 100644 index 0000000..5bfd7f6 --- /dev/null +++ b/my_homepage/test/run_pytest.py @@ -0,0 +1,6 @@ +import sys +import pytest + +# if using 'bazel test ...' +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:]))