This directory contains all the necessary files to run a local Gogs + Jenkins + Ansible pipeline.
- On any git push to the Gogs repository, it triggers the Jenkins pipeline through a webhook.
- Jenkins clones the Gogs repository, installs dependencies, runs tests, and builds the project.
- After the project is built, it is deployed to the production server by building a Docker image and pushing it to Docker Hub.
- Finally, Jenkins runs Ansible to install Docker, prometheus node exporter, configure the firewall (UFW), and run the Docker image.
Docker must be installed on the host machine as everything depends on Docker.
Create directories in the devops directory to mount as volumes for Gogs and Jenkins:
cd devops
mkdir gogs jenkins_home prometheus grafanaNow, build and run the Docker Compose services:
docker compose --build upThis will build the Jenkins, Prometheus and Gogs containers from jenkins_dockerfile and gogs_dockerfile, and start them.
Once the services are running, set up the Jenkins pipeline and credentials following the Jenkins Setup. Then configure your Gogs repository and push your code to it following the Gogs Setup.
Before pushing your repo to Gogs, don't forget to add your hosts to ansible/hosts file also prometheus/prometheus.yml file into the targets list.
- Jenkins Server: 8080
- Gogs: http: 3000, ssh: 8022
- Prometheus: 9090
- Grafana: 3001
The Docker Compose file builds the container from jenkins_dockerfile, which is based on the jenkins/jenkins:alpine image.
The following Jenkins plugins are installed:
- docker-workflow: Enables Docker integration in the pipeline to run Bun and build deployment images.
- ansible: Enables the use of Ansible within the pipeline.
- gogs-webhook: Triggers the Jenkins pipeline on Gogs webhook requests.
- slack: Sends notifications to Slack for pipeline updates.
Additionally, these packages are installed:
- docker-cli: The Docker command-line tool (without the engine).
- docker-cli-buildx: The Buildx plugin for Docker.
- ansible: To be used in the pipeline.
Installs the prometheus.prometheus Ansible collection which used to
install the node_exporter on the deployment server.
Finally, the scripts/jenkins_entrypoint.sh script is copied to the container. This script creates a Docker group with the same GID as the host and adds the Jenkins user to that group. It then starts the Jenkins entrypoint script.
The Docker Compose file builds the Gogs container from gogs_dockerfile, based on the gogs/gogs:latest image.
The gogs-config.ini file is copied to configure the Gogs server. Key settings include:
- Database type set to
sqlite3. - Default branch set to
main. - GZip enabled.
- SSH port set to 8022 (as configured in the Docker Compose file).
- Gogs uses its built-in SSH server.
- Captcha protection disabled.
- The
jenkinshost added to theLOCAL_NETWORK_ALLOWLIST. - The main config screen is disabled for the first run.
Also, the scripts/gogs_entrypoint.sh script is copied to the container. This script will set up the Gogs server with the gogs-config.ini on the first run
The Docker Compose file sets up Prometheus and Grafana containers using the prom/prometheus and grafana/grafana images.
To monitor your deployment server, add its IP address to the targets list in the prometheus/prometheus.yml file.
You can use Grafana to visualize the metrics collected by Prometheus.
For more details, refer to the guide: Prometheus and Grafana.
The ansible/ directory contains the Ansible.yml playbook, ansible.cfg, and the hosts file.
If the hosts file does not exist, create it and add your deployment server's IP address.
The playbook performs the following actions on the deployment server:
- Updates the apt cache.
- Installs Docker.
- Configures and enables the firewall (UFW).
- Installs prometheus node exporter.
- Runs the Docker image pushed to Docker Hub.
This is the Jenkins pipeline file. For more information, refer to the Jenkinsfile.
The deploy_dockerfile is used to build the deployment image by copying the dist/ directory (generated by running bun build).
The deployment image is based on the nginx:alpine image.
The scripts/ directory contains entrypoint shell scripts for the containers to perform tasks before executing the actual container entrypoint.
-
Gogs Entrypoint: Ensures that your configuration is properly set if it's the first time running the Gogs container.
-
Jenkins Entrypoint: Verifies that the Docker group has the correct GID and ensures the Jenkins user is added to the Docker group.