aboutsummaryrefslogtreecommitdiff
path: root/dream-api.lua
diff options
context:
space:
mode:
Diffstat (limited to 'dream-api.lua')
-rw-r--r--dream-api.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/dream-api.lua b/dream-api.lua
new file mode 100644
index 0000000..6aaad25
--- /dev/null
+++ b/dream-api.lua
@@ -0,0 +1,57 @@
+-- SPDX-FileCopyrightText: 2021 hellekin
+-- SPDX-FileCopyrightText: 2021 petites singularités
+-- SPDX-License-Identifier: AGPL-3.0-or-later
+
+--[[ dream-api.lua ]]
+
+-- Require POST method to avoid search spider bots
+if ngx.var.request_method ~= "POST" then
+ ngx.say('Try POST')
+ ngx.exit(ngx.OK)
+end
+
+-- By default deploy the main branch to stage
+tag = "main"
+to = ngx.var.to
+deploy_dir = "/srv/www/public.cat/dream-stage"
+
+-- Git repository
+upstream_repo = 'https://gitlab.com/public.dream/dream.public.cat.git/'
+bare_repo = '/srv/www/public.cat/dream.git'
+
+-- In production require an existing Git tag to deploy
+if to == 'production' then
+ tag = ngx.var.tag .. '' -- git tag to deploy
+ ngx.say("tag is now: " .. tag)
+
+ -- TODO Check whether it's a known tag (and exit)
+
+ -- Check that the tag exists before deploying (or exit)
+ upsteam_tag = 'https://gitlab.com/public.dream/dream.public.cat/-/tags/' .. tag
+
+ -- Save tag to avoid deploying it again
+ deploy_dir = deploy_dir.gsub(deploy_dir, '-stage', '') -- deploy to production
+
+end
+
+ngx.say('Deploying ', tag, ' to ', to)
+
+local os = require 'os'
+
+-- Checkout the site
+cmd_clone = 'test -f ' .. bare_repo .. '/config || umask 022 && git clone --bare -- ' .. upstream_repo .. ' ' .. bare_repo
+ngx.say(cmd_clone)
+os.execute(cmd_clone)
+
+cmd_deploy_dir = 'mkdir -m 0755 -p ' .. deploy_dir
+ngx.say(cmd_deploy_dir)
+os.execute('mkdir -m 0755 -p ' .. deploy_dir)
+
+cmd_fetch = 'git --git-dir ' .. bare_repo .. ' fetch'
+--cmd_deploy = cmd_fetch .. ' && ' .. cmd_fetch .. ' -t && cd ' .. deploy_dir .. ' && git --git-dir ' .. bare_repo .. ' --work-tree ' .. deploy_dir .. ' checkout -f ' .. tag
+cmd_deploy = cmd_fetch .. ' -t && cd ' .. deploy_dir .. ' && git --git-dir ' .. bare_repo .. ' --work-tree ' .. deploy_dir .. ' checkout -f ' .. tag
+ngx.say(cmd_deploy)
+os.execute(cmd_deploy)
+
+ngx.exit(ngx.OK)
+