Skip to content

Commit 154ab71

Browse files
committed
[refactor] Switch to using GitHub Actions CI to deploy to the remote server
1 parent 01fc4b1 commit 154ab71

7 files changed

Lines changed: 208 additions & 7 deletions

File tree

pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@
6969
<crypto.xar.version>6.0.1</crypto.xar.version>
7070
<githubxq.xar.version>1.0.0</githubxq.xar.version>
7171

72+
<db-tmp-collection>/db/tmp</db-tmp-collection>
73+
74+
<git-repo-url>https://github.com/majlis-erc/manuForma</git-repo-url>
75+
<git-branch>main</git-branch>
76+
77+
<!-- NOTE(AR) Do not set this below, set it via an Environment Variable. See the `env-github-private-key` profile below -->
78+
<github-private-key>YOUR-SECRET-KEY</github-private-key>
79+
80+
<!-- NOTE(AR) Do not set this below, set it via an Environment Variable. See the `env-github-pat` profile below -->
81+
<github-pat>YOUR-TOKEN-HERE</github-pat>
82+
7283
</properties>
7384

7485
<dependencies>
@@ -212,8 +223,57 @@
212223
</plugins>
213224
</build>
214225

226+
215227
<profiles>
228+
229+
<profile>
230+
<!-- NOTE(AR) override the `github-private-key` property if set from an Environment Variable -->
231+
<id>env-github-private-key</id>
232+
<activation>
233+
<property>
234+
<name>env.GITHUB_PRIVATE_KEY</name>
235+
</property>
236+
</activation>
237+
<properties>
238+
<github-private-key>${env.GITHUB_PRIVATE_KEY}</github-private-key>
239+
</properties>
240+
</profile>
241+
242+
<profile>
243+
<!-- NOTE(AR) override the `github-pat` property if set from an Environment Variable -->
244+
<id>env-github-pat</id>
245+
<activation>
246+
<property>
247+
<name>env.GITHUB_PAT</name>
248+
</property>
249+
</activation>
250+
<properties>
251+
<github-pat>${env.GITHUB_PAT}</github-pat>
252+
</properties>
253+
</profile>
254+
255+
<profile>
256+
<!-- NOTE(AR) Build the scripts needed for use in CI -->
257+
<id>ci-resources</id>
258+
<activation>
259+
<property>
260+
<name>env.CI</name>
261+
<value>true</value>
262+
</property>
263+
</activation>
264+
<build>
265+
<resources>
266+
<resource>
267+
<directory>src/main/ci-resources</directory>
268+
<filtering>true</filtering>
269+
<targetPath>ci-resources</targetPath>
270+
</resource>
271+
</resources>
272+
</build>
273+
</profile>
274+
216275
<profile>
276+
<!-- NOTE(AR) Profile to build a Docker Image -->
217277
<id>docker</id>
218278
<properties>
219279
<!-- directory that contains the filtered Dockerfile -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
# Deletes a XAR file on a remote database server
6+
## Expects the following environment variables:
7+
# GITHUB_WORKSPACE - path to the checked out git project
8+
# REMOTE_EDB_SERVER_USERNAME - username for authenticating to the remote database server
9+
# REMOTE_EDB_SERVER_PASSWORD - password for authenticating to the remote database server
10+
# REMOTE_EDB_SERVER_URL - URL of a remote database server to upload the XAR file into
11+
12+
curl --request DELETE --basic --user "${REMOTE_EDB_SERVER_USERNAME}:${REMOTE_EDB_SERVER_PASSWORD}" "${REMOTE_EDB_SERVER_URL}/rest${db-tmp-collection}/${package-final-name}.xar"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Send a query to have a remote database server redeploy a XAR package
6+
## Expects the following environment variables:
7+
# GITHUB_WORKSPACE - path to the checked out git project
8+
# REMOTE_EDB_SERVER_USERNAME - username for authenticating to the remote database server
9+
# REMOTE_EDB_SERVER_PASSWORD - password for authenticating to the remote database server
10+
# REMOTE_EDB_SERVER_URL - URL of a remote database server to upload the XAR file into
11+
12+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
13+
14+
# Read in the XQuery file
15+
XQUERY=`cat ${SCRIPT_DIR}/redeploy-xar-package.xq`
16+
17+
POST_BODY=`cat << EOF
18+
<query xmlns="http://exist.sourceforge.net/NS/exist" cache="no" enclose="no" start="1" max="-1">
19+
<text><![CDATA[
20+
${XQUERY}
21+
]]></text>
22+
</query>
23+
EOF
24+
`
25+
26+
echo "${POST_BODY}" | curl --request POST --basic --user "${REMOTE_EDB_SERVER_USERNAME}:${REMOTE_EDB_SERVER_PASSWORD}" --header "Content-Type: application/xml" --data-binary @- "${REMOTE_EDB_SERVER_URL}/rest${db-tmp-collection}"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
xquery version "3.1";
2+
3+
(:~
4+
: Simple script to uninstall a package, and install a new
5+
: version of the package from a xar file uploaded to a
6+
: location in the db.
7+
:)
8+
declare namespace repo = "http://exist-db.org/xquery/repo";
9+
declare namespace util = "http://exist-db.org/xquery/util";
10+
11+
declare variable $existdb-public-repo-url := "http://exist-db.org/exist/apps/public-repo";
12+
13+
(:~
14+
: Name of the package to uninstall and install a new version of.
15+
:)
16+
declare variable $pkg-name := "${package-name}";
17+
18+
(:~
19+
: Location where the new version of the xar file for the package
20+
: can be found in the database.
21+
:)
22+
declare variable $pkg-db-tmp-path := "${db-tmp-collection}/${package-final-name}.xar";
23+
24+
declare variable $undeploy-error := xs:QName("undeploy-error");
25+
declare variable $remove-error := xs:QName("remove-error");
26+
declare variable $no-pkg-error := xs:QName("no-pkg-error");
27+
declare variable $install-and-deploy-error := xs:QName("no-pkg-error");
28+
29+
30+
declare function local:pkg-installed($pkg-name as xs:string) as xs:boolean {
31+
exists(repo:list()[. eq $pkg-name])
32+
};
33+
34+
declare function local:undeploy-and-remove-pkg($pkg-name as xs:string) as empty-sequence() {
35+
let $undeploy-result := repo:undeploy($pkg-name)
36+
return
37+
if ($undeploy-result/@result ne "ok")
38+
then
39+
fn:error($undeploy-error, "Unable to undeploy existing package: " || $pkg-name)
40+
else
41+
let $remove-result := repo:remove($pkg-name)
42+
return
43+
if ($remove-result eq fn:false())
44+
then
45+
fn:error($remove-error, "Unable to remove existing package: " || $pkg-name)
46+
else
47+
()
48+
};
49+
50+
51+
if (local:pkg-installed($pkg-name))
52+
then
53+
local:undeploy-and-remove-pkg($pkg-name)
54+
else ()
55+
56+
,
57+
58+
if (util:binary-doc-available($pkg-db-tmp-path) eq fn:false())
59+
then
60+
fn:error($no-pkg-error, "Package: " || $pkg-name || ", cannot be found at: " || $pkg-db-tmp-path)
61+
else
62+
let $install-and-deploy-result := repo:install-and-deploy-from-db($pkg-db-tmp-path)
63+
return
64+
if ($install-and-deploy-result/@result ne "ok")
65+
then
66+
fn:error($install-and-deploy-error, "Unable to install and deploy package: " || $pkg-name)
67+
else
68+
<success timestamp="{util:system-dateTime()}"/>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
# Uploads a XAR file to a remote database server
6+
## Expects the following environment variables:
7+
# GITHUB_WORKSPACE - path to the checked out git project
8+
# REMOTE_EDB_SERVER_USERNAME - username for authenticating to the remote database server
9+
# REMOTE_EDB_SERVER_PASSWORD - password for authenticating to the remote database server
10+
# REMOTE_EDB_SERVER_URL - URL of a remote database server to upload the XAR file into
11+
12+
curl --request PUT --basic --user "${REMOTE_EDB_SERVER_USERNAME}:${REMOTE_EDB_SERVER_PASSWORD}" --header 'Content-Type: application/expath+xar' --data-binary "@$GITHUB_WORKSPACE/target/${package-final-name}.xar" "${REMOTE_EDB_SERVER_URL}/rest${db-tmp-collection}/${package-final-name}.xar"

