Finish documentation for setting up CI/CD
Compare changes
+ 141
− 17
CICD stands for continuous integration and continuous deployment and is a procedure that automates building, testing, distributing, and deploying code in a variety of formats. [RedHat](https://www.redhat.com/en/topics/devops/what-is-ci-cd) has a good summary of CI/CD and why it's useful. Here are a few examples of what CI/CD can automate:
@@ -11,6 +13,17 @@ CICD stands for continuous integration and continuous deployment and is a proced
CI/CD Setup can be split into two separate sections, one section for setting up the code repository to use CI/CD and the other section for creating the Cloud VM that will actually run the CI/CD pipeline you create. While most of the steps for each section will not overlap with the other, there will be a couple of steps at the end needed to set the repo to use the VM.
> This guide uses Docker as the build engine to create Docker images. Docker has significant security concerns due to the fact it requires `root` privileges to run, and using the Docker-in-Docker service (necessary for Docker to build Docker images) requires you to essentially disable all built-in security.
> Docker was still chosen here due to being nearly ubiquitous as an image/container service. It is critical to make sure your runner VM is ONLY EVER USED FOR CI BUILDS. NEVER put any sensitive information or access keys on the VM, and make sure only trusted individuals have the ability to run CI/CD jobs for any repo.
@@ -24,7 +37,7 @@ CI/CD can be implemented using most modern code repositories (ex. Github, Gitlab
@@ -33,19 +46,13 @@ This guide will assume basic understanding of how to use both services. If you d
Gitlab is a separate implementation of remote git repositories than Github but functions identically at a basic level. In addition to the guides, our facilitation team would be happy to assist in learning how to use either git or Cloud during our [open office hours on Zoom](https://docs.rc.uab.edu/#how-to-contact-us)
Setup can be split into two separate sections, one section for setting up the code repository to use CI/CD and the other section for creating the Cloud VM that will actually run the CI/CD pipeline you create. While most of the steps for each section will not overlap with the other, there will be a couple of steps at the end needed to set the repo to use the VM.
To implement CI/CD in a Gitlab repo, you will need to create a `.gitlab-ci.yml` file in the repo root as well as specify a runner to execute the CI. The `gitlab-ci` specifies all of the commands to be run to replicate a build and deploy process. These can range from fairly simple to very complicated depending on the pipeline.
@@ -57,7 +64,7 @@ default:
@@ -74,7 +81,7 @@ variables:
@@ -89,7 +96,7 @@ Defines the different stages of the CI pipeline:
@@ -111,7 +118,7 @@ build_package:
@@ -125,7 +132,7 @@ test_package:
@@ -138,7 +145,7 @@ publish_pip:
@@ -158,7 +165,124 @@ build_and_push_docker_image:
> We use Docker as the build engine due to its flexibility in terms of dependency installation. As long as an image exists that contains a necessary dependency or the dependency can be installed into a container running a Linux OS, Docker can build it without needing to modify any of the software on the runner VM itself. This makes builds much easier to manage than building using the Runner's system packages although it does increase the compute requirements.
- This can be altered based on what your CI/CD pipeline is doing exactly. If you are building large Docker images to store in the container registry, you will most likely want to increase the volume size beyond 100 GB since Docker will cache layers for both the host image and built images as well as volumes and containers. If you are only building small Python packages or compiled binaries, you could lower the volume size since Docker would only store layers for the host images. Be sure to clean the Docker cache regularly on the VM to maintain performance.
Up-to-date instructions for installing Gitlab Runner can be found on [Gitlab's Documentation](https://docs.gitlab.com/runner/install/linux-repository/#install-gitlab-runner). You will only need to follow the first two steps if this VM did not have Gitlab Runner installed on it previously. The commands from those steps are replicated here for convenience but are subject to change at any time on Gitlab's site. Always use the original documentation if you run into installation issues.
Official instructions for installing Docker can be found on their [website](https://docs.docker.com/engine/install/ubuntu/#installation-methods). They include 4 methods for installing Docker of which I suggest using the [apt repository](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository). The full instruction set can be found below, but the official instructions are subject to change at any time. Please use the official documentation if you run into any installation errors.
After docker is installed, it can be somewhat cumbersome to use as-is since it requires root permissions to run. This involves typing sudo before every `docker` command and makes using it with gitlab-runner difficult/nigh impossible. Instead, you can follow the [post-installation instructions](https://docs.docker.com/engine/install/linux-postinstall/) to make life easier. This involves giving both the `gitlab-runner` and `ubuntu` users root permission to run docker by default.
This command sets some default runner parameters including the executor, the docker image to use, and telling docker to run in privileged mode, a requirement for a Docker container to build other Docker images. Feel free to change the docker image version, but it's not suggested to use `docker:latest` as that version does change over time without notice, and you may not be able to replicate certain builds if they use different versions of Docker.
> If you chose to lock the runner to currently assigned projects, you will not see it listed in the available runners list for any other projects. You can create a new registration for the same runner on other projects you may want to assign it to. A single runner can have multiple registrations, and those registrations can be any combination of project, group, or instance.
If after you've registered the runner, there are settings you'd like to change about it, you can edit the `/etc/gitlab-runner/config.toml` file on the VM. You will need `sudo` permissions to make any changes. See [Gitlab's documentation](https://docs.gitlab.com/runner/configuration/advanced-configuration/) for a full breakdown of different options you can use in the configuration file.
With the example CI/CD pipeline, builds will automatically start when any commits are pushed to the `main` branch or when a pipeline is started from the web interface. You can start a pipeline manually by going to `Build > Pipeline > New Pipeline`. You can also find the logs for current and past jobs by clicking on the different pipelines.
You can also have the pipeline run in response to various conditions and actions. For instance, you can run a pipeline auntoamtically any time you push a tag to the repository. A tag is a convenient mechanism for permanently marking certain important commits, such as the last commit for a version release. That tag can be referenced in the CI/CD pipeline and a package and/or image automatically created for it. See the [Gitlab rules](https://docs.gitlab.com/ci/jobs/job_rules/) section in their documentation for more examples of specifying when a pipeline should run.