forked from weecology/PortalData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.R
69 lines (59 loc) · 2.64 KB
/
archive.R
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
#' Update the version number
#'
#' By default update the minor version number since most changes are new data.
#' If [major] or [patch] is in the last commit summary increment the matching
#' version instead.
config <- yaml::yaml.load_file("config.yml")
repo <- git2r::repository(".")
repo_url <- paste("https://github.com/", config$repo, ".git", sep = "")
git2r::remote_add(repo, name = "deploy", url = repo_url)
cred <- git2r::cred_token("GITHUB_TOKEN")
# Use a special user name so that it is clear which commits are automated
git2r::config(repo,
user.email = config$deploy_email,
user.name = config$deploy_username)
# Check the most recent commit for version instructions
last_commit <- git2r::commits(repo)[[1]]
current_ver <- semver::parse_version(readLines("version.txt"))
if (grepl("Merge pull request", last_commit['summary'])){
last_commit <- git2r::commits(repo)[[2]]
}
if (grepl("\\[no version bump\\]", last_commit['summary'])) {
new_ver <- current_ver
} else if (grepl("\\[major\\]", last_commit['summary'])) {
new_ver <- semver::increment_version(current_ver, "major", 1L)
} else if (grepl("\\[patch\\]", last_commit['summary'])) {
new_ver <- semver::increment_version(current_ver, "patch", 1L)
} else {
new_ver <- semver::increment_version(current_ver, "minor", 1L)
}
writeLines(as.character(new_ver), "version.txt")
travis_build <- Sys.getenv("TRAVIS_BUILD_NUMBER")
git2r::checkout(repo, branch = "master")
commit_message <- paste("Update data and trigger archive: Travis Build",
travis_build,
"[skip ci]")
git2r::add(repo, "*")
git2r::commit(repo, message = commit_message)
# Create a new release to trigger Zenodo archiving
github_token = Sys.getenv("GITHUB_TOKEN")
pull_request = Sys.getenv("TRAVIS_PULL_REQUEST")
# If the version has been incremented and this is not a pull request
# then push the updated data, create a new tag, push the tag and
# trigger a release.
if (new_ver > current_ver & pull_request == 'false'){
git2r::push(repo,
name = "deploy",
refspec = "refs/heads/master",
credentials = cred)
git2r::tag(repo, as.character(new_ver), paste("v", new_ver, sep=""))
git2r::push(repo,
name = "deploy",
refspec = paste("refs/tags/", new_ver, sep=""),
credentials = cred)
api_release_url = paste("https://api.github.com/repos/", config$repo, "/releases", sep = "")
httr::POST(url = api_release_url,
httr::content_type_json(),
httr::add_headers(Authorization = paste("token", github_token)),
body = paste('{"tag_name":"', new_ver, '"}', sep=''))
}