forked from manoelcampos/asciidoctor-ghpages-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·121 lines (100 loc) · 4.38 KB
/
entrypoint.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Exit if a command fails
set -e
OWNER="$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f 1)"
if [[ "$INPUT_ADOC_FILE_EXT" != .* ]]; then
INPUT_ADOC_FILE_EXT=".$INPUT_ADOC_FILE_EXT";
fi
echo "Configure git"
apk add git -q > /dev/null
apk add openssh-client -q > /dev/null
# Changes in gh-pages branch will be shown as the "GitHub Action" user.
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
# Gets latest commit hash for pushed branch
COMMIT_HASH=$(git rev-parse HEAD)
git fetch --all
# Checks if the user has provided a specific source dir different from the root.
# Only the files in the source dir are processed.
HAS_SOURCE_DIR=true
if [[ "$INPUT_SOURCE_DIR" == "." || "$INPUT_SOURCE_DIR" == "./" ]]; then
HAS_SOURCE_DIR=false
fi
# If a source dir was provided, remove all other files and directories and
# keept just the ones in the source dir, moving them to the root dir.
if [[ $HAS_SOURCE_DIR == true ]]; then
echo "Checking out the gh-pages branch on $INPUT_SOURCE_DIR (without keeping its history) from commit $COMMIT_HASH"
git branch -D gh-pages 1>/dev/null 2>/dev/null || true
git checkout -q --orphan gh-pages "$COMMIT_HASH" 1>/dev/null
mv "$INPUT_SOURCE_DIR" /tmp/source
#Ignores directories . and .git
find . -not -path './.git*' -not -name '.' -exec rm -rf {} \; || true
mv /tmp/source/* .
git add .
else
echo "Checking out the gh-pages branch (keeping its history) from commit $COMMIT_HASH"
git checkout "$COMMIT_HASH" -B gh-pages
fi
# Executes any arbitrary shell command (such as packages installation and environment setup)
# before starting build.
# If no command is provided, the default value is just an echo command.
eval "$INPUT_PRE_BUILD"
if [[ $INPUT_SLIDES_SKIP_ASCIIDOCTOR_BUILD == false ]]; then
echo "Converting AsciiDoc files to HTML"
find . -name "*$INPUT_ADOC_FILE_EXT" -exec asciidoctor -b html $INPUT_ASCIIDOCTOR_PARAMS {} \;
find . -name "README.html" -execdir ln -s "README.html" "index.html" \;
find . -name "*$INPUT_ADOC_FILE_EXT" -exec git rm -f --cached {} \;
fi
PDF_FILE="ebook.pdf"
if [[ $INPUT_PDF_BUILD == true ]]; then
INPUT_EBOOK_MAIN_ADOC_FILE="$INPUT_EBOOK_MAIN_ADOC_FILE$INPUT_ADOC_FILE_EXT"
MSG="Building $PDF_FILE ebook from $INPUT_EBOOK_MAIN_ADOC_FILE"
echo "$MSG"
asciidoctor-pdf "$INPUT_EBOOK_MAIN_ADOC_FILE" -o "$PDF_FILE" $INPUT_ASCIIDOCTOR_PARAMS
fi
SLIDES_FILE="slides.html"
if [[ $INPUT_SLIDES_BUILD == true ]]; then
echo "Build AsciiDoc Reveal.js slides"
INPUT_SLIDES_MAIN_ADOC_FILE="$INPUT_SLIDES_MAIN_ADOC_FILE$INPUT_ADOC_FILE_EXT"
MSG="Building $SLIDES_FILE with AsciiDoc Reveal.js from $INPUT_SLIDES_MAIN_ADOC_FILE"
echo "$MSG"
asciidoctor-revealjs -a revealjsdir=https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.9.2 "$INPUT_SLIDES_MAIN_ADOC_FILE" -o "$SLIDES_FILE"
fi
# Executes any post-processing command provided by the user, before changes are committed.
# If no command is provided, the default value is just an echo command.
echo "Running post build command."
eval "$INPUT_POST_BUILD"
echo "Adding output files to gh-pages branch."
if [[ $INPUT_SLIDES_SKIP_ASCIIDOCTOR_BUILD == false ]]; then
find . -name "*.html" -exec git add -f {} \;
fi
if [[ $INPUT_PDF_BUILD == true ]]; then
git add -f "$PDF_FILE";
fi
if [[ $INPUT_SLIDES_BUILD == true ]]; then
git add -f "$SLIDES_FILE";
fi
MSG="Build $INPUT_ADOC_FILE_EXT Files for GitHub Pages from $COMMIT_HASH"
git rm -rf .github/ || true
echo "Committing changes to gh-pages branch"
git commit -m "$MSG" 1>/dev/null
# Avoids confirmation about unknown host when pushing changes via ssh.
echo "
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
" > /etc/ssh/ssh_config
# If the action is being run into the GitHub Servers,
# the checkout action (which is being used)
# automatically authenticates the container using ssh.
# If the action is running locally, for instance using https://github.com/nektos/act,
# we need to push via https with a Personal Access Token
# which should be provided by an env variable.
# We ca run the action locally using act with:
# act -s GITHUB_TOKEN=my_github_personal_access_token
if ! ssh -T [email protected] > /dev/null 2>/dev/null; then
URL="https://[email protected]/$GITHUB_REPOSITORY.git"
git remote remove origin
git remote add origin "$URL"
fi
echo "Pushing changes back to the remote repository"
git push -f --set-upstream origin gh-pages