Run Slack Plugin Conditionally

Hi there!

In part of my .drone.yml below:

---
kind: pipeline
type: kubernetes
name: after 


trigger: 
    event:        
      - custom 
    status:
      - failure 
    #branch: 
    #  - master 
depends_on: #Induce Parallelism on 4 workers 
    - test-worker-1
    - test-worker-2
    - test-worker-3
    - test-worker-4
steps:
 - name: create-slack-template  
      image: python
      failure: ignore 
      commands:          
        - python -V
        - pip install -r ./.github/scripts/requirements.txt       
        - python drone/create-slack-template.py     #Loads .env file, creates and populates appropriate template 
      depends_on:
        - get-run-stats
    - name: release-run-slack-notification 
      image: plugins/slack 
      failure: ignore   
      settings:     
        webhook:
            from_secret: SLACK_WEBHOOK_SANDBOX
        channel: release-runs-cypress-drone  
        template: file:///drone/src/drone/release-slack.txt 
      depends_on:
        - create-slack-template     

I’m trying to figure out a way to conditionally run step release-run-slack-notification if the step before it, create-slack-templatefails.

The step create-slack-template checks if the drone build has a certain tag or not as an environmental variable. If it contains a tag, it runs the next step and send the slack message. If not, it shouldn’t send a slack message.

Currently, regardless of whether the drone build has a certain tag or not, the release-run-slack-notification steps still runs. I’m somewhat getting around this by creating a line in the release-slack.txt to show IGNORE if this tag is present. However, this creates a lot of unneeded notifications in slack with the IGNORE flag

Would love some help / advice on this! Thx!

Hey @ndua-globality @jimsheldon will get back to you on this.

Thanks!

Hello @ndua-globality

I think you might be mixing conditions at the stage level and pipeline level that could be the problem.

You have this at the pipeline stage level:

trigger: 
  event:
  - custom
  status:
  - failure

The “custom” event would be for a manual promotion of a build, is that what you are doing? Also, this entire pipleline stage would only run if previous pipeline stages failed (since you have set “failure” status), is that the behavior you want?

From what you are describing, I think you should remove the depends_on for create-slack-template and release-run-slack-notification, and add a “when” condition for failure.

Something like this:

---
kind: pipeline
type: kubernetes
name: after 

trigger: 
    event:        
      - custom 
    status:
      - failure 
depends_on:
    - test-worker-1
    - test-worker-2
    - test-worker-3
    - test-worker-4

steps:
 - name: create-slack-template  
      image: python
      failure: ignore 
      commands:          
        - python -V
        - pip install -r ./.github/scripts/requirements.txt       
        - python drone/create-slack-template.py     #Loads .env file, creates and populates appropriate template 
    - name: release-run-slack-notification 
      image: plugins/slack 
      failure: ignore   
      settings:     
        webhook:
            from_secret: SLACK_WEBHOOK_SANDBOX
        channel: release-runs-cypress-drone  
        template: file:///drone/src/drone/release-slack.txt 
      when:
        status:
        - failure

Documentation on conditions:
https://docs.drone.io/pipeline/kubernetes/syntax/steps/#conditions