Continuous Integration, Delivery and Deployment
What CI, continuous delivery and continuous deployment mean, with Docker multi-stage builds and Bitbucket Pipelines examples for ECR and Beanstalk.
8 min readUpdated
Continuous integration (CI), continuous delivery and continuous deployment (both abbreviated CD) are three stages of automating the path from a commit to production. This note explains the difference between them, then shows a Docker multi-stage build for a Java application and Bitbucket Pipelines configurations that build Docker images, push them to Amazon ECR or Docker Hub and deploy to AWS Elastic Beanstalk.
CI and CD compared
A CI pipeline takes the code from the Git repository, builds it, runs the unit tests, packages it, runs the integration tests and stores the result in an artifact repository. A CD pipeline then takes that artifact through the test, staging and production environments, running acceptance, performance and end-to-end tests along the way.

Continuous integration
Developers who practise continuous integration merge their changes back to the main branch as often as possible. Each change is validated by building the application and running automated tests against the build. This avoids the “integration hell” that happens when people wait until release day to merge their changes into the release branch.
Continuous integration relies heavily on test automation, to check that the application still works whenever new commits are merged into the main branch.
Continuous delivery
Continuous delivery extends continuous integration so that you can release new changes to your customers quickly and sustainably. On top of automated testing, the release process is automated too, and you can deploy the application at any time by clicking a button.
You can release daily, weekly, fortnightly or on whatever schedule suits the business. To get the full benefit, though, deploy to production as early as possible, so that each release is a small batch that is easy to troubleshoot when something goes wrong.
Continuous deployment
Continuous deployment goes one step further: every change that passes all stages of the production pipeline is released to customers. There is no human intervention, and only a failing test stops a change from reaching production.
Continuous deployment shortens the feedback loop with customers and takes pressure off the team, because there is no release day any more. Developers see their work go live minutes after they finish it.
The diagram below shows the difference. Both pipelines run builds, tests, acceptance tests and a deployment to staging automatically. In continuous delivery, the deployment to production is a manual step; in continuous deployment, it is automatic too, followed by smoke tests.

