# Reusable Workflows

> Reusable workflows to run Mélodium in GitHub Actions and report commit statuses.

Web page: https://docs.cadence.ci/en/github/run-on-github

Make sure you previously read [CI/CD with Mélodium](https://docs.cadence.ci/en/from-template/cicd.md).

> [!TIP]
>
> Open-source project? The [open-source plan](https://cadence.ci/en/open-source) is free, and makes your runs public, ready to link from pull requests and your README.

## Reusable Workflow

GitHub Actions reusable workflows are available to run Mélodium within GitHub runners: [Mélodium Reusable Workflows](https://github.com/melodium-tech/github-actions). Each workflow version matches a Mélodium version: use `@v0.11.0` for Mélodium 0.11.

Every workflow takes the same secrets:

- `github-token`: used to check out the repository, and given to the engine as the `MELODIUM_SECRET_GITHUB_TOKEN` secret, which is where the status reporting below reads it by default;
- `melodium-api-token`: your Cadence.CI run token, given to the engine as `MELODIUM_API_TOKEN` for `--api-report` and for starting machines in your cloud account.

### Mélodium on GitHub Actions

The configuration to add in GitHub YAML is:
```yaml
jobs:
  melodium:
    uses: melodium-tech/github-actions/.github/workflows/melodium.yml@v0.11.0
    permissions:
      contents: read
      statuses: write
    with:
      command: run --api-report --logs logs/melodium.log .melodium-ci/Compo.toml --repository_clone_url "${{ github.repositoryUrl }}" --repository_clone_ref "${{ github.ref_name }}" --on_github true --ci_service_project "${{ github.repository }}" --ci_service_sha "${{ github.sha }}"
      artifact-path: 'logs/'
    secrets:
      github-token: ${{ github.token }}
      melodium-api-token: ${{ secrets.MELODIUM_API_TOKEN }}
```

The `statuses: write` permission lets the pipeline [report a status](#report-a-status-to-github) on the commit; it can be left out if you don't. The logs written to `logs/` are uploaded as the job's artifact.

Refer to [melodium.yml](https://github.com/melodium-tech/github-actions/blob/master/.github/workflows/melodium.yml) for the full list of input parameters.

### Dispatch Work Within GA Runner

A reusable workflow to run Mélodium with local distribution is available. Steps then run as containers on the GitHub runner itself, instead of machines in your cloud account:
```yaml
jobs:
  melodium:
    uses: melodium-tech/github-actions/.github/workflows/melodium-local-distrib.yml@v0.11.0
    permissions:
      contents: read
      statuses: write
    with:
      command: run --api-report --logs logs/melodium.log .melodium-ci/Compo.toml --work_location compose --repository_clone_url "${{ github.repositoryUrl }}" --repository_clone_ref "${{ github.ref_name }}" --on_github true --ci_service_project "${{ github.repository }}" --ci_service_sha "${{ github.sha }}"
      artifact-path: 'logs/'
    secrets:
      github-token: ${{ github.token }}
      melodium-api-token: ${{ secrets.MELODIUM_API_TOKEN }}
```

The run is still reported to Cadence.CI; drop `--api-report` and the `melodium-api-token` secret to run without it.
Refer to [melodium-local-distrib.yml](https://github.com/melodium-tech/github-actions/blob/master/.github/workflows/melodium-local-distrib.yml) for the full list of input parameters.

### Windows Runner

A reusable workflow to run Mélodium on a Windows runner is available:
```yaml
jobs:
  melodium:
    uses: melodium-tech/github-actions/.github/workflows/melodium-windows.yml@v0.11.0
    permissions:
      contents: read
      statuses: write
    with:
      command: run --api-report --logs logs/melodium.log .melodium-ci/Compo.toml --repository_clone_url "${{ github.repositoryUrl }}" --repository_clone_ref "${{ github.ref_name }}" --on_github true --ci_service_project "${{ github.repository }}" --ci_service_sha "${{ github.sha }}"
      artifact-path: 'logs/'
    secrets:
      github-token: ${{ github.token }}
      melodium-api-token: ${{ secrets.MELODIUM_API_TOKEN }}
```

Refer to [melodium-windows.yml](https://github.com/melodium-tech/github-actions/blob/master/.github/workflows/melodium-windows.yml) for the full list of input parameters.

### Mac OS Runner

A reusable workflow to run Mélodium on a Mac OS runner is available:
```yaml
jobs:
  melodium:
    uses: melodium-tech/github-actions/.github/workflows/melodium-macos.yml@v0.11.0
    permissions:
      contents: read
      statuses: write
    with:
      command: run --api-report --logs logs/melodium.log .melodium-ci/Compo.toml --repository_clone_url "${{ github.repositoryUrl }}" --repository_clone_ref "${{ github.ref_name }}" --on_github true --ci_service_project "${{ github.repository }}" --ci_service_sha "${{ github.sha }}"
      artifact-path: 'logs/'
    secrets:
      github-token: ${{ github.token }}
      melodium-api-token: ${{ secrets.MELODIUM_API_TOKEN }}
```

Refer to [melodium-macos.yml](https://github.com/melodium-tech/github-actions/blob/master/.github/workflows/melodium-macos.yml) for the full list of input parameters.

## Report a Status to GitHub

To report [commit statuses](https://docs.github.com/en/rest/commits/statuses) to GitHub, different methods are available. They all take the GitHub token as a `Secret`, read by default from `env:MELODIUM_SECRET_GITHUB_TOKEN`: the token never appears in logs, debug events or what Cadence.CI receives.

### Using `simpleStep`

In a [`simpleStep`](https://doc.melodium.tech/latest/en/cicd/naive/simpleStep.html) treatment, add the parameters as follows.
```mel
simpleStep[dispatcher=cicd](
  /* … */
  github=true,
  github_project=/* GitHub project, as owner/name */,
  github_sha=/* Commit SHA related to the step */
)
```

The token comes from `github_token`, which defaults to `"env:MELODIUM_SECRET_GITHUB_TOKEN"`; set it only to read the token from somewhere else.

The CI/CD template receives these values in its `main` treatment parameters but does not pass them to its steps: to report statuses, add them to `build` and `test` in `main`:
```mel
build: simpleStep[dispatcher=cicd](
  /* … */
  github=on_github,
  github_project=ci_service_project,
  github_sha=ci_service_sha
)
/*
  …
*/
test: simpleStepWithInput[dispatcher=cicd](
  /* … */
  github=on_github,
  github_project=ci_service_project,
  github_sha=ci_service_sha
)
```

### Using `postGithubState`

Within the appropriate treatment, add the [`postGithubState`](https://doc.melodium.tech/latest/en/cicd/services/github/postGithubState.html) treatment.
```mel
use cicd/services/github::|success
use cicd/services/github::postGithubState

/*
    …
*/

treatment someStuff[cicd: CicdDispatchEngine](
  // Add the following parameters
  github_project: string,
  github_sha: string
)
// …
{
    /*
        …
    */
    postSuccess: postGithubState(
        name="Doing a treatment",
        description="Treatment accomplishing some stuff",
        state=|success(),
        project=github_project,
        sha=github_sha,
        token="env:MELODIUM_SECRET_GITHUB_TOKEN"
    )
    theTreatment.completed -> postSuccess.trigger
}
```

There also exists a more generic [`setServiceState`](https://doc.melodium.tech/latest/en/cicd/services/setServiceState.html) treatment with prepared triggers for each state. All its inputs must be connected; a step that is never canceled can feed `canceled` with a `closeBlock`, which closes without sending anything:
```mel
use cicd/services::setServiceState
use std/flow::closeBlock

/*
    …
*/

treatment someStuff[cicd: CicdDispatchEngine](
  // Add the following parameters
  github_project: string,
  github_sha: string
)
// …
{
    /*
        …
    */
    setServiceState(
        name="Doing a treatment",
        description="Treatment accomplishing some stuff",
        github=true,
        github_project=github_project,
        github_sha=github_sha
    )
    noCancel: closeBlock<void>()

    Self.trigger -----------> setServiceState.pending
    theTreatment.started ---> setServiceState.running
    theTreatment.success ---> setServiceState.success
    theTreatment.error -----> setServiceState.error
    theTreatment.failed ----> setServiceState.failed
    Self.trigger -> noCancel.trigger,closed -> setServiceState.canceled
}
```

## Setup Variables

In GitHub, within _Settings_ > _Secrets and variables_ > _Actions_, in the section _Repository secrets_, add `MELODIUM_API_TOKEN` with a run token created in the Cadence.CI console.

The GitHub token needs no setup: `${{ github.token }}` is provided by GitHub Actions for each job, with the permissions listed in the job's `permissions`.

To give other secrets to the pipeline, pass them to the workflow as `secret-1` to `secret-10`: the engine reads them as `env:MELODIUM_SECRET_1` to `env:MELODIUM_SECRET_10`. Secrets stored on Cadence.CI are read by the steps the pipeline dispatches, given to them [by reference](https://docs.cadence.ci/en/pipelines/secrets.md#from-github-and-gitlab-runners).
