This extract is from a bash automation script in the question-answering GitHub project. The bash script automates the deployment to IBM Cloud Code Engine. The extraction is about the building and pushing a container to the IBM Cloud Container Registry.
The extract is a function which automates following steps:
- The automation takes the commit id from the current GitHub project and uses it as the tag for the container image the automation will create.
- The automation creates a container image URL based on environment variables.
- The automation builds a container based on a Dockerfile specified with an environment variable.
- The automation logs in to the IBM Cloud Container Registry
- In case the IBM Cloud resource group for the IBM Container Registry is different, the automation changes the IBM Cloud target for the resource group.
- Set to the correct container registry region defined by an environment variable.
- Create a new namespace (repository); if the namespace doesn’t exist, the IBM Cloud Container Registry.
- Log in to IBM Cloud Registry with the Docker login command.
- Set back to the right IBM Cloud resource group if the automation changes the resource group.
This is the bash script function of the automation script:
function build_and_push_container () {
# 1. Get commit id
export COMMIT_ID=$(git rev-parse HEAD)
export CI_TAG=$COMMIT_ID
# 2. Create container image URL
export CODEENGINE_APP_IMAGE_URL="$CR/$CR_REPOSITORY/$CI_NAME:$CI_TAG"
echo "Name: $CODEENGINE_APP_IMAGE_URL"
# 3. Build container image
echo "****** BUILD *********"
cd "$HOME_PATH"/../../service
docker build -f "$HOME_PATH"/../../service/src/main/docker/"$QA_DOCKERFILE_NAME" -t "$CODEENGINE_APP_IMAGE_URL" .
cd "$HOME_PATH"
# 4. Login to IBM Cloud Container Registry
ibmcloud cr login
# 5. In case if the IBM Cloud resource group for the IBM Container Registry is different, the automation changes the IBM Cloud target for the resource group.
ERROR=$(ibmcloud target -g $CR_RESOURCE_GROUP 2>&1)
RESULT=$(echo $ERROR | grep 'FAILED' | awk '{print $1;}')
VERIFY="FAILED"
if [ "$RESULT" == "$VERIFY" ]; then
echo "Can't set to resource group: ($CR_RESOURCE_GROUP) I move on with the existing resource group."
fi
# 6. Set to the right container registry region
ibmcloud cr region-set $CR_REGION
# 7. Create a new namespace, if the namespace doesn't exists
CURR_CONTAINER_NAMESPACE=$(ibmcloud cr namespace-list -v | grep $CR_REPOSITORY | awk '{print $1;}')
if [ "$CR_REPOSITORY" != "$CURR_CONTAINER_NAMESPACE" ]; then
ibmcloud cr namespace-add $CR_REPOSITORY
fi
# 8. Log in to IBM Cloud Registry with the Docker login command
docker login -u iamapikey -p $IBM_CLOUD_API_KEY $CR_REGION
docker push "$CODEENGINE_APP_IMAGE_URL"
# 9. Set back to the right IBM Cloud resource group, in case the resource group was changed
ibmcloud target -g $IBM_CLOUD_RESOURCE_GROUP
}
I hope this was useful to you, and let’s see what’s next?
Greetings,
Thomas
#docker, #container, #automation, #bash, #ibmcloud, #ibmcloudcontainerregistry, #bashscript
Leave a Reply