Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: true
- name: Run BATS tests
- name: Run rsync-antora-reference BATS tests
run: ./rsync-antora-reference/test/bats/bin/bats ./rsync-antora-reference/test/
- name: Run rsync-schema BATS tests
run: ./rsync-schema/test/bats/bin/bats ./rsync-schema/test/
42 changes: 42 additions & 0 deletions rsync-schema/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: rsync Antora Docs
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.
branding:
icon: 'copy'
color: 'green'
inputs:
docs-username:
description: The username to connect to the docs server
required: true
docs-host:
description: The host of the docs server
required: true
docs-ssh-key:
description: The ssh key used to connect to the docs-host
required: true
docs-ssh-host-key:
description: The docs ssh host key used to connect to docs-host
required: true
dry-run:
description: Set to false if should perform the sync, else a dry run is performed
default: false
required: false
site-path:
description: The path to the schema that should be synced
default: build/schema
required: false
version:
description: The schema version being published
required: true

runs:
using: 'composite'
steps:
- id: publish-docs
shell: bash
run: |
PATH=$PATH:${{ github.action_path }}/src
flag_options=''
if [ -z "${{ inputs.dry-run }}" ]; then
flag_options="${flag_options} --dry-run"
fi
${{ 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
122 changes: 122 additions & 0 deletions rsync-schema/src/action.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash

__action_usage() {
echo "usage: action.sh [OPTION]...

--docs-username=USERNAME the username used to connect to ssh
--docs-host=HOST the host to connect to ssh
--docs-ssh-key=KEY the private key used to connect to ssh
--docs-ssh-host-key=HOST_KEY the host key used to connect to ssh
--dry-run signals that rsync should be in dry run mode
--site-path=PATH the local directory path to sync to the server. Default build/schema
--version=VERSION the release version for the schema
--github-repository=GH_REPO the github repository (e.g. spring-projects/spring-security)
"
}

__action_usage_error() {
echo "Error: $1" >&2
__action_usage
exit 1
}

__action() {
local docs_username docs_host docs_ssh_key docs_ssh_host_key site_path version github_repository valid_args
local rsync_flag_options=''
valid_args=$(getopt --options '' --long docs-username:,docs-host:,docs-ssh-key:,docs-ssh-host-key:,dry-run,site-path:,version:,github-repository: -- "$@")
if [[ $? -ne 0 ]]; then
__action_usage
exit 1;
fi

eval set -- "$valid_args"

while [ : ]; do
case "$1" in
--docs-username)
docs_username="$2"
shift 2
;;
--docs-host)
docs_host="$2"
shift 2
;;
--docs-ssh-key)
docs_ssh_key="$2"
shift 2
;;
--docs-ssh-host-key)
docs_ssh_host_key="$2"
shift 2
;;
--dry-run)
rsync_flag_options="${rsync_flag_options} --dry-run"
shift
;;
--site-path)
site_path="$2"
shift 2
;;
--version)
version="$2"
shift 2
;;
--github-repository)
github_repository="$2"
shift 2
;;
--) shift;
break
;;
*)
__action_usage_error "Invalid argument $1 $2"
;;
esac
done

if [ -z "$docs_username" ]; then
__action_usage_error "Missing option '--docs-username'"
fi
if [ -z "$docs_host" ]; then
__action_usage_error "Missing option '--docs-host'"
fi
if [ -z "$docs_ssh_key" ]; then
__action_usage_error "Missing option '--docs-ssh-key'"
fi
if [ -z "$docs_ssh_host_key" ]; then
__action_usage_error "Missing option '--docs-ssh-host-key'"
fi
if [ -z "$site_path" ]; then
site_path="build/schema"
fi
if [ -z "$version" ]; then
__action_usage_error "Missing option '--version'"
fi
if [ -z "$github_repository" ]; then
__action_usage_error "Missing option '--github-repository'"
fi


## Extract repository_name from the owner/repository_name
local github_repository_name="$(echo ${github_repository} | cut -d '/' -f 2)"
local ssh_private_key_path="$HOME/.ssh/${github_repository:-publish-docs}"
local ssh_host="${docs_username}@${docs_host}"

(
set -e
set -f

local ssh_host_path="/var/www/domains/spring.io/docs/htdocs/autorepo/schema/$github_repository_name/$version/"
local pwd="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
setup_ssh.sh --ssh-private-key-path "$ssh_private_key_path" --ssh-private-key "$docs_ssh_key" --ssh-known-host "$docs_ssh_host_key"
rsync_schema.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
)
exit_code=$?

cleanup_ssh.sh --ssh-private-key-path "$ssh_private_key_path"

exit $exit_code
}


__action "$@"
78 changes: 78 additions & 0 deletions rsync-schema/src/check_github_repository_owner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

__check_github_repository_owner_usage() {
echo "usage: check_github_repository_owner.sh [OPTION]...

--github-repository=REPO the github repository (e.g. spring-projects/spring-security)
--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)
"
}

__check_github_repository_owner_usage_error() {
echo "Error: $1" >&2
__check_github_repository_owner_usage
exit 1
}

