Skip to content

Commit

Permalink
Add dockertools (#1868)
Browse files Browse the repository at this point in the history
* Add dockertools

* Delete unnecessary vendor

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile

* Modify Dockerfile for docker solutions

* Update docker.Dockerfile

* Test CI/CD for Docker solutions

* CI/CD for dockertools

* CI/CD for dockertools

* CI/CD for dockertools

* CI/CD for dockertools

* CI/CD for dockertools

* Use Go modules

* Update README.md

* Update README.md
  • Loading branch information
Matt-Jns authored Aug 26, 2022
1 parent c0ba7e8 commit 8d27925
Show file tree
Hide file tree
Showing 11 changed files with 1,027 additions and 20 deletions.
27 changes: 7 additions & 20 deletions docker.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM marketplace.gcr.io/google/debian9 AS build
FROM marketplace.gcr.io/google/debian11

ENV BAZEL_VERSION 0.19.2
ENV BAZEL_ARCH linux_amd64_stripped
ENV DOCKER_TOOLS_TAG v1.0.0

RUN set -eux \
&& apt-get update \
&& apt-get install git wget unzip python g++ -y
&& apt-get install wget -y

# Install Bazel
RUN set -eux \
&& wget -q -O /bazel-installer.sh "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" \
&& chmod +x /bazel-installer.sh \
&& /bazel-installer.sh

RUN set -eux \
&& git clone https://github.com/GoogleCloudPlatform/runtimes-common.git --depth=1 \
&& cd runtimes-common \
&& bazel run //:gazelle \
&& bazel build versioning/scripts/dockerfiles:dockerfiles versioning/scripts/cloudbuild:cloudbuild \
&& cp bazel-bin/versioning/scripts/dockerfiles/${BAZEL_ARCH}/dockerfiles /bin/dockerfiles \
&& cp bazel-bin/versioning/scripts/cloudbuild/${BAZEL_ARCH}/cloudbuild /bin/cloudbuild

FROM marketplace.gcr.io/google/debian9

COPY --from=build /bin/dockerfiles /bin/dockerfiles
COPY --from=build /bin/cloudbuild /bin/cloudbuild
&& wget https://github.com/GoogleCloudPlatform/click-to-deploy/releases/download/${DOCKER_TOOLS_TAG}/cloudbuild -O /bin/cloudbuild \
&& wget https://github.com/GoogleCloudPlatform/click-to-deploy/releases/download/${DOCKER_TOOLS_TAG}/dockerfiles -O /bin/dockerfiles \
&& chmod +x /bin/cloudbuild \
&& chmod +x /bin/dockerfiles
55 changes: 55 additions & 0 deletions tools/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
http_archive(
name = "io_bazel_rules_go",
sha256 = "1868ff68d6079e31b2f09b828b58d62e57ca8e9636edff699247c9108518570b",
url = "https://github.com/bazelbuild/rules_go/releases/download/0.11.1/rules_go-0.11.1.tar.gz",
)

http_archive(
name = "bazel_gazelle",
sha256 = "92a3c59734dad2ef85dc731dbcb2bc23c4568cded79d4b87ebccd787eb89e8d0",
url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.11.0/bazel-gazelle-0.11.0.tar.gz",
)

load("@io_bazel_rules_go//go:def.bzl", "go_repository", "go_rules_dependencies", "go_register_toolchains")

# Declare Go direct dependencies.
go_repository(
name = "com_github_gopkg_v2",
importpath = "gopkg.in/yaml.v2",
tag = "v2.3.0",
)

go_rules_dependencies()

go_register_toolchains()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

git_repository(
name = "io_bazel_rules_docker",
commit = "8bbe2a8abd382641e65ff7127a3700a8530f02ce",
remote = "https://github.com/bazelbuild/rules_docker.git",
)

git_repository(
name = "containerregistry",
commit = "6b250f0bae8cce028df939010ee3118c8f2977ba",
remote = "https://github.com/google/containerregistry",
)

load(
"@io_bazel_rules_docker//docker:docker.bzl",
"docker_repositories",
"docker_pull",
)

docker_repositories()

load(
"@io_bazel_rules_docker//container:container.bzl",
"repositories",
)

repositories()
40 changes: 40 additions & 0 deletions tools/cloudbuild-dockertools.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
steps:
- id: Build Dockertools Docker Image
name: gcr.io/cloud-builders/docker
args:
- build
- --tag
- dockertools
- --file
- tools/dockertools.Dockerfile
- tools

- id: Share dockertools using workspaces
name: dockertools
entrypoint: bash
args:
- -c
- |
cp /bin/cloudbuild /workspace/cloudbuild &&
cp /bin/dockerfiles /workspace/dockerfiles &&
cp /bin/cloudbuild /workspace/cloudbuild_$TAG_NAME &&
cp /bin/dockerfiles /workspace/dockerfiles_$TAG_NAME
- id: Create a new GitHub Release.
name: dockertools
entrypoint: bash
args:
- -c
- |
gh release create $TAG_NAME --notes "Dockertools" /workspace/{cloudbuild,dockerfiles}
secretEnv: ['GH_TOKEN']

artifacts:
objects:
location: 'gs://$PROJECT_ID-c2d/'
paths: ['/workspace/cloudbuild', '/workspace/dockerfiles', '/workspace/cloudbuild_$TAG_NAME', '/workspace/dockerfiles_$TAG_NAME']

availableSecrets:
secretManager:
- versionName: projects/$PROJECT_ID/secrets/GH_TOKEN/versions/1
env: 'GH_TOKEN'
42 changes: 42 additions & 0 deletions tools/dockertools.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM marketplace.gcr.io/google/debian11

ENV BAZEL_VERSION 0.19.2
ENV BAZEL_ARCH linux_amd64_stripped

RUN set -eux \
&& apt-get update \
&& apt-get install git wget unzip python g++ curl -y

# Install Bazel
RUN set -eux \
&& wget -q -O /bazel-installer.sh "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" \
&& chmod +x /bazel-installer.sh \
&& /bazel-installer.sh

# Install gh CLI
RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& apt update \
&& apt install gh

RUN set -eux \
&& git clone https://github.com/GoogleCloudPlatform/click-to-deploy.git \
&& cd click-to-deploy/tools \
&& bazel build dockerversioning/scripts/dockerfiles:dockerfiles dockerversioning/scripts/cloudbuild:cloudbuild \
&& cp bazel-bin/dockerversioning/scripts/dockerfiles/${BAZEL_ARCH}/dockerfiles /bin/dockerfiles \
&& cp bazel-bin/dockerversioning/scripts/cloudbuild/${BAZEL_ARCH}/cloudbuild /bin/cloudbuild
70 changes: 70 additions & 0 deletions tools/dockerversioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Description

Versioning tools for Dockerfile source repos. Previously, these tools were in [runtimes-common](https://github.com/GoogleCloudPlatform/runtimes-common/tree/b39744e5a8588beba847a271e379b864a6ac2939), but have been moved to [click-to-deploy](https://github.com/GoogleCloudPlatform/click-to-deploy) for further development.

- `dockerfiles` generates versionsed Dockerfiles from a common template.
- `cloudbuild` generates a configuration file to build these Dockerfiles using
[Google Container Builder](https://cloud.google.com/container-builder/docs/).

# Installation

- Install [Bazel, version 0.19.2](https://bazel.build) as the build tool.

- Clone this repo:

``` shell
git clone https://github.com/GoogleCloudPlatform/click-to-deploy.git
cd click-to-deploy/tools
```

- Build:

``` shell
bazel run //:gazelle
bazel build dockerversioning/scripts/dockerfiles:dockerfiles
bazel build dockerversioning/scripts/cloudbuild:cloudbuild
```

- Set the path to the built scripts:

``` shell
BAZEL_ARCH=linux_amd64_stripped
export PATH=$PATH:$PWD/bazel-bin/dockerversioning/scripts/dockerfiles/${BAZEL_ARCH}/
export PATH=$PATH:$PWD/bazel-bin/dockerversioning/scripts/cloudbuild/${BAZEL_ARCH}/
```

# Create `versions.yaml`

At root of the Dockerfile source repo, add a file called `versions.yaml`.
Follow the format defined in `versions.go`. See an example on
[github](https://github.com/GoogleCloudPlatform/mysql-docker).

Primary folders in the Dockerfile source repo:

- `templates` contains `Dockerfile.template`, which is a Go template for
generating `Dockerfile`s.
- `tests` contains any tests that should be included in the generated cloud
build configuration.
- Version folders as defined in `versions.yaml`. The `Dockerfile`s are
generated into these folders. The folders should also contain all
supporting files for each version, for example `docker-entrypoint.sh` files.

# Usage of `dockerfiles` command

``` shell
cd path/to/dockerfile/repo
dockerfiles
```

# Usage of `cloudbuild` command

``` shell
cd path/to/dockerfile/repo
cloudbuild > cloudbuild.yaml
```

You can use the generated `cloudbuild.yaml` file as followed:

``` shell
gcloud container builds submit --config=cloudbuild.yaml .
```
15 changes: 15 additions & 0 deletions tools/dockerversioning/scripts/cloudbuild/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/GoogleCloudPlatform/click-to-deploy/tools/dockerversioning/scripts/cloudbuild",
visibility = ["//visibility:private"],
deps = ["//dockerversioning/versions:go_default_library"],
)

go_binary(
name = "cloudbuild",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 8d27925

Please sign in to comment.