@@ -106,79 +106,105 @@ runs:
106106 run : |
107107 # Exit codes are handled by script.
108108 set +e
109-
110- host_name=$(echo "${{ github.event.repository.owner.name }}.github.io" | tr "[:upper:]" "[:lower:]")
109+
110+ pages_path="${{ inputs.pages-path }}"
111+ owner_name="${{ github.event.repository.owner.name }}"
111112 repository_name="${{ github.event.repository.name }}"
113+ default_branch="${{ inputs.default-branch }}"
114+ pages_archive="${{ inputs.pages-archive }}"
115+ branch="${{ github.ref_name }}"
116+
117+ host_name=$(echo "$owner_name.github.io" | tr "[:upper:]" "[:lower:]")
112118 lower_repository_name=$(echo "$repository_name" | tr "[:upper:]" "[:lower:]")
113119
114120 if [[ "$lower_repository_name" == "$host_name" ]]
115121 then
122+ echo "Repository is GitHub Pages default"
116123 pages_url="https://$host_name"
117124 else
125+ echo "Repository is not GitHub Pages default"
118126 pages_url="https://$host_name/$repository_name"
119127 fi
120128
121- pages_archive=${{ inputs.pages-archive }}
129+ # Get GitHub Pages archive.
130+ curl -fsSL "$pages_url/$pages_archive.zip" -o "$pages_archive.zip"
122131
123- # Get and unzip GitHub Pages archive.
124- # Fail on 404 and similar so we fall back properly
125- curl -fSL "$pages_url/${{ inputs.pages-archive }}.zip" -o "${{ inputs.pages-archive }}.zip"
126- if unzip -qq "${{ inputs.pages-archive }}.zip" -d "${{ inputs.pages-archive }}"; then
127- rm "${{ inputs.pages-archive }}.zip"
132+ if [[ $? -eq 0 ]]
133+ then
134+ echo "GitHub Pages archive downloaded"
135+
136+ # Unzip will create the directory.
137+ unzip -qq "$pages_archive".zip -d "$pages_archive"
138+ rm "$pages_archive".zip
128139 else
129- echo "No existing pages archive; seeding new one"
130- mkdir -p "${{ inputs.pages-archive }}"
131- cp -r "${{ inputs.pages-path }}" "${{ inputs.pages-archive }}/"
140+ echo "No GitHub Pages archive downloaded"
141+
142+ # Copy existing output as the root, creating directory if necessary.
143+ # If not the default branch, this will overwrite the root, but it's a one-time cost.
144+ cp -r "$pages_path" "$pages_archive/"
132145 fi
133-
146+
134147 cd "$pages_archive"
135148
136149 if [[ -d _branch ]]
137150 then
151+ echo "Directory _branch exists"
152+
138153 cd _branch
139-
140- branches=$(git ls-remote --branches | grep -oE "[^/]+$")
154+
155+ # Pipes spawn extra shells that don't share variables so write to file and then read from it.
156+ git ls-remote --branches | grep -oE "[^/]+$" > remote_branches 2>/dev/null
157+ readarray -t remote_branches < remote_branches
158+ rm remote_branches
141159
142- ls | while read -r branch
160+ find . -type d -print | grep -oE "[^./].*" | while read -r archive_branch
143161 do
144- # Check that branch still exists.
145- echo "$branches" | grep -q "^$branch$"
146-
147- if [[ $? -ne 0 ]]
162+ branch_matched=0
163+
164+ for remote_branch in "${remote_branches[@]}"
165+ do
166+ if [[ "$remote_branch" == "$archive_branch" || "$remote_branch" == "$archive_branch/"* ]]
167+ then
168+ branch_matched=1
169+ fi
170+ done
171+
172+ if [[ $branch_matched -eq 0 ]]
148173 then
149- echo "Deleting stale branch $branch "
174+ echo "Deleting stale branch $archive_branch "
150175
151176 # Branch no longer exists; delete from GitHub Pages.
152- rm -rf "$branch "
177+ rm -rf "$archive_branch "
153178 fi
154179 done
155-
180+
156181 cd ..
157182 else
158183 mkdir _branch
159184 fi
160185
161- branch=${{ github.ref_name }}
162-
163- if [[ "$branch" == "${{ inputs.default-branch }}" ]]
186+ if [[ "$branch" == "$default_branch" ]]
164187 then
165188 # Move entire _branch directory.
166- mv _branch "../${{ inputs.pages-path }} "
189+ mv _branch "../$pages_path "
167190 else
168191 # Remove previous branch content if it exists and replace it with newly generated content.
169192 rm -rf "_branch/$branch"
170- mkdir "_branch/$branch"
171- mv "../${{ inputs.pages-path }} "/* "_branch/$branch"
172-
193+ mkdir -p "_branch/$branch"
194+ mv "../$pages_path "/* "_branch/$branch"
195+
173196 # Move consolidated directory into place.
174- mv * "../${{ inputs.pages-path }} "
197+ mv * "../$pages_path "
175198 fi
176-
177- cd "../${{ inputs.pages-path }}"
178-
199+
200+ cd "../$pages_path"
201+
202+ # Create archive and move into place for publication.
179203 zip -q -r ../"$pages_archive" .
180204 mv ../"$pages_archive".zip .
181-
205+
206+ echo "Pages archive created"
207+
182208 cd ..
183209
184210 - name : Configure GitHub Pages
0 commit comments