planetary-computer-apis/scripts/cipublish

128 строки
3.9 KiB
Bash
Executable File

#!/bin/bash
set -e
if [[ "${CI}" ]]; then
set -x
fi
function usage() {
echo -n \
"Usage: $(basename "$0") --acr ACR_NAME --tag IMAGE_TAG
Publishes images for this project.
Must be logged in to the ACR (e.g. with az acr login -n ACR_NAME)
--acr: The name of the ACR to publish to
"
}
while [[ "$#" > 0 ]]; do case $1 in
--acr)
ACR_NAME=$2
shift
shift
;;
--tag)
IMAGE_TAG=$2
shift
shift
;;
--help)
usage
exit 0
shift
;;
*)
usage "Unknown parameter passed: $1"
shift
shift
;;
esac done
if [[ -z ${ACR_NAME} ]]; then
echo "Must pass in ACR_NAME with --acr"
exit 1
fi
if [[ -z ${IMAGE_TAG} ]]; then
echo "Must pass in IMAGE_TAG with --tag"
exit 1
fi
function install_oras() {
# https://oras.land/docs/installation/
VERSION="1.1.0"
curl -LO "https://github.com/oras-project/oras/releases/download/v${VERSION}/oras_${VERSION}_linux_amd64.tar.gz"
mkdir -p oras-install/
tar -zxf oras_${VERSION}_*.tar.gz -C oras-install/
sudo mv oras-install/oras /usr/local/bin/
rm -rf oras_${VERSION}_*.tar.gz oras-install/
}
function deprecate_image() {
local full_image_name_with_digest=$1
deprecated_since=$(date --utc --iso-8601=seconds)
if oras discover $full_image_name_with_digest -o json | jq '.manifests[].annotations' | grep -q "vnd.microsoft.lifecycle.end-of-life.date" ; then
echo "Lifecycle metadata annotation for $full_image_name_with_digest already exists, skip."
else
echo "Deprecating previous image $full_image_name_with_digest"
oras attach \
--artifact-type "application/vnd.microsoft.artifact.lifecycle" \
--annotation "vnd.microsoft.artifact.lifecycle.end-of-life.date=$deprecated_since" \
"$full_image_name_with_digest"
fi
}
function set_lineage() {
local full_image_name_with_digest=$1
# if the annotation already exists, do not add it again
if oras discover $full_image_name_with_digest -o json | jq '.manifests[].annotations."vnd.microsoft.artifact.lineage.rolling-tag"' | grep -q $IMAGE_TAG; then
echo "Lineage annotation for $IMAGE_TAG already exists, skip."
else
echo "Adding $full_image_name_with_digest to lineage $IMAGE_TAG"
oras attach \
--artifact-type "application/vnd.microsoft.artifact.lineage" \
--annotation "vnd.microsoft.artifact.lineage.rolling-tag=$IMAGE_TAG" \
"$full_image_name_with_digest"
fi
}
function publish_image() {
local local_image=$1
local published_image=$2
local full_image_name="${ACR_NAME}.azurecr.io/${published_image}:${IMAGE_TAG}"
local local_image_digest=$(docker inspect --format='{{.RepoDigests}}' "${local_image}" | cut -d'@' -f2)
local remote_image_digest=$(az acr manifest show-metadata $full_image_name | jq -r .digest)
if [ -z "$remote_image_digest" ]; then
echo "No remote image found, will publish a new image."
elif [ "$local_image_digest" != "$remote_image_digest" ]; then
# Image rolling tag exists in the registry, update the end-of-life
# annotation for the existing image.
deprecate_image "$full_image_name@$remote_image_digest"
else
echo "No changes, licycle metadata annotation will not be attached."
fi
echo "Publishing ${local_image} to ${full_image_name}"
docker tag "${local_image}" "${full_image_name}"
docker push "${full_image_name}"
local remote_image_digest=$(az acr manifest show-metadata $full_image_name | jq -r .digest)
set_lineage "$full_image_name@$remote_image_digest"
}
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
if ! command -v oras &> /dev/null
then
install_oras
fi
# Publish images
publish_image "pc-apis-stac" "planetary-computer-apis/stac"
publish_image "pc-apis-tiler" "planetary-computer-apis/tiler"
fi