Skip to content

Commit 92af551

Browse files
committed
Add rsync-schema
Closes gh-38
1 parent 4b758cb commit 92af551

File tree

20 files changed

+882
-1
lines changed

20 files changed

+882
-1
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
with:
1515
submodules: true
16-
- name: Run BATS tests
16+
- name: Run rsync-antora-reference BATS tests
1717
run: ./rsync-antora-reference/test/bats/bin/bats ./rsync-antora-reference/test/
18+
- name: Run rsync-schema BATS tests
19+
run: ./rsync-schema/test/bats/bin/bats ./rsync-schema/test/

rsync-schema/action.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: rsync Antora Docs
2+
description: A GitHub action that syncs Antora reference documentation using rsync with support for syncing a single version. It deploys to the docs server using the github repository in the path.
3+
branding:
4+
icon: 'copy'
5+
color: 'green'
6+
inputs:
7+
docs-username:
8+
description: The username to connect to the docs server
9+
required: true
10+
docs-host:
11+
description: The host of the docs server
12+
required: true
13+
docs-ssh-key:
14+
description: The ssh key used to connect to the docs-host
15+
required: true
16+
docs-ssh-host-key:
17+
description: The docs ssh host key used to connect to docs-host
18+
required: true
19+
dry-run:
20+
description: Set to false if should perform the sync, else a dry run is performed
21+
default: false
22+
required: false
23+
site-path:
24+
description: The path to the schema that should be synced
25+
default: build/schema
26+
required: false
27+
version:
28+
description: The schema version being published
29+
required: true
30+
31+
runs:
32+
using: 'composite'
33+
steps:
34+
- id: publish-docs
35+
shell: bash
36+
run: |
37+
PATH=$PATH:${{ github.action_path }}/src
38+
flag_options=''
39+
if [ -z "${{ inputs.dry-run }}" ]; then
40+
flag_options="${flag_options} --dry-run"
41+
fi
42+
${{ github.action_path }}/src/action.sh --docs-username "${{ inputs.docs-username }}" --docs-host "${{ inputs.docs-host }}" --docs-ssh-key "${{ inputs.docs-ssh-key }}" --docs-ssh-host-key "${{ inputs.docs-ssh-host-key }}" --site-path "${{ inputs.site-path }}" --version "${{ inputs.version }}" --github-repository "${{ github.repository }}" $flag_options

