# Cookbook

These are some additional recipes you can implement in Github Actions!

## Debugging Github Actions

Given that steps in a job can be a script, you can actually perform logging as steps in a job:

```yaml
- name: Logging
  run: |
    echo ${{ github.repository }}
    echo 'Hello!'
```

These will add a step to the job that logs these echos.

## Conditional steps

You can also run steps based on certain conditions (using [expressions](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions)). This is particularly useful when you want to only run a step when certain conditions are met.

```yaml
- name: Only run on Windows
  if: ${{ runner.os == 'windows' }}
  run: |
    Write-Output "test output"
```

The above step only runs when the runner OS is Windows.

{% hint style="info" %}
It is very important to note that there are several limitations to what expressions might work in the `if` key. These are dependent on what contexts are available to each part of a step. More information about contexts can be found [here](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs) and the contexts available to `if` are: `github`, `needs`, `vars`, `inputs`&#x20;
{% endhint %}

## Adding environment variables

Given that virtual machine runners run an OS, you will have access to environment variables from within the job through the `env` context. To add to the `env` context, you can use a step:

```yaml
- name: Export environment variables
  run: |
    echo "START=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV
```

The above exports a new environment variable `START` into `env` . This can be then accessed via `${{ env.START }}`.

## Matrix strategies

Suppose that you want to verify that a set of changes are not susceptible to backward compatibility issues in a Node.js environment (version `20`), while ensuring that the latest Node.js version is supported as well (version `23`).

You can actually use matrix strategies to verify this information by running the same job across different parameters.

```yaml
jobs:
  example_matrix:
    strategy:
      matrix:
        version: [20, 23]
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.version }}
```

So using the above, we are able to then run the same job `example_matrix` twice with two different node versions: `20` and `23`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.nushackers.org/hackerschool/ci-cd-with-github-actions/cookbook.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
