Skip to content

fix deploy

fix deploy #1076

Workflow file for this run

name: Deploy
on:
push:
# Staging
branches: ["**"]
# Production
tags: ["[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"]
env:
BASEURL_PROD: https://robpol86.com/
BASEURL_STAGE: https://rob86stage.robpol86.com/
HTML_ROOT: ./docs/_build/html
IS_PROD: "${{ startsWith(github.ref, 'refs/tags/') && 'true' || '' }}"
PYTHON_VERSION: "3.10"
jobs:
build:
name: Build HTML
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Initialize Python and Poetry
uses: ./.github/actions/init
with:
python-version: "${{ env.PYTHON_VERSION }}"
- name: Build docs
env:
SPHINX_HTML_BASEURL: "${{ env.IS_PROD && env.BASEURL_PROD || env.BASEURL_STAGE }}"
run: make docs
- name: Store HTML as temporary artifact
uses: actions/upload-artifact@v3
with:
name: html
path: "${{ env.HTML_ROOT }}"
if-no-files-found: error
- name: Get version
id: version
env:
AWK_PROGRAM: |
@include ".github/workflows/deploy.awk"
match($0, /([0-9]{4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[[:space:]]*$/, ymd) {
setOutput("VERSION", sprintf("%d.%d.%d", ymd[1], ymd[2], ymd[3]))
flag = 1
exit
}
END { exit !flag }
run: ${{ env.IS_PROD && 'printenv GITHUB_REF_NAME' || 'poetry version' }} |awk -v "GITHUB_OUTPUT=$GITHUB_OUTPUT" "$AWK_PROGRAM"
- name: Find version in changelog
id: section_start
env:
AWK_PROGRAM: |
@include ".github/workflows/deploy.awk"
BEGIN {
version = "${{ steps.version.outputs.VERSION }}" # e.g. 2022.4.1
}
/^## \[[0-9]/ {
found = 1
if (!match($0, /^## \[([0-9]{4}[.][0-9]{1,2}[.][0-9]{1,2})]$/, a)) error("First section invalid format")
if (a[1] != version) error("First section doesnt match version " version)
setOutput("NR", NR+1)
exit 0
}
END {
if (!found) error("No section found")
}
run: awk -v "GITHUB_OUTPUT=$GITHUB_OUTPUT" "$AWK_PROGRAM" CHANGELOG.md
- name: Get title from changelog
id: title
env:
AWK_PROGRAM: |
@include ".github/workflows/deploy.awk"
BEGIN {
nr = int("${{ steps.section_start.outputs.NR }}")
}
NR < nr {
next
}
/^.+/ {
found = 1
if (!match($0, /^[A-Z]/)) error("Invalid title: " $0)
setOutput("TITLE", $0)
setOutput("NR", NR+1)
exit 0
}
END {
if (!found) error("No title found")
}
run: awk -v "GITHUB_OUTPUT=$GITHUB_OUTPUT" "$AWK_PROGRAM" CHANGELOG.md
- name: Get description from changelog if available
id: description
env:
AWK_PROGRAM: |
@include ".github/workflows/deploy.awk"
BEGIN {
nr = int("${{ steps.title.outputs.NR }}")
}
NR < nr {
next
}
/^## / {
exit 0
}
/^$/ && !found {
next # Skip empty lines after title.
}
/^.+/ {
found = 1
}
found {
lines[linesLen++] = $0
}
END {
# Drop trailing empty lines.
while (length(lines) && length(lines[length(lines)-1]) == 0) {
delete lines[length(lines)-1]
}
if (!length(lines)) {
print("No description available")
exit 0
}
# Join array.
for (idx in lines) {
# https://git.luolix.topmunity/t/set-output-truncates-multiline-strings/16852/2
line = gensub(/%/, "%25", "g", lines[idx])
if (idx == 0)
description = line
else
description = description "%0D" line
}
# Print.
setOutput("DESCRIPTION", description)
}
run: awk -v "GITHUB_OUTPUT=$GITHUB_OUTPUT" "$AWK_PROGRAM" CHANGELOG.md
outputs:
CL_TITLE: "${{ steps.title.outputs.TITLE }}"
CL_DESCRIPTION: "${{ steps.description.outputs.DESCRIPTION }}"
publish:
name: Publish to NFSN
runs-on: ubuntu-latest
needs: build
concurrency: "${{ github.workflow }}-${{ github.job }}"
steps:
- name: Fetch HTML files
uses: actions/download-artifact@v3
with:
name: html
path: html
- name: Setup SSH
uses: shimataro/ssh-key-action@v2
with:
key: "${{ secrets.NFSN_SSH_KEY }}"
known_hosts: >
|1|DPaY4V21SA8hqGGhvfofQD+4Q9s=|AxxSLwKe688+1aemg21SbQL4MxU= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICbXE1f5S3N/flFHUm2i97tzKGJUWzxotY1HHBMIX72h
- name: Rsync
env:
SSH_USER: "${{ env.IS_PROD && secrets.NFSN_SSH_USER_PROD || secrets.NFSN_SSH_USER_STAGE }}"
run: rsync -rptcivh --delete-after --stats ./html/ "$SSH_USER@ssh.nyc1.nearlyfreespeech.net:/home/public"
- name: Purge Cloudflare Cache
env:
CF_AUTH: "Authorization: Bearer ${{ secrets.TOKEN_CF_PURGE_CACHE }}"
CF_URL: "https://api.cloudflare.com/client/v4/zones/5efb26a03250a8bc392727af05a39aba/purge_cache"
run: curl -f "$CF_URL" -H "$CF_AUTH" --data '{"purge_everything":true}'
release:
name: Create Release
if: startsWith(github.ref, 'refs/tags/') # env.IS_PROD
runs-on: ubuntu-latest
needs: [build, publish]
steps:
- name: Fetch HTML files
uses: actions/download-artifact@v3
with:
name: html
path: html
- name: Archive HTML files
run: tar -czvf html.tar.gz html/
- name: Create release
uses: softprops/action-gh-release@v1
env:
NAME: "${{ needs.build.outputs.CL_TITLE }}"
BODY: "${{ needs.build.outputs.CL_DESCRIPTION }}"
with:
name: "${{ env.NAME }}"
body: "${{ env.BODY }}"
files: html.tar.gz