Passing variable to next step (notification plugin)

Hi there! Drone 0.8.1 here

I’d like to trigger slack notify during (or end of) build. With custom message, set dynamically in build.

Let’s say we are running tests, or counting files in repo, i’d like to notify team like this:

  1. build № started author commit etc etc (working)
  2. drone found 543 files in repo!
  3. 71 of 72 tests passed, one ignored!
  4. build success!

How can i achieve this? The only way i see here to add direct curl-ing to commands. But it would be so much more native to let slack/other plugin to inherit some env variable, or similar.

Thanks a lot in advance!

Or maybe, it could be done from filesystem, with @file syntax. with build step saving it before notification.

The only way to pass metadata between steps is by disk. You would need to create a plugin (or fork and alter) that reads such variables from disk and uses them at runtime. The current slack plugin is feature-frozen so forking and publishing your own custom remix would be your best option.

EDIT: the slack plugin does support reading the notification template from disk. So you could generate the template prior to the slack notification step.

pipeline:
  build:
    image: alpine
    commands:
      - echo "hello world" > relative/path/to/template.txt
  notify:
    image: plugins/slack
    template: relative/path/to/template.txt
1 Like

Thanks for reply!
Sadly that’s ain’t working. Just copied your solution.

pipeline:
  build:
    image: alpine
    commands:
      - echo "hello world" > template.txt
  notify:
    image: plugins/slack
    template: template.txt
    secrets: [ slack_webhook ]

and got template.txt in slack

I’m sure i’m missing something here

After looking at https://github.com/drone-plugins/drone-slack/blob/master/template.go
got it working like this

pipeline:
  build:
    image: alpine
    commands:
      - pwd
      - echo "hello world" > /tmp/som/template.txt
    volumes:
      - /tmp/som:/tmp/som
  notify:
    image: plugins/slack
    template: file:///tmp/som/template.txt
    secrets: [ slack_webhook ]
    volumes:
      - /tmp/som:/tmp/som

Gona find out how to make it work without volumes… )

1 Like

Thanks a bunch! If you know a way without volumes - it would make things all the better, but right now i already can use this!)

1 Like

Also got it working like this (for github.com)

pipeline:
  build:
    image: alpine
    commands:
      - env
      - pwd
      - echo "hello zworld" > template.txt
  notify:
    image: plugins/slack
    template: file:///drone/src/github.com/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/template.txt
    secrets: [ slack_webhook ]

It would be so much easier if DRONE_WORKSPACE was passed to slack plugin.

3 Likes

TNX! I got it working only because of this thread!
For anyone tackling this with drone 1.6.2 & kubernetes runner it did not work with the drone workspace configuration like he did, I did it with the like volumes similar to his first solution:

- name: test
  image: sunbit/testing-automation-scripts
  pull: always
  volumes:
    - name: slack
      path: /slack
  environment:
  when:
    event: pull_request

- name: slack
  image: plugins/slack
  volumes:
    - name: slack
      path: /slack
  settings:
    webhook: https://hooks.slack.com/services/xxxxxxxx/xxxxxxxxxxKETA78/xxxxxxxx
    channel: automation
    username: drone
    template: file:///slack/slack_message.txt
  when:
    event: pull_request
    status:
    - failure
    - success

image_pull_secrets:
- dockerconfigjson

volumes:
  - name: slack
    temp: {}
1 Like

TNX! I got it working only because of this thread!
For anyone tackling this with drone 1.6.2 & kubernetes runner it did not work with the drone workspace configuration like he did, I did it with the like volumes similar to his first solution:

Just a heads up that the previous example from yellowmegaman would no longer work because the default workspace path changed. Their example would therefore need to be adjusted as follows:

- template: file:///drone/src/github.com/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/template.txt
+ template: file:///drone/src/template.txt