I have created a generic API webserver sample project which covers a lot more. Check out the API Webserver w/ Docker, Caddy, and CI/CD.
A generic Gitlab CI/CD
pipeline which has the ability to build a Docker
image on one machine and pull and run the image on another.
In order to get the full benefits of this CI/CD pipeline, you will need to have two separate servers
. One which you treat as the build
server and one which you want to deploy
to.
If you wish to use one machine for everything, you can do so as well by registering your Gitlab Runner
to have the build
, docker
, and deploy
tags on it.
- The
build server
stores multiple images that otherDocker
builds can reuse - The
deploy server
only has one finalized image / container which it runs- Drastically reduces space
- Separation of concerns
- Great for
Microservices
- Build once, deploy to many
- Install
Docker
andGitlab Runner
onto thebuild
anddeploy
servers - Initiate the
Gitlab Runner
registration on both servers - For the
build
server, addbuild
anddocker
as the tags - For the
deploy
server, adddeploy
as the tag - Set
shell
as theexecutor
on both servers
Feel free to modify the docker-compose.yml or the .gitlab-ci.yml files to satisfy your needs. You can largely ignore the Dockerfile as it pertains mainly to go
.
This is just one way to structure the project but the CI/CD pipeline will be roughly the same for other languages as well.
- Runs a simple
go
web server which returns "Hello World" on port 3000 - The
Dockerfile
builds the application and its tests in two separate binaries - The
docker-compose.yml
file has two separate services - One for the application and one for the tests - The
.gitlab-ci.yml
file runs the tests throughdocker-compose
- The
Docker
image name is calledgo-docker
which is later referenced in the.gitlab-ci.yml
file - You'll need to update the image name for your project - The image name that gets pushed out to Gitlab's Docker Registry uses the following format:
<GitLab project registry URL>:<branch name>
- An example of the final image name is
registry.gitlab.com/nikitabuyevich/go-docker:master
- Feel free to change the tag to satisfy your needs
- An example of the final image name is
- Anything that references
$CI_*
is a specificGitlab CI/CD
environment variable - See Predefined environment variables
If you are planning to run CentOS as your operating system on your servers, you can utilize the Bootstrap / Harden CentOS bash script which will secure your server and install and register Docker
and Gitlab Runner
.
- Docker - Build and run images / containers
- Gitlab Runner - Integrate a Gitlab CI/CD pipeline with a server
- Gitlab CI/CD - A tool built into GitLab for software development through continuous integration, delivery, and deployment.
- Nikita Buyevich - nikitabuyevich.com
This project is licensed under the MIT License - see the LICENSE file for details.