Skip to content

Instantly share code, notes, and snippets.

@jsks
Last active June 5, 2017 12:11
Show Gist options
  • Save jsks/bb42f693d536ad31b43a9db2bcc1d5b8 to your computer and use it in GitHub Desktop.
Save jsks/bb42f693d536ad31b43a9db2bcc1d5b8 to your computer and use it in GitHub Desktop.
R Build Script
#!/usr/bin/env Rscript
#
# Simple R pkg build script with logging posted to Slack. Output gets copied to
# a pkg repository under `/usr/share/nginx/R`.
#
# Wrap with *sh to background script and save as git hook.
#
# Requires:
# 1. Slack API Test Token
# 2. Slack Webhook URL
###
library(devtools)
library(drat)
library(git2r)
library(slackr)
# Replace args as appropriate
slackr_setup(api_token = "API_TOKEN",
channel = "CHANNEL",
username = "USERNAME",
icon_emoji = ":snowman:",
incoming_webhook_url = "WEBHOOK_URL")
repo <- repository()
ll <- commits(repo)
latest <- ll[[1]]
s <- sprintf("(%s) [%s] %s: %s",
latest@author@name,
substring(latest@sha, 1, 7),
substring(when(latest), 1, 10),
latest@message)
text_slackr(s)
DIR <- file.path(tempdir(), basename(repo@path))
dir.create(DIR, recursive = T)
tryCatch({
clone(getwd(), DIR)
setwd(DIR)
dir.create("/tmp/.rlibs", showWarnings = F)
.libPaths("/tmp/.rlibs")
install_deps(threads = parallel::detectCores() - 1, quiet = F, upgrade = T)
document()
# For internal packages, I only care about running the unit tests
slackr(out <- test())
out <- as.data.frame(out)
if (any(out$failed > 0) | any(out$error)) {
# For my purposes git users = slack users
text_slackr(sprintf("@%s: :warning: tests failed!", latest@author@name),
preformatted = F)
stop("Failed test!")
}
###
# Let's append the commit count since the last tag for better versioning
# in R. Ideally, we'd like to append the SHA of the latest commit but R...
n <- ahead_behind(tags(repo)[[1]], latest)[2]
system(sprintf("sed -i '/^Version/ s/$/-%d/' DESCRIPTION", n))
build(path = ".", quiet = T)
pkg <- list.files(pattern = ".*[.]tar[.]gz$")
if (length(pkg) > 1)
stop("Multiple packages found!")
insertPackage(pkg, "/usr/share/nginx/R/")
text_slackr("<!channel> build finished! :champagne:", preformatted = F)
}, finally = function() unlink(DIR, recursive = T))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment