This is a Drone CI/CD example for single machine. I build up this example to show a simple CI/CD use case: build docker image, test, and then deploy the system. I assume that you have the drone server and agent on the same machine, and you also want to deploy your system to that machine. This setup helps us build up an example easier because we don't need to handle many secrets. Don't use this example directly if someone you don't trust can push or send pull request to your repository.
-
We will use GitHub's OAuth to login and get the repository, so you should register a new OAuth application here.
-
Authorization callback URL should be the URL you will run your drone server with
/authorize
suffix. For example, if you want to run your server onhttp://123.123.123.123:8000
, then you must puthttp://123.123.123.123:8000/authorize
here. Note that this must be a public IP, or GitHub cannot trigger the webhook. -
You can fill in whatever you want in other fields.
-
Click Register application and then you will get your Client ID and Client Secret. You will need them later.
-
Install the latest version of Docker and Docker Compose.
git clone https://github.com/ianlini/drone-example.git
cd drone-example
- You may want to fork this repository and clone your own repository so that you can push your commit.
Add .env
to drone-server/
with the following content:
DRONE_ADMIN=...
DRONE_HOST=...
DRONE_SECRET=...
DRONE_GITHUB_CLIENT=...
DRONE_GITHUB_SECRET=...
DRONE_ADMIN
: your GitHub usernameDRONE_HOST
: your host address (e.g., http://123.123.123.123:8000)DRONE_SECRET
: generate a random string and put it hereDRONE_GITHUB_CLIENT
: the Client ID you got in the previous stepDRONE_GITHUB_SECRET
: the Client Secret you got in the previous step
You can start your server with the docker-compose command now:
cd drone-server
docker-compose up -d
Now you can access your Drone web server from http://123.123.123.123:8000, and login using GitHub's OAuth.
-
You should be able to understand the following steps on the GUI easily, but I still provide the URL in case you cannot find them.
-
Turn on your repository in the repositories page (e.g., http://123.123.123.123:8000/account/repos).
-
Set the repository to be trusted because we want to link the volume in
.drone.yml
.- If your repository is
ianlini/drone-example
, then the setting page may be something like http://123.123.123.123:8000/ianlini/drone-example/settings. - This is a dangerous setting, make sure you know what this is if you are not just testing.
- If your repository is public, you can also disable pull request in Repository Hooks so that no one can trigger your webhook using pull request.
- If your repository is
-
Now you can try to trigger the webhook by pushing.
-
After pushing, you will see the build here: http://123.123.123.123:8000/ianlini/drone-example/.
-
You can also see that the image is built on your machine (
build-docker
step in the pipeline), and the flake8 (python linter) is run using that image (flake8
step in the pipeline). -
Note that only
build-docker
andflake8
is run here because we setwhen
for other steps in the pipeline so that they won't be triggered when pushing.
-
The deployment can only be triggered using the command-line tool.
-
Install the tool following the instruction here.
-
Find your token here: http://123.123.123.123:8000/account/token. This page will tell you how to use the token:
export DRONE_SERVER=...
export DRONE_TOKEN=...
drone info
- Now you can deploy a previous build using the command:
drone deploy {your repository} {build number} {environment}
- Example:
drone deploy ianlini/drone-example 3 production
-
This will trigger the
deploy-production
step in the pipeline. -
Run
docker ps
and you will see that your containerdroneexample_drone-example_1
is running. -
Run
docker logs droneexample_drone-example_1 -f
and you will see that it is printing the following things periodically:
Hello world!!
Your secret is ''.
- The secret is empty here because you haven't set your secret yet.
-
We pass a secret to the python file
hello_world.py
to show the power of the secret management in Drone. You should set it before deployment. -
Go to http://123.123.123.123:8000/ianlini/drone-example/settings/secrets and set a secret:
- Secret Name:
example_secret
- Secret Value:
this is a secret
- Secret Name:
-
Now try to deploy again.
-
Run
docker logs droneexample_drone-example_1 -f
and you will see that it is printing yourexample_secret
periodically.
- Trigger
build-docker
andflake8
:
drone exec --build-event push --commit-sha latest
- Trigger
deploy-local
:
EXAMPLE_SECRET='this is a local secret' drone exec --build-event deployment --build-target local --commit-sha latest