Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an environment variable to indicate which repository the currently running Action came from. #585

Merged
merged 6 commits into from
Nov 3, 2020
Merged

Add an environment variable to indicate which repository the currently running Action came from. #585

merged 6 commits into from
Nov 3, 2020

Conversation

chrisgavin
Copy link
Contributor

For certain types of Actions, it can be useful for the Action to know what repository it came from. For example the github/codeql-action needs to access binary files stored in GitHub Releases artifacts on its own repository.

Currently if this Action is mirrored on GitHub Enterprise Server under a different name it will be unable to locate its release artifacts as it always expects them to be in the github/codeql-action repository.

This change adds a github.action_repository context variable (and corresponding GITHUB_ACTION_REPOSITORY environment variable) that is set to the NWO of the repository which the Action came from. It is not set for script or Container Registry Actions. It is set to the empty string if the Action reference is relative to the checkout repository.

This allows Actions to be aware of their own source repository and therefore reach back out to them to do things like downloading extra artifacts.

Apologies if I've missed an existing way of getting this information, but I couldn't find one other than looking at the path which the Action is checked out to; I wasn't sure that this was a stable interface to rely on.

// Set GITHUB_ACTION_REPOSITORY if this Action is from a repository
if (actionStep.Action.Reference is Pipelines.RepositoryPathReference repositoryReference)
{
step.ExecutionContext.SetGitHubContext("action_repository", repositoryReference.Name);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice the RepositoryPathReference also has a Ref field. Would it be ok to also make that available, perhaps as a action_ref var? That would be very useful for code scanning as we've been wanting to include that data in our status reports, but couldn't work out how to get it. Doesn't have to be in this PR if the extra change would slow it down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure. I've added that field as well.

@chrisgavin
Copy link
Contributor Author

@joshhale Do you know what the best way is to get this reviewed?

@joshhale
Copy link

joshhale commented Aug 4, 2020

@TingluoHuang do you have time to review this PR?

@@ -9,6 +9,8 @@ public sealed class GitHubContext : DictionaryContextData, IEnvironmentContextDa
private readonly HashSet<string> _contextEnvWhitelist = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"action",
"action_ref",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrispat any objection to adding these 2 new environment variables?

@@ -135,6 +135,13 @@ public async Task RunAsync()
ExecutionContext.SetGitHubContext("event_path", workflowFile);
}

// Set GITHUB_ACTION_REPOSITORY if this Action is from a repository
if (Action.Reference is Pipelines.RepositoryPathReference repoPathReferenceAction)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the value would be if i use the action from the checkout repo.

ex:

- use: ./localaction
  with:
      foo: bar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like currently they are both set to the empty string. Do you think that's reasonable or should they just not be set at all in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just not set.

if (Action.Reference is Pipelines.RepositoryPathReference repoPathReferenceAction && 
    !string.Equals(repoPathReferenceAction.RepositoryType, Pipelines.PipelineConstants.SelfAlias, StringComparison.OrdinalIgnoreCase))
{
 // set action_repo/action_ref
}

@chrisgavin chrisgavin changed the base branch from master to main November 3, 2020 16:18
@chrisgavin
Copy link
Contributor Author

It looks like retargeting from master to main has brought in the commit a711bd9 (it's on master but not on main). Would you like me to rebase to take this commit out?

@TingluoHuang
Copy link
Member

@chrisgavin

It looks like retargeting from master to main has brought in the commit a711bd9 (it's on master but not on main). Would you like me to rebase to take this commit out?

it fine to include that commit. :)

@TingluoHuang TingluoHuang merged commit 95e2158 into actions:main Nov 3, 2020
AdamOlech pushed a commit to antmicro/runner that referenced this pull request Jan 28, 2021
…y running Action came from. (actions#585)

* add `workflow_dispatch`

* Add an environment variable to indicate which repository the currently running Action came from.

* Expose the Action ref as well.

* Move setting `github.action_repository` and `github.action_ref` to `ActionRunner.cs`.

* Don't set `action_repository` and `action_ref` for local Actions.

Co-authored-by: Tingluo Huang <tingluohuang@github.com>
TingluoHuang added a commit that referenced this pull request Apr 21, 2021
…y running Action came from. (#585)

* add `workflow_dispatch`

* Add an environment variable to indicate which repository the currently running Action came from.

* Expose the Action ref as well.

* Move setting `github.action_repository` and `github.action_ref` to `ActionRunner.cs`.

* Don't set `action_repository` and `action_ref` for local Actions.

Co-authored-by: Tingluo Huang <tingluohuang@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants