I am trying to optimise CodeBuild spec and check if the docker image already exists in the repo and then just exit the build phase. According to documentation version 0.2 executes all commands in one phase in a single shell, so I assumed that exit 0
should do the trick, but it doesn't seem to work.
aws ecr describe-images --repository-name $ECR_REPO_NAME --image-ids=imageTag=$TAG 2>&1 > /dev/null && echo 'Image already exists' && exit 0 || true
This prints "Image already exists" but the build continues.
If you use exit 0
then you indicate that this step in CodeBuild was successful, and CodeBuild will just continue with the next step. Returning exit 1
is no option as that will show your build as failed.
You can either write a script that checks for the existence of your image, and if that fails execute the build step. Then simply run this script as part of your build pipeline. The script will either exit quickly (if the image exists), or build and upload the image.