aboutsummaryrefslogtreecommitdiff
path: root/dream-api.lua
blob: c1ce887643555ea82581f2df279aaf9d917a8071 (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
58
59
60
61
62
63
64
65
66
-- 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

        -- 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'

os.execute('cd /srv/www/public.cat')

-- 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)
os.execute('/usr/bin/find ' .. bare_repo .. ' -type d -exec chmod 0700 {} \\;')
os.execute('/usr/bin/find ' .. bare_repo .. ' -type f -exec chmod 0600 {} \\;')

-- Prepare deployment directory
cmd_deploy_dir = 'mkdir -m 0755 -p ' .. deploy_dir
ngx.say(cmd_deploy_dir)
os.execute('mkdir -m 0755 -p ' .. deploy_dir)

-- Deploy the repository
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)

-- Fix permissions
os.execute('/usr/bin/find ' .. deploy_dir .. ' -type d -exec chmod 0755 {} \\;')
os.execute('/usr/bin/find ' .. deploy_dir .. ' -type f -exec chmod 0644 {} \\;')

ngx.exit(ngx.OK)