Newer
Older
using ghr_jll
using LazyArtifacts
using LibGit2
using Pkg.Artifacts
using Serialization
# Artifacts:
artifact_toml = LazyArtifacts.find_artifacts_toml(".")
function generate_artifacts(
datafiles;
artifact_name="artifacts-$VERSION",
root=".",
artifact_toml=joinpath(root, "Artifacts.toml"),
deploy=true,
if isnothing(tag)
tag = replace(lowercase(artifact_name), " " => "-")
end
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
if deploy && !haskey(ENV, "GITHUB_TOKEN")
@warn "For automatic github deployment, need GITHUB_TOKEN. Not found in ENV, attemptimg global git config."
end
if deploy
# Where we will put our tarballs
tempdir = mktempdir()
# Try to detect where we should upload these weights to (or just override
# as shown in the commented-out line)
origin_url = get_git_remote_url(root)
deploy_repo = "$(basename(dirname(origin_url)))/$(basename(origin_url))"
end
# Name for hash/artifact:
artifact_name = create_artifact_name_from_path(datafiles, artifact_name)
# create_artifact() returns the content-hash of the artifact directory once we're finished creating it
hash = create_artifact() do artifact_dir
cp(datafiles, joinpath(artifact_dir, artifact_name))
end
# Spit tarballs to be hosted out to local temporary directory:
if deploy
tarball_hash = archive_artifact(hash, joinpath(tempdir, "$(artifact_name).tar.gz"))
# Calculate tarball url
tarball_url = "https://github.com/$(deploy_repo)/releases/download/$(tag)/$(artifact_name).tar.gz"
# Bind this to an Artifacts.toml file
@info("Binding $(artifact_name) in Artifacts.toml...")
bind_artifact!(
artifact_toml,
artifact_name,
hash;
download_info=[(tarball_url, tarball_hash)],
lazy=true,
force=true,
)
end
if deploy
# Upload tarballs to a special github release
@info("Uploading tarballs to $(deploy_repo) tag `$(tag)`")
ghr() do ghr_exe
println(
readchomp(
`$ghr_exe -replace -u $(dirname(deploy_repo)) -r $(basename(deploy_repo)) $(tag) $(tempdir)`,
),
)
end
@info("Artifacts.toml file now contains all bound artifact names")
end
end
function get_git_remote_url(repo_path::String=".")
repo = LibGit2.GitRepo(repo_path)
origin = LibGit2.get(LibGit2.GitRemote, repo, "origin")
return LibGit2.url(origin)
end