Build a Java application with a Docker multi-stage build
A multi-stage build compiles the application in one image and copies only the result into the runtime image, so the final image contains no build tools. In this Dockerfile the first stage builds a WAR with Maven, and the second stage deploys it to Payara Server together with a PostgreSQL JDBC driver and a custom domain.xml:
# BUILD STAGEFROM maven:3.6.3-jdk-11 AS buildCOPY src /usr/src/app/srcCOPY pom.xml /usr/src/appRUN mvn -f /usr/src/app/pom.xml clean package
# DEPLOYMENT STAGEFROM payara/server-fullCOPY ./postgresql-42.2.14.jar ${PAYARA_DIR}/glassfish/domains/production/libCOPY ./domain.xml ${PAYARA_DIR}/glassfish/domains/production/configCOPY --from=build /usr/src/app/target/appname-1.0-SNAPSHOT.war ${DEPLOY_DIR}AS buildnames the first stage, andCOPY --from=buildcopies the WAR out of it.PAYARA_DIRandDEPLOY_DIRare set by the Payara image. Payara deploys the files inDEPLOY_DIRwhen the server starts.- The domain name in the paths (
production) must match the domain that your Payara image runs.
The official maven image no longer publishes jdk-* tags. Current tags name the JDK distribution, for example maven:3.9-eclipse-temurin-17.
Build and push a Docker image with Bitbucket Pipelines
Bitbucket Pipelines runs the CI/CD steps defined in a bitbucket-pipelines.yml file at the root of the repository. The examples below use Bitbucket as the automation tool, Docker as the container platform, and Amazon ECR or Docker Hub as the container registry.
Push to Amazon ECR
This pipeline has one step that builds the image and pushes it to Amazon ECR. It runs on every push or merge to the main branch. The step is defined once under definitions with a YAML anchor (&buildImage) and referenced from the pipeline with *buildImage:
image: name: atlassian/pipelines-awscli
definitions: buildImage: &buildImage name: Build and Push Docker Image caches: - docker services: - docker script: - export DOCKER_URI=$DOCKER_IMAGE:build-$BITBUCKET_BUILD_NUMBER # Log in to the ECR registry - aws ecr get-login-password | docker login --username AWS --password-stdin ${DOCKER_IMAGE%%/*} # Build image - docker build -f Dockerfile -t $DOCKER_URI . # Push image to private registry - docker push $DOCKER_URIpipelines: branches: main: - step: *buildImage${DOCKER_IMAGE%%/*} strips the repository name from the image URI and leaves the registry host, which is what docker login needs. Older versions of this pipeline used eval $(aws ecr get-login --no-include-email); that command was removed in AWS CLI version 2.
Variables whose names start with BITBUCKET_, such as BITBUCKET_BUILD_NUMBER, are provided by Bitbucket Pipelines. DOCKER_IMAGE and the AWS credentials are user-defined: add them as repository variables under Repository settings > Pipelines > Repository variables.
| Name | Description | Example |
|---|---|---|
DOCKER_IMAGE |
Amazon ECR URI for your image | 111111111111.dkr.ecr.ap-southeast-2.amazonaws.com/sct |
AWS_ACCESS_KEY_ID |
AWS access key of an IAM user or role | AKIAIOSFODNN7EXAMPLE |
AWS_SECRET_ACCESS_KEY |
Secret key for the access key | wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY |
AWS_DEFAULT_REGION |
AWS Region to send requests to | ap-southeast-2 |
Mark the secret key as a secured variable so that it is masked in the logs. Once the pipeline has finished, the new image tag appears in the ECR repository. From here you can add further steps for a complete build and deploy workflow.
Push to Docker Hub
This pipeline builds the application with Maven in a first step and passes the target directory to the next step as an artifact. The second step builds the Docker image, tags it with the commit hash and pushes it to Docker Hub:
image: atlassian/default-image:2
pipelines: branches: main: - step: name: Create Artifact caches: - maven script: - mvn -B package # -B (batch mode) makes Maven less verbose artifacts: # files passed on to the following steps - target/**
- step: name: Push docker image to the registry deployment: Production services: - docker script: # Set $DOCKER_HUB_USERNAME and $DOCKER_HUB_PASSWORD as repository variables - export IMAGE_NAME=your-Dockerhub-account/your-docker-image-name:$BITBUCKET_COMMIT
# Build the Docker image from the Dockerfile in the root of the repository - docker build -t $IMAGE_NAME . # Authenticate with the Docker Hub registry - echo "$DOCKER_HUB_PASSWORD" | docker login --username "$DOCKER_HUB_USERNAME" --password-stdin # Push the new image to Docker Hub - docker push $IMAGE_NAMEOnce the pipeline has finished, the new tag of your image appears in Docker Hub.
deployment links the step to a deployment environment, which Bitbucket uses to track what is deployed where. The three environment types are Test, Staging and Production. Test can be promoted to Staging, and Staging to Production. You can set up several environments of the same type in the Deployments settings, for example to deploy to different geographical regions separately.
Deploy to AWS Elastic Beanstalk with Bitbucket Pipelines
This larger pipeline deploys a PHP application as a single-container Elastic Beanstalk application. On every push to a release-* branch it:
- Runs
composer installin the application directory. - Builds a Docker image from the configuration in
environment/containers/production/eb_single_php_container_appand pushes it to Amazon ECR. - Creates a Beanstalk application version from the files in
environment/artifact, uploads it to S3 and registers it with Beanstalk. - Deploys to the staging environment, then to production. Both deployments are manual steps.
image: name: atlassian/pipelines-awscli
definitions: composerInstall: &composerInstall name: Install Composer image: composer caches: - composer script: - composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader artifacts: - 'application/vendor/**' buildImage: &buildImage name: Build and Push Docker Image caches: - docker services: - docker script: - export DOCKER_URI=$DOCKER_IMAGE:prod-$BITBUCKET_BUILD_NUMBER # Log in to the ECR registry - aws ecr get-login-password | docker login --username AWS --password-stdin ${DOCKER_IMAGE%%/*} # Build image - docker build -f environment/containers/production/eb_single_php_container_app/Dockerfile -t $DOCKER_URI . # Push image to private registry - docker push $DOCKER_URI sendToAws: &sendToAws name: Create EB Version script: # Set environment variables - export DOCKER_URI=$DOCKER_IMAGE:prod-$BITBUCKET_BUILD_NUMBER - export FILE_NAME=build-$BITBUCKET_BUILD_NUMBER.zip - export GIT_DESCRIPTION=`echo $(git log -1 --pretty=%B) | cut -c -199` - export VERSION_NAME=${BITBUCKET_BUILD_NUMBER}_${BITBUCKET_TAG:=$BITBUCKET_BRANCH} - cd environment/artifact # Put the image URI into the Beanstalk Docker file - sed -i "s|<IMAGE_URL>|$DOCKER_URI|g" Dockerrun.aws.json # Compress deployment artifact - zip $FILE_NAME -r * .[^.]* # Copy the artifact to S3 - aws s3 cp "$FILE_NAME" s3://$BUILD_BUCKET/$BUILD_PATH/"$FILE_NAME" # Create EB application version - aws elasticbeanstalk create-application-version --application-name "$APP_NAME" --version-label "$VERSION_NAME" --description "$GIT_DESCRIPTION" --source-bundle S3Bucket=$BUILD_BUCKET,S3Key="$BUILD_PATH/$FILE_NAME" deployStaging: &deployStaging name: Deploy to Staging deployment: staging trigger: manual script: # Set environment variable - export VERSION_NAME=${BITBUCKET_BUILD_NUMBER}_${BITBUCKET_TAG:=$BITBUCKET_BRANCH} # Deploy api - aws elasticbeanstalk update-environment --application-name "$APP_NAME" --environment-name "$APP_ENV_STAGING" --version-label "$VERSION_NAME" deployProduction: &deployProduction name: Deploy to Production deployment: production trigger: manual script: # Set environment variable - export VERSION_NAME=${BITBUCKET_BUILD_NUMBER}_${BITBUCKET_TAG:=$BITBUCKET_BRANCH} # Deploy api - aws elasticbeanstalk update-environment --application-name "$APP_NAME" --environment-name "$APP_ENV_PRODUCTION" --version-label "$VERSION_NAME"pipelines: branches: release-*: - step: *composerInstall - step: *buildImage - step: *sendToAws - step: *deployStaging - step: *deployProductionVERSION_NAMEcombines the build number with the tag or, when the build has no tag, the branch name.${BITBUCKET_TAG:=$BITBUCKET_BRANCH}falls back to the branch.GIT_DESCRIPTIONis the last commit message, cut to 199 characters to fit the Beanstalk description limit.Dockerrun.aws.jsoninenvironment/artifactcontains the placeholder<IMAGE_URL>, whichsedreplaces with the image just pushed.trigger: manualmakes the staging and production steps wait for someone to start them in the Bitbucket UI.
Required repository variables
Add these variables in Bitbucket for the pipeline to run. The AWS access key needs write access to the S3 bucket that stores the Beanstalk versions, and read and write access to Amazon ECR.
| Name | Description | Example |
|---|---|---|
AWS_ACCESS_KEY_ID |
AWS access key of an IAM user or role | AKIAIOSFODNN7EXAMPLE |
AWS_SECRET_ACCESS_KEY |
Secret key for the access key | wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY |
AWS_DEFAULT_REGION |
AWS Region to send requests to | ap-southeast-2 |
DOCKER_IMAGE |
Amazon ECR URI for your image | 111111111111.dkr.ecr.ap-southeast-2.amazonaws.com/sct |
BUILD_BUCKET |
S3 bucket where builds are stored | ct-builds-configs20190605042428563200000001 |
BUILD_PATH |
Path in the S3 bucket for builds | builds/sct |
APP_NAME |
Beanstalk application name to register the version with | EB Single Container App |
APP_ENV_STAGING |
Beanstalk environment name for staging | eb-single-container-app-staging |
APP_ENV_PRODUCTION |
Beanstalk environment name for production | eb-single-container-app-production |