Use Duplo to update a service's container from Gitlab CI/CD
The goal of this section is to show how you can update the docker image for a service, after you have built that image.
This example makes some assumptions:
Your workflow already has a build
job - we created one in the previous section
Duplocloud Services
Use Duplo to build and push a docker image from Gitlab CI/CD
The goal of this section is to show how you can build a docker image and push it to DockerHub.
It does three basic things:
Logs in to DockerHub
Builds and tags your docker image, with the tag based on the git commit SHA.
Pushes your docker image
Here is an example gitlab workflow that builds a docker image and pushes it to DockerHub.
To use it you will need to change:
DOCKERHUB_USERNAME
variable
DOCKERHUB_REPO
variable
DUPLO_HOST
variable
DUPLO_SERVICE_NAME
variable
TENANT_NAME
variable
The goal of this section is to show how you can build a docker image and push it to Amazon ECR.
It does three basic things:
Logs in to Amazon ECR
Builds and tags your docker image, with the tag based on the GitLab CI Pipeline execution id.
Pushes your docker image to ECR
Prerequisite - A repository in ECR must have been created before proceeding with the next steps.
This process uses DuploCloud API Token (refer DuploCloud API Token) to gain access to AWS ECR.
Go to GitLab > Settings > CI CD > Variables > Expand and ensure that DUPLO_TOKEN variable is set and has correct value. Check the Protect Variable and Masked options for security purposes. You can refer to Configuring GitLab for the steps to setup a service account and to create a token for the newly configured account. The service account must have admin role.
The script uses amazon/aws-cli image as the base running image and uses Docker-in-Docker (docker/dind) to run the Docker commands. It uses duplo_utils.sh script from DuploCloud to get configuration from the DuploCloud instance.
Here is an example gitlab workflow that builds a docker image and pushes it to DockerHub.
To use it you will need to change:
DOCKER_REGISTRY
variable
DOCKER_REPO
variable
DUPLO_HOST
variable
DUPLO_SERVICE_NAME
variable
TENANT_NAME
variable
AWS_DEFAULT_REGION
variable
APP_NAME
variable
CI/CD using GitLab
DuploCloud uses cURL commands to support Continuous Integration and Deployment (CI/CD) in GitLab
This section discusses how you can configure Gitlab to integrate with Duplo
To interact with your Duplocloud Portal from GitLab CI/CD, you need to follow these steps.
First you need a service account in your portal which has the needed permissions.
Note: A 'service account' user in DuploCloud is just a user whose user name is not an email address, such as gitlab-bot
or simply gitlab
. These users are not able to use the web portal.
Now we can save the information about the service account within the Gitlab CI/CD Environment variables. You mainly need to set DUPLO_HOST
and DUPLO_TOKEN
variables.
If using GKE or Azure you will also need CLOUD_CREDENTIALS
and CLOUD_ACCOUNT
which you obtain when creating a service account in step 1.
Gitlab does have the option to set these variables at a variety of level scopes; organization, group, project. For example, you can set variables at the organization level and then all projects would be able to interact with your portal.
Gitlab has a unique feature to scope variables into tiers as well. This is very useful when you have many instances of an app within some tiers like dev, stage, prod. With this you can scope a variable like this dev/*
which means any Gitlab Environment with that prefix will inherit this variable. This means you can make all dev environments share the DUPLO_HOST
variable. This can group tenants under namespace as well, so you can rename your dev01
tenant to be a dev/dev01
to be an environment under a tier in Gitlab CI. This is particularly useful with multiple Duplocloud Portals.
The rest of this documentation will assume all the variables needed for service account access are set.