Home > database >  How can I write a docker image dev to stage promotion script for Azure container registry?
How can I write a docker image dev to stage promotion script for Azure container registry?

Time:08-16

We want to write a shell script to promote docker images from dev to stage container registries for azure.

CodePudding user response:

Here is something you can modify and use:

#!/usr/bin/env bash
BASE_DIR=.
LOG_FILE=${BASE_DIR}/PromotionImageScriptLog.log

log()
{
        echo "`date  '%m-%d-%Y: %X'` - $@" >> ${BASE_DIR}/${LOG_FILE} 2>>${BASE_DIR}/${LOG_FILE}
        echo "`date  '%m-%d-%Y: %X'` - $@"
}

echo "Choose the environments for docker image promotions"
echo "Enter option 1: For dev to stage promotions"
echo "Enter option 2: For stage to prod promotions"

read promotion_env_option
echo "You chose:$promotion_env_option"


echo "Enter the docker image to be promoted with path: (zippy-backend/zippy-webhook-processor:2.1.1-SNAPSHOT)"
read source_image_with_path
echo "Image to be promoted:$source_image_with_path"

source_repo=$source_image_with_path
target_repo=$source_image_with_path



if [ $promotion_env_option = "1" ]
then
    source_registry="/subscriptions/-----YOUR-SOURCE-SUBSCRIPTION-ID----/resourceGroups/----YOUR-SOURCE-RG----/providers/Microsoft.ContainerRegistry/registries/----YOUR-SOURCE-CONTAINER-REGISTRY----"
    target_subscription="-----YOUR-TARGET-SUBSCRIPTION-NAME----"
    target_registry_name="----YOUR-TARGET-CONTAINER-REGISTRY----"
elif [ $promotion_env_option = "2" ]
then
    source_registry="/subscriptions/YOUR-SOURCE-SUBSCRIPTION-ID/resourceGroups/----YOUR-SOURCE-RG----/providers/Microsoft.ContainerRegistry/registries/----YOUR-SOURCE-CONTAINER-REGISTRY----"
    target_subscription="-----YOUR-TARGET-SUBSCRIPTION-NAME----"
    target_registry_name="----YOUR-TARGET-CONTAINER-REGISTRY----"
else
    echo "Please choose 1 or 2 option for promtion."
fi



echo "source_registry:$source_registry"
echo "target_subscription:$target_subscription"
echo "target_registry_name:$target_registry_name"



log "az account set --subscription ${target_subscription}"
az account set --subscription "${target_subscription}"
RC=$?
if [[ ${RC} -ne 0 ]]; then
  log "ERROR: Failed to set subscription. RC=${RC}"
  exit 1
fi

log "az acr import --name $target_registry_name --subscription "$target_subscription" --source $source_repo --image $target_repo --registry $source_registry"
az acr import --name $target_registry_name --subscription "$target_subscription" --source $source_repo --image $target_repo --registry $source_registry
RC=$?
if [[ ${RC} -ne 0 ]]; then
   log "ERROR: Failed to promote ${target_registry_name}:${target_repo} to Production ACR. RC=${RC}"
else   
   log "Image promted to $target_registry_name"
fi
  • Related