after_success

This block is executed only if the previous stage ends without any error. Once the block is executed, we are ready to publish our image:

docker login -u ${CI_ENV_REGISTRY_USER} -p "${CI_ENV_REGISTRY_PASS}"
if [[ ${TRAVIS_TAG} =~ ^v.*$ ]]; then
docker tag my-app ${RELEASE_IMAGE_PATH}
docker push ${RELEASE_IMAGE_PATH}
else
docker tag my-app ${BUILD_IMAGE_PATH}
docker push ${BUILD_IMAGE_PATH}
fi

Our image tag uses the commit hash for ordinary builds and uses a manually tagged version for releases. There's no absolute rule for tagging an image, but using the default latest tag for your business service is strongly discouraged as it could result in version confusion, such as running two different images that have the same name. The last conditional block is used to publish the image on certain branch tags, and we want to keep building and releasing on separate tracks. Remember to authenticate to Docker Hub before pushing an image.

Kubernetes decides whether the image should be pulled using imagePullPolicy, which defaults to IfNotPresent:
IfNotPresent: kubelet pulls images if they aren't present on the node. If the image tag is :latest and the policy isn't Never, then kubelet falls back to Always.
Always: kubelet always pulls images.
Never: kubelet never pulls images; it will find out whether the desired image is on the node or not.

Because we set our project deployments to actual machines only on a release, a build may stop and be returned at that moment. Let's have a look into the log of this build: https://travis-ci.com/DevOps-with-Kubernetes/okeydokey/builds/93296022. The log retains the executed scripts and outputs from every line of the script during a CI build:

As we can see, our build is successful, so the image is then published here: https://hub.docker.com/r/devopswithkubernetes/okeydokey/tags/The build refers to the build-842eb66b2fa612598add8e19769af5c56b922532 tag and we can now run it outside the CI server:

$ docker run --name test -dp 5000:5000 devopswithkubernetes/okeydokey:build-842eb66b2fa612598add8e19769af5c56b922532
3d93d6505e369286c3f072ef4f04e15db2638f280c4615be95bff47379a70388
$ curl localhost:5000
OK
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset