Skip to content

Automated Deployment using Gitlab CI

You can use Gitlab CI/CD to deploy to Subkube directly from Gitlab.

Prerequisites

  • A Namespace in a Subkube Project
  • A Gitlab(.com) account

Steps

Prepare Application

For this guide we will be deploying Nextcloud, which we covered in our Kompose guide. The resulting manifests can easily be fetched from our examples repository, as shown below.

1
2
3
4
5
curl -LO https://github.com/subkube/examples/archive/master.zip
unzip master.zip
mkdir -p nextcloud-from-gitlab/manifests
cp -R examples-master/kompose/output-after/* nextcloud-from-gitlab/manifests/
cd nextcloud-from-gitlab

Prepare Repository

After fetching the resources from our examples repository, you should create a new Gitlab repository.

Create Github repository

Now we can initialize the local git repository, and set up Gitlab as it's origin:

1
2
3
4
5
git init
git add manifests/
git commit -m "first commit"
git remote add origin [email protected]:stud-io/subkube/nextcloud-from-gitlab.git
git push -u origin master

Setup (Partial) Gitlab Kubernetes Integration

In order to deploy to Subkube using kubectl from Gitlab CI, we'll need to add Subkube as a Kubernetes environment to Gitlab. You don't need to supply a PEM certificate, as you use the Subkube API proxy to interface with the Kubernetes API.

Create Kubernetes Gitlab Environment

Add Kubectl action

After we've setup our kubeconfig in Github, we can define an Action using this config to deploy to subkube. Define the following action inside the .github/workflows/deploy.yaml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
stages:
  - deploy

deploy-subkube:
  stage: deploy
  image: lwolf/helm-kubectl-docker:latest
  environment: SubkubeTest
  script:
    - kubectl config view
    - kubectl config unset clusters.gitlab-deploy.certificate-authority-data
    - kubectl apply -n poc-gitlab -f manifests/

Commit and Deploy

Now everything is setup, we can push our changes and watch them be applied to your Subkube project.

1
2
3
git add .gitlab-ci.yml
git commit -m "Add Gitlab CI deploy to Subkube job"
git push origin main

Our workflow should now be visible under the actions tab:

Github Action Output