til

GitHub Actions Context Reference

If you have written workflows for projects of yours, the chance is high that you were referencing information from the GitHub context.

For this you may also have seen and used:

- name: Dump GitHub context
  env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
  run: echo "$GITHUB_CONTEXT"
- name: Dump job context
  env:
    JOB_CONTEXT: ${{ toJson(job) }}
  run: echo "$JOB_CONTEXT"
- name: Dump steps context
  env:
    STEPS_CONTEXT: ${{ toJson(steps) }}
  run: echo "$STEPS_CONTEXT"
- name: Dump runner context
  env:
    RUNNER_CONTEXT: ${{ toJson(runner) }}
  run: echo "$RUNNER_CONTEXT"
- name: Dump strategy context
  env:
    STRATEGY_CONTEXT: ${{ toJson(strategy) }}
  run: echo "$STRATEGY_CONTEXT"
- name: Dump matrix context
  env:
    MATRIX_CONTEXT: ${{ toJson(matrix) }}
  run: echo "$MATRIX_CONTEXT"

Warning: When using the whole github context, be mindful that it includes sensitive information such as github.token. GitHub masks secrets when they are printed to the console, but you should be cautious when exporting or printing the context.

GitHub Actions Context

Adding the above works, but requires a change to the workflow build, rerun it, check the output, modify again to remove output again, ...

Doing so more than once becomes tedious. Sadly there seems to be no good reference on what's actually included in the context.

context outputs will differ per build, and depend on the trigger starting the build.

To highlight the most common cases, this post highlights the contents for a pull_request and for a push.tags trigger.

Below you can see the output for a normal pull_request event, logged using ${{ toJson(github) }}

