Skip to content

Commit

Permalink
Consolidate custom DC Docker image release scripts (datacommonsorg#4527)
Browse files Browse the repository at this point in the history
This will be used to build + tag stable after the stable branch is
updated: https://paste.googleplex.com/5895977398697984

An alternate approach would be to just keep the shared script and have
the deployment yamls call it with arguments. Lmk what you think!
  • Loading branch information
hqpho authored Nov 14, 2024
1 parent c3aa528 commit a11642e
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 23 deletions.
95 changes: 95 additions & 0 deletions scripts/build_and_tag_cdc_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash
#
# Copyright 2024 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
#
# https://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.

# Creates a new custom DC data or services docker image and tags it.
# Also tags it with custom labels from arguments.

# Usage: From root, ./scripts/build_and_tag_cdc_image.sh --image-type {data|services} --commits-label \$COMMITS_LABEL --release-label \$RELEASE_LABEL

set -e
set -x

image_type=""
commits_label=""
release_label=""

while [[ $# -gt 0 ]]; do
case "$1" in
--image-type)
image_type="$2"
shift 2
;;
--commits-label)
commits_label="$2"
shift 2
;;
--release-label)
release_label="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done

if [[ $image_type != "data" && $image_type != "services" ]]; then
echo "Invalid image type: $image_type"
echo "Usage: ./scripts/build_and_tag_cdc_image.sh --image-type {data|services} --commits-label \$COMMITS_LABEL --release-label \$RELEASE_LABEL"
exit 1
fi

if [[ $commits_label = "" ]]; then
echo "Expected named argument --commits-label with image label."
echo "Usage: ./scripts/build_and_tag_cdc_image.sh --image-type {data|services} --commits-label \$COMMITS_LABEL --release-label \$RELEASE_LABEL"
exit 1
fi

if [[ $release_label = "" ]]; then
echo "Expected named argument --release-label with image label."
echo "Usage: ./scripts/build_and_tag_cdc_image.sh --image-type {data|services} --commits-label \$COMMITS_LABEL --release-label \$RELEASE_LABEL"
exit 1
fi

if [[ $image_type == "data" ]]; then
dockerfile="build/cdc_data/Dockerfile"
image_name="datacommons-data"
elif [[ $image_type == "services" ]]; then
dockerfile="build/cdc_services/Dockerfile"
image_name="datacommons-services"
fi

# Check for an existing image with the given commits label.
list_result=$(gcloud container images list-tags gcr.io/datcom-ci/${image_name} --filter="tags=${commits_label}" 2>/dev/null)

if echo "$list_result" | grep -q $commits_label; then
# Add release label to existing image.
echo "Image gcr.io/datcom-ci/${image_name}:${commits_label} already exists."
echo "Tagging existing image with release label."
gcloud container images add-tag --quiet \
"gcr.io/datcom-ci/${image_name}:${commits_label}" \
"gcr.io/datcom-ci/${image_name}:${release_label}"

else
# Build and push a fresh image, adding commits label and release label.
docker build -f "$dockerfile" \
--tag "gcr.io/datcom-ci/${image_name}:${commits_label}" \
--tag "gcr.io/datcom-ci/${image_name}:${release_label}" \
.
docker push "gcr.io/datcom-ci/${image_name}:${commits_label}"
docker push "gcr.io/datcom-ci/${image_name}:${release_label}"
fi
21 changes: 9 additions & 12 deletions scripts/build_cdc_data_and_tag_latest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@
# Creates a new custom DC data docker image and tags it latest.
# Also tags it with a custom label from an argument.

# Usage: From root, ./scripts/build_cdc_data_and_tag_latest.sh $IMAGE_LABEL
# Usage: From root, ./scripts/build_cdc_data_and_tag_latest.sh $COMMITS_LABEL

# The latest image = gcr.io/datcom-ci/datacommons-data:latest

set -e
set -x

image_label=$1
if [[ $image_label = "" ]]; then
echo "Expected positional argument with image label."
echo "Usage: ./scripts/build_cdc_data_and_tag_latest.sh \$IMAGE_LABEL"
commits_label=$1
if [[ $commits_label = "" ]]; then
echo "Expected positional argument with commits label."
echo "Usage: ./scripts/build_cdc_data_and_tag_latest.sh \$COMMITS_LABEL"
exit 1
fi

# Build a new image and push it to Container Registry, tagging it as latest
docker build -f build/cdc_data/Dockerfile \
--tag "gcr.io/datcom-ci/datacommons-data:${image_label}" \
--tag gcr.io/datcom-ci/datacommons-data:latest \
.
docker push "gcr.io/datcom-ci/datacommons-data:${image_label}"
docker push gcr.io/datcom-ci/datacommons-data:latest
./scripts/build_and_tag_cdc_image.sh \
--image-type data \
--commits-label "${commits_label}" \
--release-label "latest"
37 changes: 37 additions & 0 deletions scripts/build_cdc_data_and_tag_stable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
#
# Copyright 2024 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
#
# https://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.

# Creates a new custom DC data docker image and tags it stable.
# Also tags it with a custom label from an argument.

# Usage: From root, ./scripts/build_cdc_data_and_tag_stable.sh $COMMITS_LABEL

# The stable image = gcr.io/datcom-ci/datacommons-data:stable

set -e
set -x

commits_label=$1
if [[ $commits_label = "" ]]; then
echo "Expected positional argument with commits label."
echo "Usage: ./scripts/build_cdc_data_and_tag_stable.sh \$COMMITS_LABEL"
exit 1
fi

./scripts/build_and_tag_cdc_image.sh \
--image-type data \
--commits-label "${commits_label}" \
--release-label "stable"
19 changes: 8 additions & 11 deletions scripts/build_cdc_services_and_tag_latest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
set -e
set -x

image_label=$1
if [[ $image_label = "" ]]; then
echo "Expected positional argument with image label."
echo "Usage: ./scripts/build_cdc_services_and_tag_latest.sh \$IMAGE_LABEL"
commits_label=$1
if [[ $commits_label = "" ]]; then
echo "Expected positional argument with commits label."
echo "Usage: ./scripts/build_cdc_services_and_tag_latest.sh \$COMMITS_LABEL"
exit 1
fi

# Build a new image and push it to Container Registry, tagging it as latest
DOCKER_BUILDKIT=1 docker build -f build/cdc_services/Dockerfile \
--tag "gcr.io/datcom-ci/datacommons-services:${image_label}" \
--tag gcr.io/datcom-ci/datacommons-services:latest \
.
docker push "gcr.io/datcom-ci/datacommons-services:${image_label}"
docker push gcr.io/datcom-ci/datacommons-services:latest
./scripts/build_and_tag_cdc_image.sh \
--image-type services \
--commits-label "${commits_label}" \
--release-label "latest"
37 changes: 37 additions & 0 deletions scripts/build_cdc_services_and_tag_stable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
#
# Copyright 2024 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
#
# https://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.

# Creates a new custom DC services docker image and tags it stable.
# Also tags it with a custom label from an argument.

# Usage: From root, ./scripts/build_cdc_services_and_tag_stable.sh $IMAGE_LABEL

# The stable image = gcr.io/datcom-ci/datacommons-services:stable

set -e
set -x

commits_label=$1
if [[ $commits_label = "" ]]; then
echo "Expected positional argument with commits label."
echo "Usage: ./scripts/build_cdc_services_and_tag_stable.sh \$COMMITS_LABEL"
exit 1
fi

./scripts/build_and_tag_cdc_image.sh \
--image-type services \
--commits-label "${commits_label}" \
--release-label "stable"

0 comments on commit a11642e

Please sign in to comment.