rsync-schema/src/action.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/bin/bash
2+
3+
__action_usage() {
4+
echo "usage: action.sh [OPTION]...
5+
6+
--docs-username=USERNAME the username used to connect to ssh
7+
--docs-host=HOST the host to connect to ssh
8+
--docs-ssh-key=KEY the private key used to connect to ssh
9+
--docs-ssh-host-key=HOST_KEY the host key used to connect to ssh
10+
--dry-run signals that rsync should be in dry run mode
11+
--site-path=PATH the local directory path to sync to the server. Default build/schema
12+
--version=VERSION the release version for the schema
13+
--github-repository=GH_REPO the github repository (e.g. spring-projects/spring-security)
14+
"
15+
}
16+
17+
__action_usage_error() {
18+
echo "Error: $1" >&2
19+
__action_usage
20+
exit 1
21+
}
22+
23+
__action() {
24+
local docs_username docs_host docs_ssh_key docs_ssh_host_key site_path version github_repository valid_args
25+
local rsync_flag_options=''
26+
if [ ! -z "$BUILD_REFNAME" ]; then
27+
rsync_flag_options="${rsync_flag_options} --build-ref-name $BUILD_REFNAME"
28+
fi
29+
valid_args=$(getopt --options '' --long docs-username:,docs-host:,docs-ssh-key:,docs-ssh-host-key:,dry-run,site-path:,version:,github-repository: -- "$@")
30+
if [[ $? -ne 0 ]]; then
31+
__action_usage
32+
exit 1;
33+
fi
34+
35+
eval set -- "$valid_args"
36+
37+
while [ : ]; do
38+
case "$1" in
39+
--docs-username)
40+
docs_username="$2"
41+
shift 2
42+
;;
43+
--docs-host)
44+
docs_host="$2"
45+
shift 2
46+
;;
47+
--docs-ssh-key)
48+
docs_ssh_key="$2"
49+
shift 2
50+
;;
51+
--docs-ssh-host-key)
52+
docs_ssh_host_key="$2"
53+
shift 2
54+
;;
55+
--dry-run)
56+
rsync_flag_options="${rsync_flag_options} --dry-run"
57+
shift
58+
;;
59+
--site-path)
60+
site_path="$2"
61+
shift 2
62+
;;
63+
--version)
64+
version="$2"
65+
shift 2
66+
;;
67+
--github-repository)
68+
github_repository="$2"
69+
shift 2
70+
;;
71+
--) shift;
72+
break
73+
;;
74+
*)
75+
__action_usage_error "Invalid argument $1 $2"
76+
;;
77+
esac
78+
done
79+
80+
if [ -z "$docs_username" ]; then
81+
__action_usage_error "Missing option '--docs-username'"
82+
fi
83+
if [ -z "$docs_host" ]; then
84+
__action_usage_error "Missing option '--docs-host'"
85+
fi
86+
if [ -z "$docs_ssh_key" ]; then
87+
__action_usage_error "Missing option '--docs-ssh-key'"
88+
fi
89+
if [ -z "$docs_ssh_host_key" ]; then
90+
__action_usage_error "Missing option '--docs-ssh-host-key'"
91+
fi
92+
if [ -z "$site_path" ]; then
93+
site_path="build/schema"
94+
fi
95+
if [ -z "$version" ]; then
96+
__action_usage_error "Missing option '--version'"
97+
fi
98+
if [ -z "$github_repository" ]; then
99+
__action_usage_error "Missing option '--github-repository'"
100+
fi
101+
102+
103+
## Extract repository_name from the owner/repository_name
104+
local github_repository_name="$(echo ${github_repository} | cut -d '/' -f 2)"
105+
local ssh_private_key_path="$HOME/.ssh/${github_repository:-publish-docs}"
106+
local ssh_host="${docs_username}@${docs_host}"
107+
108+
(
109+
set -e
110+
set -f
111+
112+
local ssh_host_path="/var/www/domains/spring.io/docs/htdocs/autorepo/schema/$github_repository_name/$version/"
113+
local pwd="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
114+
setup_ssh.sh --ssh-private-key-path "$ssh_private_key_path" --ssh-private-key "$docs_ssh_key" --ssh-known-host "$docs_ssh_host_key"
115+
rsync_docs.sh --ssh-host "$ssh_host" --ssh-host-path "$ssh_host_path" --local-path "$site_path" --ssh-private-key-path "$ssh_private_key_path" $rsync_flag_options
116+
)
117+
exit_code=$?
118+
119+
cleanup_ssh.sh --ssh-private-key-path "$ssh_private_key_path"
120+
121+
exit $exit_code
122+
}
123+
124+
125+
__action "$@"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
__check_github_repository_owner_usage() {
4+
echo "usage: check_github_repository_owner.sh [OPTION]...
5+
6+
--github-repository=REPO the github repository (e.g. spring-projects/spring-security)
7+
--ssh-docs-path=PATH the full path that the docs will be deployed to (e.g. https://docs.spring.io/spring-security/reference/ is \${HTTP_DOCS}/spring-security/reference)
8+
"
9+
}
10+
11+
__check_github_repository_owner_usage_error() {
12+
echo "Error: $1" >&2
13+
__check_github_repository_owner_usage
14+
exit 1
15+
}
16+
17+
__check_github_repository_owner() {
18+
local github_repository ssh_docs_path valid_args
19+
valid_args=$(getopt --options '' --long ,github-repository:,ssh-docs-path: -- "$@")
20+
if [[ $? -ne 0 ]]; then
21+
__check_github_repository_owner_usage
22+
exit 1;
23+
fi
24+
25+
eval set -- "$valid_args"
26+
27+
while [ : ]; do
28+
case "$1" in
29+
--github-repository)
30+
github_repository="$2"
31+
shift 2
32+
;;
33+
--ssh-docs-path)
34+
ssh_docs_path="$2"
35+
shift 2
36+
;;
37+
--) shift;
38+
break
39+
;;
40+
*)
41+
__check_github_repository_owner_usage_error "Invalid argument $1 $2"
42+
;;
43+
esac
44+
done
45+
46+
if ! [[ "$github_repository" =~ .+/.+ ]]; then
47+
__check_github_repository_owner_usage_error " '--github-repository' must be in the form of <owner>/<name> but got '$github_repository'"
48+
fi
49+
if ! [[ "$ssh_docs_path" =~ ^/.+ ]]; then
50+
__check_github_repository_owner_usage_error " '--ssh-docs-path' must start with and not equal / but got '$ssh_docs_path'"
51+
fi
52+
53+
local marker_file="${ssh_docs_path}/.github-repository"
54+
55+
if [ -d "$ssh_docs_path" ]; then
56+
# The path exists so ensure the marker file contents contain github_repository
57+
local marker_file_content=""
58+
if [ -f "$marker_file" ]; then
59+
marker_file_content="$(cat $marker_file)"
60+
fi
61+
if [ "$marker_file_content" == "$github_repository" ]; then
62+
echo "Owner is verified"
63+
exit 0
64+
else
65+
echo "Failed to verify that $ssh_docs_path is owned by $github_repository because the file $marker_file contains $marker_file_content" >&2
66+
exit 2
67+
fi
68+
else
69+
# The path does not yet exist so create the folder and add the marker file
70+
echo "Directory $ssh_docs_path does not exist. Marking as owned by $github_repository"
71+
mkdir -p "$ssh_docs_path"
72+
echo -n "$github_repository" >$marker_file
73+
exit 0
74+
fi
75+
76+
}
77+
78+
__check_github_repository_owner "$@"

rsync-schema/src/cleanup_ssh.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
__cleanup_ssh_usage() {
4+
echo "usage: cleanup_ssh.sh [OPTION]...
5+
6+
--ssh-private-key-path=PATH the path to the private key to use
7+
"
8+
}
9+
10+
__cleanup_ssh_usage_error() {
11+
echo "Error: $1" >&2
12+
usage
13+
exit 1
14+
}
15+
16+
__cleanup_ssh() {
17+
local ssh_private_key_path valid_args
18+
valid_args=$(getopt --options '' --long ssh-private-key-path: -- "$@")
19+
if [[ $? -ne 0 ]]; then
20+
usage
21+
exit 1;
22+
fi
23+
24+
eval set -- "$valid_args"
25+
26+
while [ : ]; do
27+
case "$1" in
28+
--ssh-private-key-path)
29+
ssh_private_key_path="$2"
30+
shift 2
31+
;;
32+
--) shift;
33+
break
34+
;;
35+
*)
36+
__cleanup_ssh_usage_error "Invalid argument $1 $2"
37+
;;
38+
esac
39+
done
40+
41+
rm -f "$ssh_private_key_path"
42+
}
43+
44+
__cleanup_ssh "$@"

0 commit comments

Comments
 (0)