Both gitea and drone is built on golang runs on multiple platforms including a raspberry pi and its super lightweight. Oh yes, and its awesome!
In this tutorial we will see how we can implement our own git service and cicd platform by setting up gitea and drone on docker and commit a python flask application to gitea and build a pipeline on drone.
Some definition
Gitea: will be your self hosted git server
Drone Server: will be server being responsible for the web service, repositories, secrets, users, etc.
Drone Agent: will be the workers that runs your builds, jobs etc.
Server Confguration
We will change our host’s ssh port to something else as our git server’s ssh method will be listening on port 22 and we would like to add our ssh key to gitea so that we can commit to our git server via ssh.
Change your server’s ssh port to 2222, by opening /etc/ssh/sshd_config
and edit the port to 2222
then restart sshd with:
1
|
|
Your ssh connection will still be established, but you can exit and ssh to your server by specifying the new port:
1
|
|
Pre-Requirements
Make sure you have docker and docker-compose installed
Deploy Gitea and Drone
Below is the docker-compose file for our deployment. Note that we are running a postgres database which gitea will be configured on, you can also use other databases like mysql, sqlite etc. Visit their documentation for more info.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
Create the volumes path:
1
|
|
Create the external network:
1
|
|
Some key configuration,
Our SSH_DOMAIN
will be the domain that gets used for things like cloning a repository. When you register your gitea account, you will use the same credentials to logon to drone.
Deploy your stack:
1
|
|
Register on Gitea
When your deployment is done, access your gitea server which should be available on http://your-docker-ip:3000/
complete the registration, if you decide to go with postgres your username/password will be postgres
and your host will be gitea-db:5432
.
Make sure to complete the administrator account to get your admin credentials.
Setup SSH Key and Repo
Generate a ssh key that you will use for communicating to git over ssh. If you have already have an ssh key you can skip this step.
1 2 |
|
Login on gitea, once you are logged in, head over to profile / settings / ssh keys: http://your-docker-ip:3000/user/settings/keys
.
Add a new ssh key, go back to your terminal and copy the public key which we will provide to gitea:
1 2 |
|
Paste your public key and provide a descriptive title.
Head back to your dashboard and create your first repository:
It should look more or less like this:
Enable Repo on Drone
Head over to drone and select the repositores on the right hand side http://
Once its enabled head back to gitea.
Clone the repository
On your repository select ssh and copy the ssh link for your repository:
Then from your terminal add your private ssh key to your ssh agent and clone the repository:
1 2 3 |
|
Add Example Python Flask app to git
I will use a basic python flask application with some tests.
Let’s first add our pipeline definition for drone, so that drone understands how the pipeline should be run when gitea receives a commit:
1
|
|
Our pipeline:
1 2 3 4 5 6 7 8 9 10 |
|
Lets add that to git:
1 2 3 |
|
Our drone file should be in git, now our requirements dependency file for python:
1 2 3 |
|
Our blank file to make our application a module:
1 2 |
|
And our flask app:
1 2 3 4 5 6 7 |
|
Our tests directory and our python init file:
1 2 |
|
Now that we have all our files ready, commit and push to git:
1 2 3 |
|
Look at Drone Running
Head over to drone and look at the builds, you should see your build running at http://<docker-ip>:80/<user>/<repo-name>
:
If everything ran as expected you should see that it passed.
Build Status Badges
You can also include the build status badges from drone which will look like:
You can use the drone api: http://drone-ip:80/api/badges/<your-user>/<your-repo>/status.svg
1 2 3 4 5 |
|
Overall gitea and drone is really amazing and quite impressed with it, especially from the low memory footprint and that its so easy to work with.
Resources
Have a look at this for more resources:
Docs:
- https://github.com/drone/drone
- http://plugins.drone.io/drone-plugins/drone-docker/
- https://docs.drone.io/reference/server/drone-user-create/
- https://docs.gitea.io/en-us/install-with-docker/
Examples:
- https://hackernoon.com/build-your-own-ci-cd-pipeline-with-drone-e43d7190989b
- https://github.com/drone-demos/drone-with-python
- https://github.com/drone/awesome-drone
- https://github.com/juliantellez/drone-ci-pipeline
- https://github.com/sguter90/docker-compose-gitea
- https://gist.github.com/joffilyfe/1a99250cb74bb75e29cbe8d6ca8ceedb
- https://florian-latifi.at/2018/02/21/using-drone-to-build-and-deploy-a-jekyll-site/
- https://angristan.xyz/host-your-own-ci-cd-server-with-drone/