Correct path for upload in merge job
[releng.git] / jjb / releng / docu-build-new.sh
1 #!/bin/bash
2 set -e
3 set -o pipefail
4
5 export PATH=$PATH:/usr/local/bin/
6 git_sha1="$(git rev-parse HEAD)"
7
8 clean() {{
9 if [[ -d docs/output ]]; then
10 rm -rf docs/output
11 echo "cleaning up output directory"
12 fi
13 }}
14
15 trap clean EXIT TERM INT SIGTERM SIGHUP
16
17 #set git_sha1
18 files=()
19 while read -r -d ''; do
20   files+=("$REPLY")
21 done < <(find docs/ -type f -iname '*.rst' -print0)
22 for file in "${{files[@]}}"; do
23   sed -i "s/_sha1_/$git_sha1/g" $file
24 done
25
26 directories=()
27 while read -d $'\n'; do
28   directories+=("$REPLY")
29 done < <(find docs/ -name 'index.rst' -printf '%h\n' | sort -u )
30
31 for dir in "${{directories[@]}}"; do
32   echo
33   echo "#############################"
34   echo "Building DOCS in ${{dir##*/}}"
35   echo "#############################"
36   echo
37
38   if [[ ! -d docs/output/"${{dir##*/}}/" ]]; then
39     mkdir -p docs/output/"${{dir##*/}}/"
40   fi
41
42   sphinx-build -b html -E -c docs/etc/ ""$dir"/" docs/output/"${{dir##*/}}/"
43 done
44
45 # NOTE: make sure source parameters for GS paths are not empty.
46 [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
47 [[ $GERRIT_PROJECT =~ .+ ]]
48 [[ $GERRIT_BRANCH =~ .+ ]]
49 gs_path_review="artifacts.opnfv.org/review/$GERRIT_CHANGE_NUMBER"
50
51 for dir in "${{directories[@]}}"; do
52   echo
53   echo "#############################"
54   echo "UPLOADING DOCS in ${{dir##*/}}"
55   echo "#############################"
56   echo
57
58   if [[ $GERRIT_BRANCH = "master" ]] ; then
59     gs_path_branch="artifacts.opnfv.org/$GERRIT_PROJECT"
60   else
61     gs_path_branch="artifacts.opnfv.org/$GERRIT_PROJECT/${{GERRIT_BRANCH##*/}}"
62   fi
63
64   if [[ $JOB_NAME =~ "verify" ]] ; then
65
66     #upload artifacts for verify job
67     gsutil cp -r docs/output/"${{dir##*/}}/" "gs://$gs_path_review/"
68
69     # post link to gerrit as comment
70     gerrit_comment="$(echo '"Document is available at 'http://$gs_path_review/"${{dir##*/}}"/index.html' for review"')"
71     echo "$gerrit_comment"
72     ssh -p 29418 gerrit.opnfv.org gerrit review -p $GERRIT_PROJECT -m \
73     "$gerrit_comment" $GERRIT_PATCHSET_REVISION
74
75     #set cache to 0
76     for x in $(gsutil ls gs://$gs_path_review/"${{dir##*/}}" | grep html);
77     do
78       gsutil setmeta -h "Content-Type:text/html" \
79       -h "Cache-Control:private, max-age=0, no-transform" \
80       "$x"
81     done
82
83   else
84
85     #upload artifacts for merge job
86     gsutil cp -r docs/output/"${{dir##*/}}/" "gs://$gs_path_branch/"${{dir}}/""
87     echo "Latest document is available at http://$gs_path_branch/"${{dir}}/"index.html"
88
89     #set cache to 0
90     for x in $(gsutil ls gs://$gs_path_branch/"${{dir}}" | grep html);
91     do
92       gsutil setmeta -h "Content-Type:text/html" \
93       -h "Cache-Control:private, max-age=0, no-transform" \
94       "$x"
95     done
96
97     #Clean up review when merging
98     if gsutil ls "gs://$gs_path_review" > /dev/null 2>&1 ; then
99       echo
100       echo "Deleting Out-of-dated Documents..."
101       gsutil rm -r "gs://$gs_path_review"
102     fi
103
104   fi
105
106 done