|
| 1 | +name: PythonKR - Publish static HTML to production |
| 2 | + |
| 3 | +run-name: "Static HTML: publish www.python.or.kr from cx43 by @${{ github.actor }}" |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + confirm: |
| 9 | + description: Type "publish static html www.python.or.kr" |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: pages |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + build_and_publish_static_html: |
| 19 | + if: >- |
| 20 | + github.ref == 'refs/heads/master' && |
| 21 | + github.actor == 'darjeeling' && |
| 22 | + github.triggering_actor == 'darjeeling' && |
| 23 | + inputs.confirm == 'publish static html www.python.or.kr' |
| 24 | + runs-on: ubuntu-latest |
| 25 | + timeout-minutes: 45 |
| 26 | + environment: |
| 27 | + name: production |
| 28 | + url: https://www.python.or.kr/ |
| 29 | + permissions: |
| 30 | + contents: write |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Request cx43 production static HTML build |
| 35 | + env: |
| 36 | + TOKEN: ${{ secrets.STATIC_PUBLISHER_PRODUCTION_TOKEN }} |
| 37 | + run: | |
| 38 | + set -euo pipefail |
| 39 | + payload=$(jq -nc \ |
| 40 | + --arg repository "$GITHUB_REPOSITORY" \ |
| 41 | + --arg run_id "$GITHUB_RUN_ID" \ |
| 42 | + --arg run_attempt "$GITHUB_RUN_ATTEMPT" \ |
| 43 | + '{repository:$repository,run_id:$run_id,run_attempt:$run_attempt}') |
| 44 | + curl --fail-with-body --silent --show-error \ |
| 45 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 46 | + -H 'Content-Type: application/json' \ |
| 47 | + --data "$payload" \ |
| 48 | + https://cx43.iz4u.net/hooks/static/publish/pythonkr/production |
| 49 | +
|
| 50 | + - name: Download and verify artifact |
| 51 | + env: |
| 52 | + TOKEN: ${{ secrets.STATIC_PUBLISHER_PRODUCTION_TOKEN }} |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | + base_url="https://cx43.iz4u.net/hooks/static" |
| 56 | + run_key="${GITHUB_RUN_ID}/${GITHUB_RUN_ATTEMPT}" |
| 57 | + status_url="${base_url}/runs/pythonkr/production/${run_key}" |
| 58 | + artifact_url="${base_url}/artifacts/pythonkr/production/${run_key}" |
| 59 | + status=queued |
| 60 | + for _ in $(seq 1 360); do |
| 61 | + status=$(curl --fail-with-body --silent --show-error \ |
| 62 | + -H "Authorization: Bearer ${TOKEN}" "$status_url" | jq -r .status) |
| 63 | + case "$status" in |
| 64 | + ready) break ;; |
| 65 | + build_failed) exit 1 ;; |
| 66 | + *) sleep 5 ;; |
| 67 | + esac |
| 68 | + done |
| 69 | + test "$status" = ready |
| 70 | + curl --fail-with-body --silent --show-error \ |
| 71 | + -H "Authorization: Bearer ${TOKEN}" \ |
| 72 | + --output artifact.tar.gz.tmp "$artifact_url" |
| 73 | + mv artifact.tar.gz.tmp artifact.tar.gz |
| 74 | + python - artifact.tar.gz site <<'PY' |
| 75 | + import pathlib |
| 76 | + import sys |
| 77 | + import tarfile |
| 78 | +
|
| 79 | + destination = pathlib.Path(sys.argv[2]) |
| 80 | + destination.mkdir() |
| 81 | + with tarfile.open(sys.argv[1], "r:gz") as archive: |
| 82 | + members = archive.getmembers() |
| 83 | + if any(not (member.isfile() or member.isdir()) for member in members): |
| 84 | + raise SystemExit("artifact contains a non-file member") |
| 85 | + archive.extractall(destination, members=members, filter="data") |
| 86 | + PY |
| 87 | + test -s site/index.html |
| 88 | + test -s site/sitemap.xml |
| 89 | + printf 'User-agent: *\nAllow: /\nSitemap: https://www.python.or.kr/sitemap.xml' \ |
| 90 | + > "$RUNNER_TEMP/expected-robots.txt" |
| 91 | + cmp -s "$RUNNER_TEMP/expected-robots.txt" site/robots.txt |
| 92 | + test ! -e site/CNAME |
| 93 | + test ! -e site/tr |
| 94 | + test ! -e site/rssitem-crawling |
| 95 | + test ! -e site/media/tr |
| 96 | + test ! -e site/media/rssitem-crawling |
| 97 | + test "$(find site -name '*.html' -type f -size 0 | wc -l)" -eq 0 |
| 98 | + python - https://www.python.or.kr site/sitemap.xml <<'PY' |
| 99 | + import sys |
| 100 | + from urllib.parse import urlsplit |
| 101 | + from xml.etree import ElementTree |
| 102 | +
|
| 103 | + origin = urlsplit(sys.argv[1]) |
| 104 | + locations = [ |
| 105 | + (element.text or "").strip() |
| 106 | + for element in ElementTree.parse(sys.argv[2]).iter() |
| 107 | + if element.tag.rsplit("}", 1)[-1] == "loc" |
| 108 | + ] |
| 109 | + if not locations: |
| 110 | + raise SystemExit("sitemap has no loc entries") |
| 111 | + for location in locations: |
| 112 | + parsed = urlsplit(location) |
| 113 | + if (parsed.scheme, parsed.netloc) != (origin.scheme, origin.netloc): |
| 114 | + raise SystemExit(f"non-canonical sitemap location: {location}") |
| 115 | + PY |
| 116 | + if grep -RIEq \ |
| 117 | + '(pao|pk)-static-staging\.iz4u\.net|(pao|pk)-staging\.iz4u\.net' site; then |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +
|
| 121 | + - name: Update production static HTML tree |
| 122 | + run: | |
| 123 | + set -euo pipefail |
| 124 | + rsync -a --delete site/ web/ |
| 125 | + git config user.name static-publisher |
| 126 | + git config user.email static-publisher@iz4u.net |
| 127 | + git add --all web |
| 128 | + if ! git diff --cached --quiet; then |
| 129 | + git commit -m "Publish PythonKR production from ${GITHUB_SHA}" |
| 130 | + git push origin HEAD:master |
| 131 | + fi |
| 132 | +
|
| 133 | + - name: Upload Pages artifact |
| 134 | + uses: actions/upload-pages-artifact@v3 |
| 135 | + with: |
| 136 | + path: web/ |
| 137 | + |
| 138 | + deploy_static_html_to_pages: |
| 139 | + needs: build_and_publish_static_html |
| 140 | + runs-on: ubuntu-latest |
| 141 | + permissions: |
| 142 | + pages: write |
| 143 | + id-token: write |
| 144 | + environment: |
| 145 | + name: github-pages |
| 146 | + url: ${{ steps.deployment.outputs.page_url }} |
| 147 | + steps: |
| 148 | + - name: Configure Pages |
| 149 | + uses: actions/configure-pages@v5 |
| 150 | + |
| 151 | + - name: Deploy static HTML to GitHub Pages |
| 152 | + id: deployment |
| 153 | + uses: actions/deploy-pages@v4 |
0 commit comments