summaryrefslogtreecommitdiff
path: root/dream-api.lua
blob: 6aaad2506d30a7b119c2130e6c97d9b951ae376b (plain)
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
-- 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)