-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_inputs.py
85 lines (71 loc) · 2.35 KB
/
check_inputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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)
or get_main_branch(os.getenv("GITHUB_REPOSITORY"))
or default
)
return os.getenv(name, default)
def main():
required_inputs = [
"IMAGE_NAME",
"REGISTRIES",
"CREDENTIALS",
"CONTEXT",
"DOCKERFILE",
"PLATFORMS",
"LATEST_ON_TAG",
"GITHUB_ENV",
"GITHUB_REF",
"GITHUB_ACTION_PATH",
"GITHUB_REPOSITORY",
"DEFAULT_BRANCH",
]
optional_inputs = [
"ON_MASTER",
"BUILD_ARGS",
"TAG_PATTERN",
"MANUAL_TAG",
"RESTRICT_TO",
"DOCKER_BUILDX_VERSION",
"WEBHOOK_URL",
"REPO_DESCRIPTION",
"REPO_FULL_DESCRIPTION",
"SHOULD_UPDATE_DOCKERIO",
]
# fail early if missing this required info
for env in required_inputs:
if not getenv(env):
print(f"missing param `{env}`, exiting.")
return 1
# `RESTRICT_TO` env prevents this from running from forked repositories
if getenv("RESTRICT_TO") and getenv("GITHUB_REPOSITORY") != getenv("RESTRICT_TO"):
print("not triggered on restricted-to repo, skipping.", getenv("RESTRICT_TO"))
return 1
if "docker.io" in getenv("REGISTRIES", "").split() and (
getenv("REPO_DESCRIPTION") or getenv("REPO_FULL_DESCRIPTION")
):
os.environ["SHOULD_UPDATE_DOCKERIO"] = "1"
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(getenv(env, "").strip().split("\n"))
)
)
return 0
if __name__ == "__main__":
sys.exit(main())