Skip to content

Instantly share code, notes, and snippets.

@imerr
Created April 26, 2018 05:57
Show Gist options
  • Save imerr/8880c5d0eaae8d96d34f482f59e87dbb to your computer and use it in GitHub Desktop.
Save imerr/8880c5d0eaae8d96d34f482f59e87dbb to your computer and use it in GitHub Desktop.
Unity cloud build hook for build downloading with nginx lua
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
server_name example.com;
root /var/www/example/httpdocs/;
include /etc/nginx/vhost-common.conf;
index index.html index.html index.php;
client_max_body_size 100m;
access_log /var/www/example/_logs/access.log;
error_log /var/www/example/_logs/error.log;
location / {
try_files $uri $uri/ =404;
}
location /server {
try_files $uri =404;
auth_basic "Password required";
auth_basic_user_file /var/www/example/server.htpasswd;
}
location /unity-cloud-build {
content_by_lua_block {
local json = require "cjson"
ngx.req.read_body()
local res = json.decode(ngx.req.get_body_data())
if not res then
ngx.log(ngx.ERR, "Invalid body", ngx.req.get_body_data())
return
end
local function fetch(_, url, saveTo)
local http = require "resty.http"
local httpc = http.new()
local io = require "io"
local scheme, host, port, path = unpack(httpc:parse_uri(url, true))
httpc:set_timeouts(10000, 60 * 60 * 1000, 60 * 60 * 1000)
-- reverse proxy downloads since the s3 unity uses is super slow to our particular network for some reason
-- config is available here if you need to host it on your own infrastructure: https://gist.github.com/imerr/7fb5ca8a03f7d81838be0f641bc5c956
local c, cerr httpc:connect("proxy.example.com", 443)
local handshakeOk, handshakeErr = httpc:ssl_handshake(nil, "proxy.example.com", true)
if not handshakeOk then
ngx.log(ngx.ERR, "SSL Handshake failed with rproxy.dl.je:" , handshakeErr)
return
end
local rpath = "/i/"
if scheme == "https" then
rpath = "/s/"
end
rpath = rpath .. host .. path
ngx.log(ngx.ERR, rpath)
local resp, rerr = httpc:request({
path = rpath,
version = 1.1,
method = "GET",
headers = {
["Host"] = "proxy.example.com",
["User-Agent"] = "resty-http"
},
})
if not resp then
ngx.log(ngx.ERR, "failed to request: ", rerr)
return
end
-- Now we can use the body_reader iterator, to stream the body according to our desired chunk size.
local reader = resp.body_reader
local f, ierr = io.open("/var/www/example/httpdocs/" .. saveTo , "wb")
if f==nil then
ngx.log(ngx.ERR, "Couldn't open file: "..ierr)
return
end
repeat
local chunk, err = reader(8192)
if err then
ngx.log(ngx.ERR, err)
break
end
if chunk then
f:write(chunk)
end
until not chunk
f:close()
httpc:close()
end
if res.buildStatus == "success" and res.buildTargetName == "My Build Name" then
local ok, err = ngx.timer.at(0, fetch, res.links.artifacts[1].files[1].href, "path_to_save_to/build_" .. res.buildNumber .. ".zip")
if not ok then
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end
end
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment