YOU54F / cypress-plugins

A home for various Cypress Plugins

Home Page:https://cypress-plugins.saf.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for github actions

vrknetha opened this issue · comments

Send the message support for Github actions and bitbucket pipelines

+1 would be great

Hey @vrknetha @heidi-patrick

Sounds like a good idea, I've not had a play with github actions yet, happy for contributions

I would love github actions support too. I did test it just to see what happens:

- name: Slack notification with total pass/fail
        if: always()
        run: |
          npx cypress-slack-reporter --report-dir mochawesome-report --ci-provider none

and SLACK_WEBHOOK_URL set in GitHub secrets.

Erroring I guess because it cannot fetch the webhook URL:

(node:901) UnhandledPromiseRejectionWarning: Error: TypeError: Cannot read property 'split' of undefined
    at Object.exports.slackRunner (/__w/cypress-reporting-gh-pages/cypress-reporting-gh-pages/node_modules/cypress-slack-reporter/bin/slack/slack-alert.js:167:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:901) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

Hey @bennymeade, thanks for taking a look!

How do env vars get set, the script is using PROCESS.env. Are the github secrets automatically set an env vars in the runner?

Yes I believe so, in a GH Actions workflow the synatx for calling a secret is like this:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TEST_AUTOMATION }}

... and it behaves the same as an global variable.

weird, could you try echoing it out on a step, you might need to export it in your ci runner like

export SLACK_WEBHOOK_URL = ${{ secrets.SLACK_WEBHOOK_TEST_AUTOMATION }} ?

echo "SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TEST_AUTOMATION }}" but obviously for security they mask the output.

SLACK_WEBHOOK_URL: ***

Just looking at the docs

https://docs.github.com/en/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow

steps:
  - name: Hello world action
    with: # Set the secret as an input
      super_secret: ${{ secrets.SuperSecret }}
    env: # Or as an environment variable
      super_secret: ${{ secrets.SuperSecret }}

So I think you will need something like


steps:
-  name: Slack notification with total pass/fail
   env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TEST_AUTOMATION }}
   if: always()
       run: |
         npx cypress-slack-reporter --report-dir mochawesome-report --ci-provider none

Bravo, that worked : )
image

Result my mate!

How would you feel about sticking something in the readme to cover that for other github actions users, and maybe we can follow this up with a later ticket to support it as a supported --ci-provider option!

Yes definitely, I'm happy to do that @YOU54F Thanks again for your speedy responses.

Here's my GH workflow for others benefit:

name: Cypress Slack notify
on:
  workflow_dispatch:

jobs:
  cypress-test:
    name: Run on Chrome 89
    runs-on: ubuntu-18.04
    container: cypress/browsers:node14.16.0-chrome89-ff86
    steps:
      - uses: actions/checkout@v2

      - name: Install Cypress with a clean slate
        run: |
          npm ci
          $(npm bin)/cypress cache path
          $(npm bin)/cypress cache list
          $(npm bin)/cypress verify

      - name: Run Cypress tests
        run: npm run cy:run

      - name: Slack notification with cypress-slack-reporter
        if: always()
        env: 
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        run: |
          npx cypress-slack-reporter --report-dir mochawesome-report --ci-provider none

with minimalist repo sample here: https://github.com/bennymeade/cypress-slack-reporter-gh-actions

commented

Hi @bennymeade, I followed your example and got it to work. Thanks for that!

In the Slack message, the commit link is pointing to https://github.com/undefined/undefined/commit/undefined. Did you have the same problem?

I need to pass --ci-provider github.

But the issue is I have to use a command like this:

GITHUB_REPOSITORY=my_repo npx cypress-slack-reporter --report-dir mochawesome-merged --ci-provider github

Otherwise, the commit link would have duplicated owners.