[DroneCI] How to Specify Pipelines to Run on the Particular Drone CI Runner
Let’s say you have multiple Drone CI runners. By some reasons, you want to route pipelines to a particular Drone CI runner. How do you do that?
The answer is to use the DRONE_RUNNER_LABELS
environment variable to label your Dronce CI runner and in the Drone CI pipeline, specify the the label value under the node
section.
Scenario
Without examples, it’s hard to understand. So let’s go through an example.
The scenario is that you have two Drone CI Runners. You want one of them to only run the pipeline for staging environment and the other one to only run the pipeline for production environment.
Step 1: Label Drone CI Runner
For the runner where we want to run the pipeline for production environment.
1version: "3"
2services:
3 drone_runner:
4 image: drone/drone-runner-docker:latest
5 container_name: drone-runner-production
6 restart: always
7 environment:
8 - DRONE_RPC_HOST="drone-server.example.com"
9 - DRONE_RPC_PROTO=https
10 - DRONE_RPC_SECRET="xxx"
11 - DRONE_RUNNER_CAPACITY=2
12 - DRONE_RUNNER_NAME=drone-runner-1
13 - DRONE_RUNNER_LABELS=environment:production
14 expose:
15 - "3000"
16 volumes:
17 - /var/run/docker.sock:/var/run/docker.sock
Line 13: We label the Drone CI runner with environment:production
. The value is arbitrary. You can use any value you want. However, you will need to use key:value
format.
So for the runner where we want to run the pipeline for staging environment, we will label it with environment:staging
.
Step 2: Specify the Label in Drone CI Pipeline
1---
2kind: pipeline
3type: docker
4name: build-deploy
5
6volumes:
7- name: dockersock
8 host:
9 path: /var/run/docker.sock
10
11platform:
12 os: linux
13 arch: amd64
14
15steps:
16- name: build-and-push-image
17 image: plugins/docker
18 volumes:
19 - name: dockersock
20 path: "/var/run/docker.sock"
21 settings:
22 username:
23 from_secret: registry_username
24 password:
25 from_secret: registry_pwd
26 repo: <dockerhub user>/<repo>
27 tags:
28 - latest
29 dockerfile: build/Dockerfile
30
31node:
32 environment: production
33
34trigger:
35 branch:
36 - main
37 event:
38 exclude:
39 - pull_request
Line 31-32: We specify the label value production
under the node
section. So when the pipeline is triggered, it will be routed to and run on the Drone CI runner with the label environment:production
.
So that’s it. Now you can try it out.
Caveat
We figured out how to make sure the pipeline goes to the right Drone CI runner. But there’s a little catch you should know about.
Let’s say you only have two Drone CI runners like in our example. And let’s say you added environment variable DRONE_RUNNER_LABELS
to both of them.
Now here’s the thing: if you have a pipeline without the node
section, it won’t go anywhere. That’s because the Drone CI runners will only run pipelines that match their labels.
But if you have a third Drone CI runner, then no worries! The pipelines without node
section will go to that one. Just keep in mind that the third runner might have a heavier workload.
If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!