__check_github_repository_owner() {
local github_repository ssh_docs_path valid_args
valid_args=$(getopt --options '' --long ,github-repository:,ssh-docs-path: -- "$@")
if [[ $? -ne 0 ]]; then
__check_github_repository_owner_usage
exit 1;
fi

eval set -- "$valid_args"

while [ : ]; do
case "$1" in
--github-repository)
github_repository="$2"
shift 2
;;
--ssh-docs-path)
ssh_docs_path="$2"
shift 2
;;
--) shift;
break
;;
*)
__check_github_repository_owner_usage_error "Invalid argument $1 $2"
;;
esac
done

if ! [[ "$github_repository" =~ .+/.+ ]]; then
__check_github_repository_owner_usage_error " '--github-repository' must be in the form of <owner>/<name> but got '$github_repository'"
fi
if ! [[ "$ssh_docs_path" =~ ^/.+ ]]; then
__check_github_repository_owner_usage_error " '--ssh-docs-path' must start with and not equal / but got '$ssh_docs_path'"
fi

local marker_file="${ssh_docs_path}/.github-repository"

if [ -d "$ssh_docs_path" ]; then
# The path exists so ensure the marker file contents contain github_repository
local marker_file_content=""
if [ -f "$marker_file" ]; then
marker_file_content="$(cat $marker_file)"
fi
if [ "$marker_file_content" == "$github_repository" ]; then
echo "Owner is verified"
exit 0
else
echo "Failed to verify that $ssh_docs_path is owned by $github_repository because the file $marker_file contains $marker_file_content" >&2
exit 2
fi
else
# The path does not yet exist so create the folder and add the marker file
echo "Directory $ssh_docs_path does not exist. Marking as owned by $github_repository"
mkdir -p "$ssh_docs_path"
echo -n "$github_repository" >$marker_file
exit 0
fi

}

__check_github_repository_owner "$@"
44 changes: 44 additions & 0 deletions rsync-schema/src/cleanup_ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

__cleanup_ssh_usage() {
echo "usage: cleanup_ssh.sh [OPTION]...

--ssh-private-key-path=PATH the path to the private key to use
"
}

__cleanup_ssh_usage_error() {
echo "Error: $1" >&2
usage
exit 1
}

__cleanup_ssh() {
local ssh_private_key_path valid_args
valid_args=$(getopt --options '' --long ssh-private-key-path: -- "$@")
if [[ $? -ne 0 ]]; then
usage
exit 1;
fi

eval set -- "$valid_args"

while [ : ]; do
case "$1" in
--ssh-private-key-path)
ssh_private_key_path="$2"
shift 2
;;
--) shift;
break
;;
*)
__cleanup_ssh_usage_error "Invalid argument $1 $2"
;;
esac
done

rm -f "$ssh_private_key_path"
}

__cleanup_ssh "$@"
95 changes: 95 additions & 0 deletions rsync-schema/src/rsync_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

__rsync_schema_usage() {
echo "usage: rsync_schema.sh [OPTION]...

--ssh-host=SSH_HOST the ssh host in the format USER@HOST to connect to ssh
--ssh-private-key-path=KEY_PATH the path to the private key used to connect to ssh
--ssh-host-path=HOST_PATH the path of the ssh server to sync the documentation to
--dry-run signals that rsync should be in dry run mode
--local-path=PATH the local directory path to sync to the server
"
}

__rsync_schema_usage_error() {
echo "Error: $1" >&2
__rsync_schema_usage
exit 1
}

__rsync_schema() {
local ssh_host ssh_host_path local_path ssh_private_key_path
local dry_run="false"
valid_args=$(getopt --options '' --long ssh-host:,ssh-host-path:,dry-run,local-path:,ssh-private-key-path: -- "$@")
if [[ $? -ne 0 ]]; then
__rsync_schema_usage
exit 1;
fi

eval set -- "$valid_args"

while [ : ]; do
case "$1" in
--ssh-host)
ssh_host="$2"
shift 2
;;
--ssh-host-path)
ssh_host_path="$2"
shift 2
;;
--local-path)
local_path="$2"
shift 2
;;
--ssh-private-key-path)
ssh_private_key_path="$2"
shift 2
;;
--dry-run)
dry_run=true
shift
;;
--) shift;
break
;;
*)
__rsync_schema_usage_error "Invalid argument $1 $2"
;;
esac
done

if [ -z "$ssh_host" ]; then
__rsync_schema_usage_error "Missing option '--ssh-host'"
fi
if [ -z "$ssh_host_path" ]; then
__rsync_schema_usage_error "Missing option '--ssh-host-path'"
fi
if [ -z "$local_path" ]; then
__rsync_schema_usage_error "Missing option '--local-path'"
fi
if [ -z "$ssh_private_key_path" ]; then
__rsync_schema_usage_error "Missing option '--ssh-private-key-path'"
fi

local rsync_opts='-avz --delete '
if [ "$dry_run" != "false" ]; then
rsync_opts="$rsync_opts --dry-run "
fi
if [ -f "$local_path/.htaccess" ]; then
rsync_opts="$rsync_opts --include /.htaccess "
fi
if [ -d "$local_path/.cache" ]; then
rsync_opts="$rsync_opts$(find $local_path/.cache -printf ' --include /.cache/%P')"
fi
rsync_opts="$rsync_opts --exclude /.github-repository --exclude /.cache --exclude /.cache/* "
# Disable filename expansion (globbing)
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
(
set -f
rsync $rsync_opts -e "ssh -i $ssh_private_key_path" $local_path/ "$ssh_host:$ssh_host_path"
)
}


__rsync_schema "$@"
Loading