{
    "token": "***",
    "job": "build",
    "ref": "refs/pull/34/merge",
    "sha": "dfc3caf476a75101334606a4eca8015d926bd928",
    "repository": "mikepenz/release-changelog-builder-action",
    "repository_owner": "mikepenz",
    "repositoryUrl": "git://github.com/mikepenz/release-changelog-builder-action.git",
    "run_id": "313456928",
    "run_number": "45",
    "retention_days": "90",
    "actor": "mikepenz",
    "workflow": "CI",
    "head_ref": "feature/external_changelog_showcase",
    "base_ref": "develop",
    "event_name": "pull_request",
    "event": {
      "action": "synchronize",
      "after": "bff1ece57d23911ef15ee7d0218033671336e358",
      "before": "18b7cdd010de2ad26d5a0a046044b599d0059572",
      "number": 34,
      "pull_request": {
        "_links": {
          "comments": {
            "href": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/issues/34/comments"
          },
          "commits": {
            "href": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/pulls/34/commits"
          },
          "html": {
            "href": "https://github.com/mikepenz/release-changelog-builder-action/pull/34"
          },
          "issue": {
            "href": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/issues/34"
          },
          "review_comment": {
        "node_id": "MDEwOlJlcG9zaXRvcnkzMDQ1NTMxNTI=",
        "notifications_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/notifications{?since,all,participating}",
        "open_issues": 1,
        "open_issues_count": 1,
        "owner": {
          /*USER OBJECT*/
        },
        "private": false,
        "pulls_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/pulls{/number}",
        "pushed_at": "2020-10-18T07:44:22Z",
        "releases_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/releases{/id}",
        "size": 478,
        "ssh_url": "[email protected]:mikepenz/release-changelog-builder-action.git",
        "stargazers_count": 4,
        "stargazers_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/stargazers",
        "statuses_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/statuses/{sha}",
        "subscribers_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/subscribers",
        "subscription_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/subscription",
        "svn_url": "https://github.com/mikepenz/release-changelog-builder-action",
        "tags_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/tags",
        "teams_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/teams",
        "trees_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/git/trees{/sha}",
        "updated_at": "2020-10-18T07:30:10Z",
        "url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action",
        "watchers": 4,
        "watchers_count": 4
      },
      "sender": {
        /*USER OBJECT*/
      }
    },
    "server_url": "https://github.com",
    "api_url": "https://api.github.com",
    "graphql_url": "https://api.github.com/graphql",
    "workspace": "/home/runner/work/release-changelog-builder-action/release-changelog-builder-action",
    "action": "run3",
    "event_path": "/home/runner/work/_temp/_github_workflow/event.json",
    "path": "/home/runner/work/_temp/_runner_file_commands/add_path_e28f929f-3f81-4b2e-8961-f68887fae780",
    "env": "/home/runner/work/_temp/_runner_file_commands/set_env_e28f929f-3f81-4b2e-8961-f68887fae780"
}
  

The user object usually looks like the following, and has these attributes

{
    "avatar_url": "https://avatars0.githubusercontent.com/u/1476232?v=4",
    "events_url": "https://api.github.com/users/mikepenz/events{/privacy}",
    "followers_url": "https://api.github.com/users/mikepenz/followers",
    "following_url": "https://api.github.com/users/mikepenz/following{/other_user}",
    "gists_url": "https://api.github.com/users/mikepenz/gists{/gist_id}",
    "gravatar_id": "",
    "html_url": "https://github.com/mikepenz",
    "id": 1476232,
    "login": "mikepenz",
    "node_id": "MDQ6VXasdfNzYyMzI=",
    "organizations_url": "https://api.github.com/users/mikepenz/orgs",
    "received_events_url": "https://api.github.com/users/mikepenz/received_events",
    "repos_url": "https://api.github.com/users/mikepenz/repos",
    "site_admin": false,
    "starred_url": "https://api.github.com/users/mikepenz/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/mikepenz/subscriptions",
    "type": "User",
    "url": "https://api.github.com/users/mikepenz"
}

Different event types differ by the contents of the event object.

Looking at the event_name will indicate which event type triggered the specific build.

A push event with a new tag for example looks like:

{
    "token": "***",
    "job": "build",
    "ref": "refs/tags/v0.0.7",
    "sha": "80cf18bd05e70ee8454afa82fc9d5036f784c434",
    "repository": "mikepenz/release-changelog-builder-action",
    "repository_owner": "mikepenz",
    "repositoryUrl": "git://github.com/mikepenz/release-changelog-builder-action.git",
    "run_id": "313462379",
    "run_number": "48",
    "retention_days": "90",
    "actor": "mikepenz",
    "workflow": "CI",
    "head_ref": "",
    "base_ref": "",
    "event_name": "push",
    "event": {
      "after": "25083319fae65416945ae7dd3f8789c291819ea7",
      "base_ref": null,
      "before": "0000000000000000000000000000000000000000",
      "commits": [],
      "compare": "https://github.com/mikepenz/release-changelog-builder-action/compare/v0.0.7",
      "created": true,
      "deleted": false,
      "forced": false,
      "head_commit": {
        "author": {
          "email": "[email protected]",
          "name": "Mike Penz",
          "username": "mikepenz"
        },
        "committer": {
          "email": "[email protected]",
          "name": "Mike Penz",
          "username": "mikepenz"
        },
        "distinct": true,
        "id": "80cf18bd05e70ee8454afa82fc9d5036f784c434",
        "message": "- export tags github context",
        "timestamp": "2020-10-18T09:51:01+02:00",
        "tree_id": "4684456f506a277a1c4e544dd69a3ec36b5b0712",
        "url": "https://github.com/mikepenz/release-changelog-builder-action/commit/80cf18bd05e70ee8454afa82fc9d5036f784c434"
      },
      "pusher": {
        "email": "[email protected]",
        "name": "mikepenz"
      },
      "ref": "refs/tags/v0.0.7",
      "repository": {
        "archive_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/{archive_format}{/ref}",
        "archived": false,
        "assignees_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/assignees{/user}",
        "blobs_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/git/blobs{/sha}",
        "branches_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/branches{/branch}",
        "clone_url": "https://github.com/mikepenz/release-changelog-builder-action.git",
        "collaborators_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/collaborators{/collaborator}",
        "comments_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/comments{/number}",
        "commits_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/commits{/sha}",
        "compare_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/compare/{base}...{head}",
        "contents_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/contents/{+path}",
        "contributors_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/contributors",
        "created_at": 1602833523,
        "default_branch": "develop",
        "deployments_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/deployments",
          "following_url": "https://api.github.com/users/mikepenz/following{/other_user}",
          "gists_url": "https://api.github.com/users/mikepenz/gists{/gist_id}",
          "gravatar_id": "",
          "html_url": "https://github.com/mikepenz",
          "id": 1476232,
          "login": "mikepenz",
          "name": "mikepenz",
          "node_id": "MDQ62341a0NzYyMzI=",
          "organizations_url": "https://api.github.com/users/mikepenz/orgs",
          "received_events_url": "https://api.github.com/users/mikepenz/received_events",
          "repos_url": "https://api.github.com/users/mikepenz/repos",
          "site_admin": false,
          "starred_url": "https://api.github.com/users/mikepenz/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/mikepenz/subscriptions",
          "type": "User",
          "url": "https://api.github.com/users/mikepenz"
        },
        "private": false,
        "pulls_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/pulls{/number}",
        "pushed_at": 1603007506,
        "releases_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/releases{/id}",
        "size": 478,
        "ssh_url": "[email protected]:mikepenz/release-changelog-builder-action.git",
        "stargazers": 4,
        "stargazers_count": 4,
        "stargazers_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/stargazers",
        "statuses_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/statuses/{sha}",
        "subscribers_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/subscribers",
        "subscription_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/subscription",
        "svn_url": "https://github.com/mikepenz/release-changelog-builder-action",
        "tags_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/tags",
        "teams_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/teams",
        "trees_url": "https://api.github.com/repos/mikepenz/release-changelog-builder-action/git/trees{/sha}",
        "updated_at": "2020-10-18T07:30:10Z",
        "url": "https://github.com/mikepenz/release-changelog-builder-action",
        "watchers": 4,
        "watchers_count": 4
      },
      "sender": {
        /*USER OBJECT*/
      }
    },
    "server_url": "https://github.com",
    "api_url": "https://api.github.com",
    "graphql_url": "https://api.github.com/graphql",
    "workspace": "/home/runner/work/release-changelog-builder-action/release-changelog-builder-action",
    "action": "run3",
    "event_path": "/home/runner/work/_temp/_github_workflow/event.json",
    "path": "/home/runner/work/_temp/_runner_file_commands/add_path_2fb9b2b2-96b5-40ec-8ca3-c855cfe69897",
    "env": "/home/runner/work/_temp/_runner_file_commands/set_env_2fb9b2b2-96b5-40ec-8ca3-c855cfe69897"
  }

There are a lot more contexts you can access to retrieve additional information on the environment of your build.

Each build also contains the runner context, giving you information about the actual system the build was executed on.

This one will give you insights on the os and different important directories.

{
  "os": "Linux",
  "tool_cache": "/opt/hostedtoolcache",
  "temp": "/home/runner/work/_temp",
  "workspace": "/home/runner/work/release-changelog-builder-action"
}

Feedback

Do you have more suggestions, or would love to see the outputs for different environments? Or additional tips, thoughts, feedback, improvements or comments?

Let me know @mike_penz

Attribution

Link preview photo by Marcel Strauß / Unsplash