forked from espressif/github-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_zips.py
More file actions
68 lines (55 loc) · 2.92 KB
/
Copy pathrelease_zips.py
File metadata and controls
68 lines (55 loc) · 2.92 KB
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
#!/usr/bin/env python
from github import Github, GithubException
import os
import subprocess
def main():
ref = os.environ.get("GITHUB_REF", None)
if not ref:
raise SystemExit("Not an event based on a push. Workflow configuration is wrong?")
REF_PREFIX = "refs/tags/"
if not ref.startswith(REF_PREFIX):
raise SystemExit("Ref {} is not a tag. Workflow configuration is wrong?".format(ref))
tag = ref[len(REF_PREFIX):]
github_actor = os.environ["GITHUB_ACTOR"]
github_token = os.environ["GITHUB_TOKEN"]
github_repo = os.environ["GITHUB_REPOSITORY"]
print("Connecting to GitHub...")
github = Github(github_token)
repo = github.get_repo(github_repo)
if repo.private:
# For private repos, git needs authentication (but set so that the
# remote URL doesn't embed the temporary credentials in the zip file or
# even store the temporary credential token in the filesystem.)
subprocess.run(["git", "config", "--global", "credential.https://github.com.username", github_actor], check=True)
helper_cmd = "!f() { test \"$1\" = get && echo \"password=$GITHUB_TOKEN\"; }; f" # shell command
subprocess.run(["git", "config", "--global", "credential.https://github.com.helper", helper_cmd], check=True)
git_url = "https://github.com/{}.git".format(github_repo)
repo_name = github_repo.split("/")[1]
directory = "{}-{}".format(repo_name, tag)
print("Doing a full recursive clone of {} ({}) into {}...".format(git_url, tag, directory))
# note: it may be easier to use github's "checkout" action here, with the correct args
subprocess.run(["git", "clone", "--recursive", "--branch", tag, git_url, directory], check=True)
zipfile = "{}.zip".format(directory)
print("Zipping {}...".format(zipfile))
subprocess.run(["/usr/bin/7z", "a", "-mx=9", "-tzip", zipfile, directory], check=True)
try:
release = repo.get_release(tag)
print("Existing release found...")
if any(asset.name == zipfile for asset in release.get_assets()):
raise SystemExit("A release for tag {} already exists and has a zip file {}. Workflow configured wrong?".format(tag, zipfile))
except GithubException:
print("Creating release...")
is_prerelease = "-" in tag # tags like vX.Y-something are pre-releases
release_repo_name = os.environ.get('RELEASE_PROJECT_NAME', repo_name)
name = "{} {} {}".format(release_repo_name,
"Pre-release" if is_prerelease else "Release",
tag)
release = repo.create_git_release(tag, name,
"(Draft created by Action)",
draft=True, prerelease=is_prerelease)
print("Attaching zipfile...")
release.upload_asset(zipfile)
print("Release URL is {}".format(release.html_url)
)
if __name__ == "__main__":
main()