In a first post about Tanzu Platform for Kubernetes I walked you through the process of bringing an application “from code to production” on Kubernetes using four simple commands: build, deploy, bind and scale.
VMware Tanzu Platform provides everything that you need to build application development environments, develop applications, and push those applications to production quickly, consistently, scalably, and securely.
In this earlier blogpost I’ve explained how to setup TP for Kubernetes, create a project & a space, and how to interact from the command-line with Tanzu Platform. For the build process of the app, a local Docker (compatible) runtime is required to create the container(s) and push them to a registry. After this process is completed, Tanzu Platform for Kubernetes deploys the app to the selected Availability Targets (Kubernetes clusters) on Tanzu Platform and pulls the pre-build image from the registry.
In this blogpost we will explore how we can further automate this process and integrate TP for K8S with Gitlab CI/CD. This Gitlab repository you will find some files that will help you to get started!
Prerequisites
Of course you need to setup Tanzu Platform to get started, you will need to configure a Project and Space to be able to deploy an application, more info on this is available in this blogpost.
On top of that, you will need the files in the GitLab repository, a container registry and the following credentials and API tokens:
- Username and password for the container registry (read/write access)
- API Token for accessing Tanzu Platform for Kubernetes.
The API token must be configured as follows:
Setup your Gitlab project
Now let’s setup the Gitlab project that will use Tanzu Platform for Kubernetes. We will need to configure a few things to get started:
- You project needs to have a tanzu.yml in the root of the project, including the .tanzu/config folder that holds additional information about your app.
- You need to setup a few variables in your Gitlab project: REGISTRY_USER, REGISTRY_PASSWORD and TANZU_API_TOKEN. The variables are available as environment variables in the pipeline and are used to succesfully run the script.
- You need to copy, rename and configure the .gitlab-ci.yml to your own project.
You can either clone and use my “Hello World” like app called nl-tanzu-java-web-app, or use your own application. You need to run
tanzu app init
to initialize your application, this will setup tanzu.yml and the contents of the .tanzu/config folder. If your app requires external connectivity, you need to setup something like this and put this file in the .tanzu/config folder. For the nl-tanzu-java-web-app demo app, the files are already there.
Now you need to setup some variables in your project:
The last thing you need to do is copy gitlab-ci-example to your own project and rename it to .gitlab-ci.yml. You need to update a few variables in this file, or add these variables directly to the project (the same as you did with REGISTRY_PASSWORD/USER and TANZU_API_TOKEN). An example .gitlab-ci.yml can be found in the nl-tanzu-java-web-app project.
The new pipeline will automatically run after an update on the Project, or you can start it manually under Build->Pipelines. The pipeline will automatically executed on SAAS based Gitlab runner, using the Docker-in-Docker image. The Docker-in-Docker image is used to get access to the Docker runtime, another option is that you have your own Gitlab runner available that has the Docker executable as well as the Tanzu CLI tool available.
The result of an actual run will look like this:
That’s it!
If you require any specific configurations (for example you want to deploy pre-build images), you might want to fork my Tanzu Deploy CI-CD repo and update it according to your needs. Don’t forget to update the includes in your .gitlab-ci.yml file, so that they point to your customized files.
My colleague Corby Page created some other examples for the tooling of your preference:
- Azure DevOps: https://github.com/cpage-pivotal/tanzu-deploy-ado-pipeline
- Jenkins: https://github.com/tanzu-build/jenkins
- Github Actions: https://github.com/cpage-pivotal/tanzu-deploy-action
I hope this was helpful, feel free to reach out in case you have any questions.