Skip to content

Commit

Permalink
Supporting schedule runs
Browse files Browse the repository at this point in the history
Schedule runs don't have access to github.event.repository.default_branch
If this is not present, query Github API to get the default_branch instead
  • Loading branch information
rgaudin committed Sep 28, 2021
1 parent d33e608 commit dab9c2b
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions check_inputs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/usr/bin/env python3

import json
import os
import sys
import urllib.request


def get_main_branch(repository):
url = "https://api.github.com/repos/{repository}".format(repository=repository)
with urllib.request.urlopen(url) as uh:
return json.load(uh).get("default_branch", None)


def getenv(name, default=None):
"""default branch is only available on repo-related trigger events (not schedule)"""
if name == "DEFAULT_BRANCH":
return os.getenv(name, "").strip() or get_main_branch(
os.getenv("GITHUB_REPOSITORY")
)
return os.getenv(name, default)


def main():
Expand All @@ -13,11 +30,11 @@ def main():
"DOCKERFILE",
"PLATFORMS",
"LATEST_ON_TAG",
"DEFAULT_BRANCH",
"GITHUB_ENV",
"GITHUB_REF",
"GITHUB_ACTION_PATH",
"GITHUB_REPOSITORY",
"DEFAULT_BRANCH",
]
optional_inputs = [
"ON_MASTER",
Expand All @@ -30,27 +47,23 @@ def main():

# fail early if missing this required info
for env in required_inputs:
if not os.getenv(env):
if not getenv(env):
print(f"missing param `{env}`, exiting.")
return 1

# `RESTRICT_TO` env prevents this from running from forked repositories
if os.getenv("RESTRICT_TO") and os.getenv("GITHUB_REPOSITORY") != os.getenv(
"RESTRICT_TO"
):
print(
"not triggered on restricted-to repo, skipping.", os.getenv("RESTRICT_TO")
)
if getenv("RESTRICT_TO") and getenv("GITHUB_REPOSITORY") != getenv("RESTRICT_TO"):
print("not triggered on restricted-to repo, skipping.", getenv("RESTRICT_TO"))
return 1

with open(os.getenv("GITHUB_ENV"), "a") as fh:
with open(getenv("GITHUB_ENV"), "a") as fh:
for env in required_inputs + optional_inputs:
# don't write credentials to shared env! nor don't overwrite GH ones
if env == "CREDENTIALS" or env.startswith("GITHUB_"):
continue
fh.write(
"{env}={value}\n".format(
env=env, value=" ".join(os.getenv(env, "").strip().split("\n"))
env=env, value=" ".join(getenv(env, "").strip().split("\n"))
)
)

Expand Down

0 comments on commit dab9c2b

Please sign in to comment.