CircleCI-Public / slack-orb

Create custom Slack notifications for CircleCI job statuses
https://circleci.com/developer/orbs/orb/circleci/slack
MIT License
214 stars 206 forks source link

Slack channels per matrix #394

Open apujari-hippo opened 1 year ago

apujari-hippo commented 1 year ago

Orb version: 4.12.1

What happened:

I have a list of jobs (aka tests) that run parallelly. I would like to send the status of each job to a separate slack channel.

I have included a sample in the additional information section. The current sample code only lists only 3 tests, but my actual config in production has 95 tests.

How can I achieve this?

Expected behavior:

I was thinking of some kind of key-value pair matrix where I can pass the test name and channel name which can be referenced in the later part of the config.

Additional Information:

Sample GIST: https://gist.github.com/apujari-hippo/50cb076627d3a27adfc93aeb14690faf

KyleTryon commented 1 year ago

Hey @apujari-hippo,

What you are looking to do is possible but unfortunately outside the realm of what the matrix feature of CircleCI is capable of. The matrix feature automatically makes combinations of each supplied parameter. That's useful when you want to test multiple browsers on multiple versions of node for example, but does not help here were you want to "group" parameters together.

Rather than using the matrix feature, I recommend expanding your config one level up like this:

- deploy:
    name: test-x
    test-name: test-x
    slack-channel:  channel-x
- deploy:
    name: test-y
    test-name: test-y
    slack-channel:  channel-y

Config SDK

It may be overkill for a situation like this, but the ConfigSDK can be used to make matrix-like jobs using Dynamic Config and Javascript. https://github.com/CircleCI-Public/circleci-config-sdk-ts/wiki

example:

//index.ts
import * as CircleCI from '@circleci/circleci-config-sdk';
import { testJob, deployJob } from './jobs';
import { isDeployable } from './utils';

const myConfig = new CircleCI.Config();
const myWorkflow = new CircleCI.Workflow('my-workflow');

// Support the three latest versions of Node
const nodeVersions = ["16.19", "18.13", "19.7"];

const testJobs = nodeVersions.map((version) => testJob(version));
testJobs.forEach((job) => {
  myConfig.addJob(job);
  myWorkflow.addJob(job)
});

if (isDeployable()) {
  myConfig.addJob(deployJob);
  myWorkflow.addJob(deployJob, {
    requires: testJobs.map((job) => job.name)
  });
}

myConfig.addWorkflow(myWorkflow);
myConfig.writeFile('dynamicConfig.yml')
// jobs/index.ts
import * as CircleCI from "@circleci/circleci-config-sdk";
import { nodeExecutor } from "../executors";

function sanetizeVersion(version: string) {
  return version.replace(".", "-");
}

const testJob = (version: string) =>
  new CircleCI.Job(`test-${sanetizeVersion(version)}`, nodeExecutor(version), [
    new CircleCI.commands.Checkout(),
    new CircleCI.commands.Run({
      command: "npm install && npm run test",
    }),
  ]);

const deployJob = new CircleCI.Job(
  "deploy",
  nodeExecutor("14"),
  [
    new CircleCI.commands.Checkout(),
    new CircleCI.commands.Run({
      command: "npm install && npm run deploy"
    })
  ]
)

export { testJob, deployJob }
apujari-hippo commented 1 year ago

@KyleTryon The problem with the above solution is the YAML is too big for the Circle CI to ingest, as we have 92 tests in there.

KyleTryon commented 1 year ago

What is the total size of the config? I have seen some interesting configs with even more than 92 jobs in them.

apujari-hippo commented 1 year ago

-rw-r--r-- 1 apujari staff 13K Apr 12 15:22 .circleci/config.yml The current size of the file is 13K

KyleTryon commented 1 year ago

@apujari-hippo sorry for the late reply. 13K is well within the limits. It sounds like that might be your config before the 92 jobs. What would it be with? You have several megabytes to work with.