# GitHub Actions

[GitHub Actions](https://github.com/features/actions) is a continuous integration platform that comes with [GitHub](https://github.com/).

Integrating Blackfire with GitHub Actions enables running Blackfire tests and scenarios, through [PHPUnit tests](../../php/integrations/phpunit), the [Blackfire Player](../../builds-cookbooks/player), or the [Blackfire CLI Tool](../../up-and-running/configuration/cli#configuration-cli).

> **Note**
>
> GitHub Actions support is available, via the [setup-php action](https://github.com/marketplace/actions/setup-php-action).
>
> While this action is designed for PHP, it installs all needed components, making Blackfire **usable by apps written in any language supported by Blackfire** (Python, Go...).

## Configuring the Project

To set up Blackfire on GitHub Actions, you need to define some [secret variables in your project settings](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets).

Define secrets for your **Client** credentials:

Define secrets for the **Server** credentials. Select the Blackfire environment which you want to use for your GitHub Actions-triggered tests:

## Configuring your Job

Within your workflow, configure each job where you need Blackfire:

```yaml
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Blackfire via setup-php Action
      uses: shivammathur/setup-php@v2
      with:
        # PHP Only: Setup PHP extensions, including Blackfire Probe.
        # It is recommended to disable xdebug.
        # Not needed for other languages.
        extensions: blackfire, :xdebug
        # Setup Blackfire Agent and CLI tool, and Blackfire Player
        tools: blackfire, blackfire-player
      env:
        # Expose your Blackfire credentials stored in secrets
        # as environment variables.
        BLACKFIRE_SERVER_ID: ${{ secrets.BLACKFIRE_SERVER_ID }}
        BLACKFIRE_SERVER_TOKEN: ${{ secrets.BLACKFIRE_SERVER_TOKEN }}
        BLACKFIRE_CLIENT_ID: ${{ secrets.BLACKFIRE_CLIENT_ID }}
        BLACKFIRE_CLIENT_TOKEN: ${{ secrets.BLACKFIRE_CLIENT_TOKEN }}

    # Trigger a profile using Blackfire CLI tool.
    - name: Profile list-users command
      env:
        APP_ENV: prod
        APP_DEBUG: 0
      run: blackfire run php bin/console app:list-users
      # Other example using Python:
      #run: blackfire run python my_script.py
```

Use option `--env` to target a specific environment ([see documentation](../../profiling-cookbooks/profiling-cli#profiling-cli-specifying-target-environment)).

## Using Blackfire Player

[Requires Production Plan](https://www.blackfire.io/pricing)

Thanks to the [setup-php action](https://github.com/marketplace/actions/setup-php-action), you can [use Blackfire Player to run your scenarios and get build reports](../../integrations/blackfire-player) each time you push a new version of your code on GitHub. `setup-php` also supports the Symfony CLI tool. This enables you to start the Symfony local webserver so that you can execute the Blackfire Player scenarios directly from your running job.

> **Note**
>
> When using other languages than PHP, e.g. Python, you may use other webservers like `gunicorn` or the Django development server.

```yaml
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Blackfire via setup-php Action
      uses: shivammathur/setup-php@v2
      with:
        php-version: '7.4'
        extensions: blackfire, :xdebug
        # Setup Blackfire Agent and CLI tool, Blackfire Player, and Symfony CLI
        tools: blackfire, blackfire-player, symfony
      env:
        BLACKFIRE_SERVER_ID: ${{ secrets.BLACKFIRE_SERVER_ID }}
        BLACKFIRE_SERVER_TOKEN: ${{ secrets.BLACKFIRE_SERVER_TOKEN }}
        BLACKFIRE_CLIENT_ID: ${{ secrets.BLACKFIRE_CLIENT_ID }}
        BLACKFIRE_CLIENT_TOKEN: ${{ secrets.BLACKFIRE_CLIENT_TOKEN }}

    - name: Symfony local server start
      env:
        APP_ENV: prod
        APP_DEBUG: 0
      run: |
        symfony local:server:start -d # Start Symfony local webserver
        # Run Blackfire Player scenarios from .blackfire.yaml or .bkf files
        blackfire-player run --endpoint=http://localhost:8000 --blackfire-env=<your-blackfire-environment-id> .blackfire.yaml
```
