Skip to content

Commit

Permalink
Merge pull request #455 from kindlyops/oci-image
Browse files Browse the repository at this point in the history
build and publish container image to ghcr.io during release
  • Loading branch information
statik authored Sep 24, 2022
2 parents cdd4cea + 675020e commit 77687d4
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/release-publisher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ jobs:
run: |
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | \
sh -s -- -b /usr/local/bin v0.32.0
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push vbs image to ghcr.io
run: bazel run --define=IMAGE_TAG=${{ github.ref_name }} --stamp //:push_image
- name: Run goreleaser
uses: goreleaser/goreleaser-action@master
env:
Expand Down
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ linters:
- ifshort
- gofumpt
- tagliatelle
- structcheck
- staticcheck
- unparam
- wastedassign
- noctx
- nilerr
- bodyclose
- contextcheck
- stylecheck
- gosimple
- golint
- sqlclosecheck
- rowserrcheck
- tparallel
- unused

run:
skip-dirs:
- vendor
- external
skip-files:
- dummy.go
36 changes: 36 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@bazel_gazelle//:def.bzl", "gazelle")
load("//:version.bzl", "VERSION")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -76,6 +78,40 @@ go_binary(
},
)

DEFAULT_BASE = select({
"@io_bazel_rules_docker//:debug": "@go_debug_image_base//image",
"@io_bazel_rules_docker//:fastbuild": "@go_image_base//image",
"@io_bazel_rules_docker//:optimized": "@go_image_base//image",
"//conditions:default": "@go_image_base//image",
})

# using container_image since go_image is not forwarding
# the labels to the underlying container_image
container_image(
name = "vbs_base",
base = DEFAULT_BASE,
labels = {
"org.opencontainers.image.source" : "https://github.com/kindlyops/vbs",
},
)

# see https://github.com/bazelbuild/rules_docker/pull/2087 for pending multi-arch
go_image(
base = ":vbs_base",
name = "vbs_image",
binary = ":vbs-linux",
)

container_push(
name = "push_image",
format = "Docker",
image = ":vbs_image",
registry = "ghcr.io",
repository = "kindlyops/vbs",
tag = "$(IMAGE_TAG)",
tags=["manual"],
)

alias(
name = "vbs",
actual = select({
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,20 @@ Want to help add features or fix bugs? Awesome! vbs is build using bazel.
To run goreleaser locally to test changes to the release process configuration:

goreleaser release --snapshot --skip-publish --rm-dist

### Running container image locally

To run the container image locally to test changes to the container:

bazel run vbs_image -- --publish=8090:8090 -- serve

To inspect the contents of the container using the dive utility

bazel run vbs_image -- --norun
dive bazel:vbs_image

### pushing an experimental version of the image to github container registry

To push a locally built image to github container registry:

bazel run --define=IMAGE_TAG=experimental --stamp //:push_image
75 changes: 75 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,78 @@ yarn_install(
quiet = False,
yarn_lock = "//embeddy:yarn.lock",
)

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"],
)

# OPTIONAL: Call this to override the default docker toolchain configuration.
# This call should be placed BEFORE the call to "container_repositories" below
# to actually override the default toolchain configuration.
# Note this is only required if you actually want to call
# docker_toolchain_configure with a custom attr; please read the toolchains
# docs in /toolchains/docker/ before blindly adding this to your WORKSPACE.
# BEGIN OPTIONAL segment:
load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
docker_toolchain_configure="toolchain_configure"
)
# docker_toolchain_configure(
# name = "docker_config",
# # OPTIONAL: Bazel target for the build_tar tool, must be compatible with build_tar.py
# build_tar_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable build_tar target>",
# # OPTIONAL: Path to a directory which has a custom docker client config.json.
# # See https://docs.docker.com/engine/reference/commandline/cli/#configuration-files
# # for more details.
# client_config="<enter Bazel label to your docker config.json here>",
# # OPTIONAL: Path to the docker binary.
# # Should be set explicitly for remote execution.
# docker_path="<enter absolute path to the docker binary (in the remote exec env) here>",
# # OPTIONAL: Path to the gzip binary.
# gzip_path="<enter absolute path to the gzip binary (in the remote exec env) here>",
# # OPTIONAL: Bazel target for the gzip tool.
# gzip_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable gzip target>",
# # OPTIONAL: Path to the xz binary.
# # Should be set explicitly for remote execution.
# xz_path="<enter absolute path to the xz binary (in the remote exec env) here>",
# # OPTIONAL: Bazel target for the xz tool.
# # Either xz_path or xz_target should be set explicitly for remote execution.
# xz_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable xz target>",
# # OPTIONAL: List of additional flags to pass to the docker command.
# docker_flags = [
# "--tls",
# "--log-level=info",
# ],

# )
# End of OPTIONAL segment.

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//container:container.bzl",
"container_pull",
)
load(
"@io_bazel_rules_docker//go:image.bzl",
_go_image_repos = "repositories",
)

_go_image_repos()

container_pull(
name = "static_base",
registry = "gcr.io",
repository = "distroless/static",
# 'tag' is also supported, but digest is encouraged for reproducibility.
digest = "sha256:d1d4a57d06e3c59f71cd1d72d894ab2a3c17973684d42348fbe84c1396fb4b41",
)
9 changes: 3 additions & 6 deletions cmd/fly.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/muesli/coral"
"github.com/pocketbase/pocketbase"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

var flyServerCmd = &coral.Command{
Expand All @@ -33,8 +32,6 @@ var flyServerCmd = &coral.Command{
}

func flyServer(cmd *coral.Command, args []string) {
_ = "127.0.0.1:" + viper.GetString("port")

configDir, err := os.UserConfigDir()
if err != nil {
log.Fatal().Stack().Err(err).Msg("Couldn't locate config dir")
Expand Down Expand Up @@ -65,10 +62,10 @@ func flyServer(cmd *coral.Command, args []string) {
}

// Port to listen for HTTP requests.
var FlyPort string
var httpTarget string

func init() {
flyServerCmd.Flags().StringVarP(&FlyPort, "port", "p", "8080", "Port to listen for HTTP requests")
viper.BindPFlag("port", flyServerCmd.Flags().Lookup("port"))
flyServerCmd.Flags().StringVarP(&httpTarget, "http", "a", "0.0.0.0:8090", "Address & port to listen for HTTP requests")
//viper.BindPFlag("port", flyServerCmd.Flags().Lookup("port"))
rootCmd.AddCommand(flyServerCmd)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"path/filepath"
"time"

"github.com/mattn/go-isatty"
isatty "github.com/mattn/go-isatty"
"github.com/muesli/coral"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down

0 comments on commit 77687d4

Please sign in to comment.