In December of 2017 I was working on a Java EE web project from my startup.
One of the first things that needed to be defined was where to host the application.
After studying Heroku, Openshift, AWS and Google Cloud, I decided to use Docker containers on Google Cloud infrastructure.
Google Cloud with Docker container lets you use all the power of the Java EE stack. It has low prices, ease of configuration, scalability and platform independence.
As it was a private project, I chose to use Bitbucket because it has Git and pepilines to do the integrations with Google Cloud and Slack.
The following are the settings required to run the continuous integration and continuous deployment pipelines:
At the root of the Java project two files are required:
/Dockerfile
FROM jboss/wildfly | |
ADD ./target/app.war /opt/jboss/wildfly/standalone/deployments/ |
/bitbucket-pipelines.yml
# This is build configuration for Java and Docker. | |
image: maven:3.3.9 | |
options: | |
docker: true | |
pipelines: | |
custom: | |
schedule-ci: #Continuous Integration | |
– step: | |
caches: | |
– maven | |
script: | |
– mvn clean package | |
deployment-to-prod: #Deployment to Production | |
– step: | |
caches: | |
– maven | |
script: | |
– mvn clean package | |
# Downloading and installing gcloud | |
– curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-161.0.0-linux-x86_64.tar.gz | |
– tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/ | |
– /tmp/google-cloud-sdk/install.sh -q | |
– source /tmp/google-cloud-sdk/path.bash.inc | |
# Installing kubectl | |
– gcloud components install kubectl -q | |
# Authentication | |
– echo $GCLOUD_API_KEYFILE | base64 –decode –ignore-garbage > ./gcloud-api-key.json | |
– gcloud auth activate-service-account –key-file gcloud-api-key.json | |
– gcloud config set project $GCLOUD_PROJECT | |
– gcloud container clusters get-credentials $GCLOUD_CLUSTER –zone=$GCLOUD_ZONE | |
# Building images | |
– docker build . -t gcr.io/$GCLOUD_PROJECT/$APP_NAME:$BITBUCKET_COMMIT | |
# Pushing SHA1 image | |
– gcloud docker — push gcr.io/$GCLOUD_PROJECT/$APP_NAME:$BITBUCKET_COMMIT | |
# Pushing latest image | |
– docker tag gcr.io/$GCLOUD_PROJECT/$APP_NAME:$BITBUCKET_COMMIT gcr.io/$GCLOUD_PROJECT/$APP_NAME:latest | |
– gcloud docker — push gcr.io/$GCLOUD_PROJECT/$APP_NAME:latest | |
# Deploy the image | |
– kubectl set image deployment/app-jee app-jee=gcr.io/$GCLOUD_PROJECT/$APP_NAME:$BITBUCKET_COMMIT | |
You must add three Environment variables:
Go to your repository settings in Bitbucket and navigate to Pipelines > Environment variables. Create a new variable named GCLOUD_API_KEYFILE and paste the encoded service account credentials in it.
Add a new variable called GCLOUD_PROJECT and set the value to the key of your Google Cloud project.
Add another variable called APP_NAME and set the name of your app.
In Pipelines you can configure the build schedule.
See also:
https://confluence.atlassian.com/bitbucket/deploy-to-google-cloud-900820342.html
https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app