How To Build a Deployment Pipeline With Gitlab CI/CD and Cloud Foundry

Shedrack Akintayo
Cloud Foundry Foundation
6 min readFeb 16, 2021
Cloud Foundry and Gitlab CI/CD logos

Introduction

GitLab is one of the most popular tools for source code management and DevOps lifecycle available today for developers. A lot of engineering teams and also developers today use GitLab to help automate their entire DevOps workflow.

GitLab offers a bunch of use cases for software developers and teams like:

  • Source Code Management
  • Auto DevOps
  • Continuous integration (CI/CD)
  • DevSecOps

Cloud Foundry, on the other hand, offers developers the best developer experience on Kubernetes. This means that the Cloud Foundry for Kubernetes(cf-for-k8s) project can solve your Kubernetes problems without you worrying about the difficulty that comes with using Kubernetes for your project.

The CI/CD use case of GitLab is probably its most popular use case, in this tutorial I’ll show how to can use the GitLab CI/CD and Cloud Foundry for Kubernetes to build an automated deployment pipeline.

Benefits of Using Cloud Foundry

The benefits of using Cloud Foundry (cf-for-k8s) with GitLab CI/CD tool includes:

  • All projects are pushed from the source — This means that the source code management aspect of GitLab is responsible for all the operations that will happen in the Cloud Foundry environment. So this means that deployments on various branches will always be up to date.
  • The cf-for-k8s project makes use of a cloud-hosted container registry during the cf push process. When the application needs to be deployed, it is packaged as a container using Buildpacks and first uploaded to a container registry. It is then pulled from here and then deployed to the container runtime/Kubernetes-powered infrastructure. This means that no extra configurations for a container registry is required.

So how do we build a deployment pipeline that uses Cloud Foundry and GitLab?

Let’s quickly take a look at the steps needed to be taken in order to make it happen.

Building The Deployment Pipeline

Before starting, an account on GitLab will be needed. Follow the steps from the official GitLab docs to create your account.

As a prerequisite, install the cf-for-k8s project on an infrastructure provider of your choice. Use the linked pages to deploy either on Google Kubernetes Engine or using the DigitalOcean Kubernetes Service. For the purpose of this tutorial, I have installed the cf-for-k8s project on my Google Kubernetes Engine cluster.

A screenshot of my terminal showing that I'm logged into a CF Cluster

Next, Pick a sample repository that is available on GitLab. You can use existing repositories or create a new one.

Here is an example of a GitLab repo

A screenshot of a Gitlab repo

In this repo, we will add a .gitlab-ci.yml file.

This is a YAML file that contains the GitLab CI/CD configuration for your project and it contains is everything you need to build the CI/CD pipeline on GitLab.

The contents of the .gitlab-ci.yml file we will be using are:

In the above YAML configuration, we are telling GitLab CI/CD steps to take (stages) whenever we make a push to our application.

stages:
— build
— deploy

In this part of the code, we are telling GitLab CI/CD that our pipeline should have two stages which in this case is “build” and “deploy”.

build:
image: node:14
stage: build
script:
— npm install
— npm audit fix
only:
— master

In this part of the code, we are specifying the scripts that should be executed when the pipeline gets to the build stage.

.login:
image: node:14
before_script:
— apt-get update
— apt-get install -y ca-certificates jq
— echo “deb [trusted=yes] https://packages.cloudfoundry.org/debian stable main” > /etc/apt/sources.list.d/cloudfoundry-cli.list
— apt-get update
— apt-get install cf7-cli
— cf — version
— cf api — skip-ssl-validation “$CF_API”
— cf auth “$CF_USERNAME” “$CF_Password”
only:
— master

Over here, this is a .pre stage, the script here gets executed before the next job. In this stage we are installing the Cloud Foundry CLI in our app container and setting all the appropriate value.

The values starting with a dollar sign ($) are environmental variables that you can set in your repo by clicking on settings on the left side bar of the screen and selecting CI/CD

A screenshot of Gitlab left sidebar

After clicking on CI/CD, scroll to the bottom and locate the Variables option, click on expand and select add variable. Fill in the fields with the appropriate environmental variables.

Here is a GIF showing the process

A gif showing how to add environmental variables in Gitlab
deploy:
extends: .login
stage: deploy
script:
— cf target -o “foo” -s “gitlab-test”
— ls -la
— echo “Deployment starting…..”
— cf push exchange-rate-spa
only:
— master

In this part, this is the last stage which we named deploy. Over here, we are moving into the correct space and organisation of our choice with support from the CL CLI we have installed in the .login stage and then pushing our application to cf-for-k8s that we have installed on an infrastructure.

This entire process will run whenever we make an update to the code and push to our GitLab repo. This is made possible by GitLab Runner which is a trigger application that works with GitLab CI/CD to run jobs in a pipeline.

You can use a shared runner for your project if you don’t want to install a dedicated one for your project.

Check out the GitLab Docs for more information about Runners.

To see this in action, when I make an update to the code and push to this Gitlab repo, then click on CI/CD on the left side bar and select pipelines

A screenshot of Gitlab left sidebar

A deployment pipeline will start and show up on the screen and automatically begin the deployment process.

A screenshot of gitlab pipelines menu

When each job has passed successfully, you should see a green check mark on the icons in the stages row like this.

A screenshot of gitlab pipelines menu

If there’s an error in any stage, GitLab will alert you and you can check through the job logs to see what caused the job to fail. You can also check the logs of a specific job by clicking on any of the icons in the stage row and select the job name.

A screenshot of gitlab job logs

Let’s check the deploy job logs to get our application route since the job was successful.

A screenshot of gitlab job logs

From the logs, we can see that our app has been deployed successfully and a route has been generated for us to view our application.

Once you set up this entire pipeline, any modified code that is pushed into the GitLab repo will trigger a fully automated build and subsequent deployment to Cloud Foundry.

This guide is expected to provide an introduction to automating application deployments made using Cloud Foundry. A highly efficient and modern means of cloud-native application delivery over Kubernetes-based infrastructure is made possible using Cloud Foundry. Tutorials such as these are meant to address the needs of all types of software development teams.

For more information, please visit cloudfoundry.org. The best way to connect with the Cloud Foundry community is to join our Slack Workspace at https://slack.cloudfoundry.org/. Those in the Slack community will help you get answers to any queries you may have about using Cloud Foundry.

--

--