src/main/xar-resources/modules/git-sync.xql

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,34 @@ xquery version "3.1";
1616

1717
import module namespace githubxq="http://exist-db.org/lib/githubxq";
1818

19+
(:~
20+
: Path to eXist-db application.
21+
:)
22+
declare variable $local:db-application-path := "/db/apps/${package-target}";
23+
(:~
24+
: GitHub repository.
25+
:)
26+
declare variable $local:git-repo := "${git-repo-url}";
27+
(:~
28+
: GitHub branch to get data from.
29+
:)
30+
declare variable $local:git-branch := "${git-branch}";
31+
(:~
32+
: Private key for GitHub authentication.
33+
: See: https://developer.github.com/webhooks/securing/
34+
:)
35+
declare variable $local:github-private-key := "${github-private-key}";
36+
(:~
37+
: Git Token used for rate limiting.
38+
: See: https://developer.github.com/v3/#rate-limiting
39+
:)
40+
declare variable $local:github-rate-limit-token := "";
41+
1942
let $data := request:get-data()
2043
return
2144
githubxq:execute-webhook($data,
22-
'PATH-TO-YOUR-EXISTDB-REPOSITORY',
23-
'PATH-TO-YOUT-GITHUB-REPOSITORY',
24-
'OPTIONAL-BRANCH-NAME',
25-
'YOUR-SECRET-KEYE',
26-
'OPTIONAL-RATE-LIMIT-KEY')
45+
$local:db-application-path,
46+
$local:git-repo,
47+
$local:git-branch,
48+
$local:github-private-key,
49+
$local:github-rate-limit-token)

src/main/xar-resources/services/git-commit.xql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ import module namespace http="http://expath.org/ns/http-client";
4444
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
4545
declare namespace json = "http://www.json.org";
4646

47-
(: Set up global varaibles :)
48-
declare variable $gitcommit:authorization-token := 'YOUR-TOKEN-HERE';
47+
(: Set up global variables :)
48+
declare variable $gitcommit:authorization-token := '${github-pat}';
4949
declare variable $gitcommit:apiURL := 'https://api.github.com';
5050
declare variable $gitcommit:owner := if(request:get-parameter('githubOwner','') != '') then request:get-parameter('githubOwner','') else 'wsalesky';
5151
declare variable $gitcommit:repoName := if(request:get-parameter('githubRepoName','') != '') then request:get-parameter('githubRepoName','') else 'blogs';

0 commit comments

Comments
 (0)