Drone

Triggering Promotion Event

In this topic I propose allowing deployment events to trigger steps. It looks like this feature may exist as the “promote” event. I do not see a way to get drone to respond to the GitHub deployment webhook event using promote. Am I missing something and, if so, can someone point me in the right direction? Thanks!

Drone supports deployment webhooks and the deployment status API as of version 1.3.1, so please make sure you are using a compatible version of Drone. If at one point you were using an older version of Drone and later upgraded, you may need to de-activate and re-activate the repository to ensure the GitHub webhook is properly configured.

Once you confirm the above, you can use the below guide to better understand why pipelines are not being triggered. If the below guide does not help, please provide the requested information in the Still Having Issues section.

Does this mean that the promote event is the event I want to use to respond to a GitHub deployment webhook? I would like to trigger my step on successful deployment to staging. I’ve read the documentation and don’t see an obvious way to do this.

Yes, a deployment webhook creates a build with event type promote. When Drone receives a deployment webhook it will automatically execute your pipeline unless you have configured the trigger section to ignore the hook. There is otherwise nothing special you need to do to use deployment events.

So if I want to trigger my step after a successful deployment, should I use the deployments webhook or the deployment statuses webhook? Could you possibly give me an example of configuring my Drone file to respond to a success deployment status? Thank you!

First you would use the deployment API to create a GitHub deployment:

When you create a deployment in GitHub, Drone will receive a deployment webhook from GitHub and execute your pipeline. The below pipeline will match any webhook from GitHub, including push, pull_request, and deployment hooks. You will notice there is no special syntax required to process a deployment webhook and execute a pipeline.

kind: pipeline
type: docker
name: default

steps:
  - name: greetings
    image: alpine
    commands:
    - echo hello

If you want to define a pipeline that only executes when a deployment webhook is received, you would modify the trigger section accordingly. Remember that deployment webhooks are referred to as promote events in Drone.

kind: pipeline
type: docker
name: default

steps:
  - name: greetings
    image: alpine
    commands:
    - echo hello

+trigger:
+  event: [ promote ]

If you want to define two separate pipelines, one that executes when a deployment webhook is received, and another that executes when push and pull request webhooks are received, it can be achieved with the following:

kind: pipeline
type: docker
name: default

steps:
  - name: echo
    image: alpine
    commands:
    - echo "this is not a deployment"

trigger:
  event: [ push, pull_request ]

---
kind: pipeline
type: docker
name: deploy

steps:
  - name: echo
    image: alpine
    commands:
    - echo "this is a deployment"

trigger:
  event: [ promote ]

Once a deployment pipeline completes, Drone will automatically make an API call to the deployment status endpoint in GitHub to update the status of your deployment.

I hope this helps clarify how everything fits together.

So how do I trigger the pipeline only when a successful deployment has already happened? I am trying to run tests in a staging environment AFTER a successful deployment. Sorry for the continuous responses. Maybe I am not understanding or phrasing my question well.

No worries, I think part of the confusion is that this thread is asking about GitHub Deployment Hooks and about deploying code. You can deploy code without using GitHub Deployment Hooks, in fact, very few people actually use GitHub Deployment Hooks.

If your goal is to deploy code and then execute tests you can do this in a single pipeline. In the below example we build and publish and image, then we deploy the image to a remote machine, and then we run integration tests:

kind: pipeline
type: docker
name: default

steps:
# compile the bundle
- name: bundle
  image: node
  commands:
  - npm run tests
  - npm run bundle

# build and publish a docker image
- name: publish
  image: plugins/drone
  settings:
    repo: company/project
    username: ...
    password: ...
    tags: ${DRONE_COMMIT}

# ssh into the machine and deploy the docker image.
# this is pseudo code.
- name: deploy
  image: appleboy/drone-ssh
  settings:
    host: foo.com
    username: root
    password: password
    script:
      - docker stop myprogram
      - docker pull company/project:${DRONE_COMMIT}
      - docker run -d company/project:${DRONE_COMMIT} myprogram

# run integration tests
- name: integration
  image: node
  commands:
  - npm run integration

The above workflow is not terribly sophisticated since it uses plain ssh to login to a machine and perform deployment tasks with shell commands. Most people use more sophisticated tooling these days (terraform, kubernetes, etc) but hopefully this more simple example helps convey how the system might be used to achieve your goals.

So I am not using Drone for deployments. We are using a different tool to deploy our service. I am just wanting to execute some automated tests after the service has been successfully deployed. Thanks for your help and patience!

Thank you for providing clarification. I think it is important to note that GitHub deployment hooks are sent to request a deployment, not to signal a deployment is completed. Since you are using a separate tool to deploy your service, I do not think deploy hooks can be used to satisfy your use case.

Deployments are requests to deploy a specific ref (branch, SHA, tag). GitHub dispatches a deployment event that external services can listen for and act on when new deployments are created.

Instead I would recommend having your deployment tool invoke the Drone API after a successful deployment to execute a pipeline so that you can run your tests. For some more information about the API endpoint see https://github.com/drone/drone/issues/2679.

When you trigger a build using this endpoint it will have an event type of custom. You can use this to limit your pipeline or pipeline steps accordingly. For example:

trigger:
  event: [ custom ]

or

steps:
- name: integration
  image: node
  commands:
  - npm install
  - npm run integration
  when:
    event: [ custom ]

I think I now understand your use case a bit better, so hopefully this helps. If you have any more questions I would be happy to answer them. We can also see if @bradrydzewski has open office hour slots if you think a video chat would be helpful.

This is helpful and is the path we have already went down, but were hoping it was possible to do using only Drone. Thanks so much for your help!!