Setting up GitLab as a CI/CD and container registry for GitHub projects
Integrating GitLab Remote
We basically have two options to integrate GitLab in our project:
- Add a new remote.
- Add a new URL to the origin remote.
Add A New Remote
This solution gives you more control on when things get updated since you have to explicitly push to GitLab.
$ git remote add gitlab https://github.com/<USER>/<REPO>.git
Then to push to GitHub you would do the normal push to origin:
$ git push origin
And to push to GitLab:
$ git push gitlab
Add A New URL To Origin Remote
This option will mirror your content in GitHub, so everytime you git push
to GitHub it will also push to GitLab.
git remote set-url ––add origin https://github.com/<USER>/<REPO>.git
Connect GitLab for CI/CD
You can follow the instructions from here. It explains really well how to do this integration.
Connecting To The Registry
We want to use GitLab's Container Registry to store images of our projects. We first want to make sure we can push images locally, then we will setup the CI/CD pipeline.
Log into the GitLab registry:
$ docker login registry.gitlab.com
Then we have two commands to build and push images:
docker build -t registry.gitlab.com/<USER>/<REPO> .
$ docker push registry.gitlab.com/<USER>/<REPO>
Integration With CapRover
Add your GitLab credentials to integrate to a Container registry.
Default name format is as such:
gitlab+deploy-token-<app-name>
You set this in the Cluster tab by Add Remote Registry.
Note that by default your registry will be set as the default registry for pushing new images. This means that if you build an image in CapRover it will push the image to GitLab.
This might be what you want but be aware that building images consumes memory and adds load to the server, which is fine if you are just playing around and don't have much load or have a machine that is beefy enough. This feature is only needed if CapRover is running as a cluster.
You can disable this by going to Cluster > Default Registry Configuration > Default Push Registry
and edit to disable push.
This is the format for your images[^tags] in a captain-definition file format:
{
"schemaVersion": 2,
"imageName": "registry.gitlab.com/goliatone/owerwatch:v0.0.12"
}
[^tags] Try to avoid using latest
tag, eventually it will bring pain in the form of weird bugs.
Deploying Container Image from CapRover CLI
The CLI tool takes one flag --imageName
that should be a reference to your image.
caprover deploy -h https://captain.root.domain.com -p password -a app-name --imageName private.registry.com/username/new-image:version
The following bash script provides two functions, gitlab:build
to build and tag an image and gitlab:push
to push the image to the registry:
#!/bin/bash
# Build image and tag. You can pass a tag
# as the function's only argument. If you don't
# provide a tag we try to use a `git` tag.
# If there are no `git` tags we default to **latest**.
#
# arg {1} Tag
function gitlab:build {
TAG=${1:-$(git describe --abbrev=0 --tags)}
if [ -z "${TAG}" ]; then
TAG=latest
fi
docker build --no-cache --tag "$PROJECT_REPO:$TAG" .
}
function gitlab:push {
TAG=${1:-$(git describe --abbrev=0 --tags)}
if [ -z "${TAG}" ]; then
TAG=latest
fi
docker push "$PROJECT_REPO:$TAG"
}
The script expects an env var PROJECT_REPO
that would be in the format registry.gitlab.com/<USER>/<REPO>
. For example it could be registry.gitlab.com/goliatone/overwatch
.