|
| 1 | +name: Deploy Static Sites |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + products_dir: |
| 7 | + description: Root directory that contains product folders. |
| 8 | + type: string |
| 9 | + required: false |
| 10 | + default: products |
| 11 | + deploy_path: |
| 12 | + description: Remote deploy base path. |
| 13 | + type: string |
| 14 | + required: false |
| 15 | + default: /var/www/static-sites |
| 16 | + force_deploy: |
| 17 | + description: Deploy all product folders, ignoring git diff. |
| 18 | + type: boolean |
| 19 | + required: false |
| 20 | + default: false |
| 21 | + max_parallel: |
| 22 | + description: Max parallel rsync jobs. |
| 23 | + type: number |
| 24 | + required: false |
| 25 | + default: 3 |
| 26 | + reload_nginx: |
| 27 | + description: Reload nginx after deploy. |
| 28 | + type: boolean |
| 29 | + required: false |
| 30 | + default: true |
| 31 | + nginx_container: |
| 32 | + description: Docker container name that runs nginx. |
| 33 | + type: string |
| 34 | + required: false |
| 35 | + default: static-sites |
| 36 | + secrets: |
| 37 | + SERVER_HOST: |
| 38 | + required: true |
| 39 | + SERVER_USER: |
| 40 | + required: true |
| 41 | + SERVER_PASS: |
| 42 | + required: true |
| 43 | + |
| 44 | +permissions: |
| 45 | + contents: read |
| 46 | + |
| 47 | +jobs: |
| 48 | + detect-changes: |
| 49 | + runs-on: ubuntu-latest |
| 50 | + outputs: |
| 51 | + changed_products: ${{ steps.changes.outputs.products }} |
| 52 | + has_changes: ${{ steps.changes.outputs.has_changes }} |
| 53 | + steps: |
| 54 | + - uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + fetch-depth: 0 |
| 57 | + |
| 58 | + - name: Detect changed products |
| 59 | + id: changes |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + set -euo pipefail |
| 63 | +
|
| 64 | + PRODUCTS_DIR="${{ inputs.products_dir }}" |
| 65 | + PRODUCTS_DIR="${PRODUCTS_DIR%/}" |
| 66 | +
|
| 67 | + if ! [[ "$PRODUCTS_DIR" =~ ^[a-zA-Z0-9._/-]+$ ]]; then |
| 68 | + echo "Invalid products_dir: $PRODUCTS_DIR" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + if [[ "$PRODUCTS_DIR" == -* ]]; then |
| 72 | + echo "products_dir must not start with '-'" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +
|
| 76 | + if [ ! -d "$PRODUCTS_DIR" ]; then |
| 77 | + echo "Products dir not found: $PRODUCTS_DIR" |
| 78 | + echo 'products=[]' >> "$GITHUB_OUTPUT" |
| 79 | + echo 'has_changes=false' >> "$GITHUB_OUTPUT" |
| 80 | + exit 0 |
| 81 | + fi |
| 82 | +
|
| 83 | + if [ "${{ inputs.force_deploy }}" = "true" ]; then |
| 84 | + PRODUCTS=$(find "$PRODUCTS_DIR" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | jq -R -s -c 'split("\n")[:-1]') |
| 85 | + else |
| 86 | + EVENT_NAME="${{ github.event_name }}" |
| 87 | + BEFORE_SHA="${{ github.event.before }}" |
| 88 | + AFTER_SHA="${{ github.sha }}" |
| 89 | +
|
| 90 | + if [ "$EVENT_NAME" = "push" ] && [ -n "$BEFORE_SHA" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then |
| 91 | + CHANGED_FILES=$(git diff --name-only "$BEFORE_SHA" "$AFTER_SHA" -- "$PRODUCTS_DIR/" || true) |
| 92 | + elif git rev-parse --verify HEAD~1 >/dev/null 2>&1; then |
| 93 | + CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD -- "$PRODUCTS_DIR/" || true) |
| 94 | + else |
| 95 | + CHANGED_FILES="" |
| 96 | + fi |
| 97 | +
|
| 98 | + if [ -z "${CHANGED_FILES:-}" ]; then |
| 99 | + PRODUCTS='[]' |
| 100 | + else |
| 101 | + PRODUCTS=$(echo "$CHANGED_FILES" | awk -v dir="$PRODUCTS_DIR" ' |
| 102 | + index($0, dir"/")==1 { |
| 103 | + rest = substr($0, length(dir)+2) |
| 104 | + split(rest, parts, "/") |
| 105 | + if (parts[1] != "") print parts[1] |
| 106 | + }' | sort -u | jq -R -s -c 'split("\n")[:-1]') |
| 107 | + fi |
| 108 | + fi |
| 109 | +
|
| 110 | + PRODUCTS=$(echo "$PRODUCTS" | jq -r '.[]' | while IFS= read -r product; do |
| 111 | + [ -z "$product" ] && continue |
| 112 | + if [[ "$product" =~ ^[a-zA-Z0-9._][a-zA-Z0-9._-]*$ ]] && [ -d "$PRODUCTS_DIR/$product" ]; then |
| 113 | + echo "$product" |
| 114 | + fi |
| 115 | + done | jq -R -s -c 'split("\n")[:-1]') |
| 116 | +
|
| 117 | + echo "products=$PRODUCTS" >> "$GITHUB_OUTPUT" |
| 118 | +
|
| 119 | + PRODUCT_COUNT=$(echo "$PRODUCTS" | jq 'length') |
| 120 | + if [ "$PRODUCT_COUNT" -eq 0 ]; then |
| 121 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 122 | + else |
| 123 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 124 | + fi |
| 125 | +
|
| 126 | + echo "Changed products: $PRODUCTS" |
| 127 | +
|
| 128 | + deploy: |
| 129 | + needs: detect-changes |
| 130 | + if: needs.detect-changes.outputs.has_changes == 'true' |
| 131 | + runs-on: ubuntu-latest |
| 132 | + strategy: |
| 133 | + matrix: |
| 134 | + product: ${{ fromJson(needs.detect-changes.outputs.changed_products) }} |
| 135 | + max-parallel: ${{ inputs.max_parallel }} |
| 136 | + |
| 137 | + steps: |
| 138 | + - uses: actions/checkout@v4 |
| 139 | + |
| 140 | + - name: Validate inputs |
| 141 | + shell: bash |
| 142 | + env: |
| 143 | + PRODUCTS_DIR: ${{ inputs.products_dir }} |
| 144 | + DEPLOY_PATH: ${{ inputs.deploy_path }} |
| 145 | + PRODUCT: ${{ matrix.product }} |
| 146 | + run: | |
| 147 | + set -euo pipefail |
| 148 | +
|
| 149 | + PRODUCTS_DIR="${PRODUCTS_DIR%/}" |
| 150 | + DEPLOY_PATH="${DEPLOY_PATH%/}" |
| 151 | +
|
| 152 | + fail() { echo "$1" >&2; exit 1; } |
| 153 | +
|
| 154 | + [[ -n "$PRODUCTS_DIR" ]] || fail "products_dir is empty" |
| 155 | + [[ "$PRODUCTS_DIR" =~ ^[a-zA-Z0-9._/-]+$ ]] || fail "Invalid products_dir: $PRODUCTS_DIR" |
| 156 | + [[ "$PRODUCTS_DIR" != -* ]] || fail "products_dir must not start with '-'" |
| 157 | + [[ -n "$DEPLOY_PATH" ]] || fail "deploy_path is empty" |
| 158 | + [[ "$DEPLOY_PATH" =~ ^[a-zA-Z0-9._/-]+$ ]] || fail "Invalid deploy_path: $DEPLOY_PATH" |
| 159 | + [[ "$DEPLOY_PATH" != -* ]] || fail "deploy_path must not start with '-'" |
| 160 | + [[ -n "$PRODUCT" ]] || fail "product is empty" |
| 161 | + [[ "$PRODUCT" =~ ^[a-zA-Z0-9._][a-zA-Z0-9._-]*$ ]] || fail "Invalid product: $PRODUCT" |
| 162 | +
|
| 163 | + echo "PRODUCTS_DIR=$PRODUCTS_DIR" >> "$GITHUB_ENV" |
| 164 | + echo "DEPLOY_PATH=$DEPLOY_PATH" >> "$GITHUB_ENV" |
| 165 | + echo "PRODUCT=$PRODUCT" >> "$GITHUB_ENV" |
| 166 | +
|
| 167 | + if [ ! -d "$PRODUCTS_DIR/$PRODUCT" ]; then |
| 168 | + echo "Source directory not found: $PRODUCTS_DIR/$PRODUCT (skipping)" |
| 169 | + echo "SKIP_DEPLOY=true" >> "$GITHUB_ENV" |
| 170 | + exit 0 |
| 171 | + fi |
| 172 | +
|
| 173 | + - name: Install sshpass |
| 174 | + run: | |
| 175 | + sudo apt-get update |
| 176 | + sudo apt-get install -y sshpass |
| 177 | +
|
| 178 | + - name: Add server to known hosts |
| 179 | + run: | |
| 180 | + mkdir -p ~/.ssh |
| 181 | + ssh-keyscan -H "${{ secrets.SERVER_HOST }}" >> ~/.ssh/known_hosts |
| 182 | +
|
| 183 | + - name: Deploy ${{ matrix.product }} |
| 184 | + env: |
| 185 | + SSHPASS: ${{ secrets.SERVER_PASS }} |
| 186 | + run: | |
| 187 | + set -euo pipefail |
| 188 | +
|
| 189 | + if [ "${SKIP_DEPLOY:-false}" = "true" ]; then |
| 190 | + echo "Skipping deploy for $PRODUCT" |
| 191 | + exit 0 |
| 192 | + fi |
| 193 | +
|
| 194 | + echo "Deploying product: $PRODUCT" |
| 195 | + sshpass -e rsync -avz --delete \ |
| 196 | + --exclude='.git*' \ |
| 197 | + -e "ssh" \ |
| 198 | + -- \ |
| 199 | + "$PRODUCTS_DIR/$PRODUCT/" \ |
| 200 | + "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:$DEPLOY_PATH/$PRODUCT/" |
| 201 | +
|
| 202 | + reload-nginx: |
| 203 | + needs: deploy |
| 204 | + if: ${{ inputs.reload_nginx }} |
| 205 | + runs-on: ubuntu-latest |
| 206 | + steps: |
| 207 | + - name: Install sshpass |
| 208 | + run: | |
| 209 | + sudo apt-get update |
| 210 | + sudo apt-get install -y sshpass |
| 211 | +
|
| 212 | + - name: Add server to known hosts |
| 213 | + run: | |
| 214 | + mkdir -p ~/.ssh |
| 215 | + ssh-keyscan -H "${{ secrets.SERVER_HOST }}" >> ~/.ssh/known_hosts |
| 216 | +
|
| 217 | + - name: Validate inputs |
| 218 | + shell: bash |
| 219 | + env: |
| 220 | + NGINX_CONTAINER: ${{ inputs.nginx_container }} |
| 221 | + run: | |
| 222 | + set -euo pipefail |
| 223 | +
|
| 224 | + fail() { echo "$1" >&2; exit 1; } |
| 225 | +
|
| 226 | + [[ -n "$NGINX_CONTAINER" ]] || fail "nginx_container is empty" |
| 227 | + [[ "$NGINX_CONTAINER" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.-]*$ ]] || fail "Invalid nginx_container: $NGINX_CONTAINER" |
| 228 | + echo "NGINX_CONTAINER=$NGINX_CONTAINER" >> "$GITHUB_ENV" |
| 229 | +
|
| 230 | + - name: Reload Nginx |
| 231 | + env: |
| 232 | + SSHPASS: ${{ secrets.SERVER_PASS }} |
| 233 | + run: | |
| 234 | + set -euo pipefail |
| 235 | +
|
| 236 | + sshpass -e ssh \ |
| 237 | + "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}" \ |
| 238 | + "docker exec $NGINX_CONTAINER nginx -s reload || true" |
0 commit comments