# Forgejo Actions Workflow for Hugo Blog Auto-Deployment # # This workflow automatically builds and deploys your Hugo site when you push to main name: Build and Deploy Hugo Site on: push: branches: - main workflow_dispatch: # Allow manual triggering jobs: deploy: runs-on: docker steps: - name: Checkout repository uses: actions/checkout@v4 with: submodules: recursive # Required for Hugo themes fetch-depth: 0 # Fetch full history for .GitInfo and .Lastmod - name: Generate timestamp id: timestamp run: echo "BUILD_TIME=$(date +%Y%m%d-%H%M%S)" >> $GITHUB_OUTPUT - name: Set up Go uses: actions/setup-go@v5 with: go-version: '1.24' cache: true - name: Install Hugo run: | go install -tags extended github.com/gohugoio/hugo@latest echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - name: Verify Hugo installation run: hugo version - name: Build Hugo site run: hugo --minify - name: Set up SSH run: | mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key chmod 600 ~/.ssh/deploy_key echo "${{ vars.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts - name: Create deployment directory env: SSH_HOST: ${{ vars.SSH_HOST }} SSH_USER: ${{ vars.SSH_USER }} SSH_PORT: ${{ vars.SSH_PORT }} DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} BUILD_TIME: ${{ steps.timestamp.outputs.BUILD_TIME }} run: | ssh -i ~/.ssh/deploy_key -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} \ "mkdir -p ${DEPLOY_PATH}-${BUILD_TIME}" - name: Deploy via SCP env: SSH_HOST: ${{ vars.SSH_HOST }} SSH_USER: ${{ vars.SSH_USER }} SSH_PORT: ${{ vars.SSH_PORT }} DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} BUILD_TIME: ${{ steps.timestamp.outputs.BUILD_TIME }} run: | scp -i ~/.ssh/deploy_key -P ${SSH_PORT} -r public/* \ ${SSH_USER}@${SSH_HOST}:${DEPLOY_PATH}-${BUILD_TIME}/ - name: Update symlink env: SSH_HOST: ${{ vars.SSH_HOST }} SSH_USER: ${{ vars.SSH_USER }} SSH_PORT: ${{ vars.SSH_PORT }} DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} BUILD_TIME: ${{ steps.timestamp.outputs.BUILD_TIME }} run: | ssh -i ~/.ssh/deploy_key -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} \ "ln -sfn ${DEPLOY_PATH}-${BUILD_TIME} ${DEPLOY_PATH}" - name: Clean up old deployments env: SSH_HOST: ${{ vars.SSH_HOST }} SSH_USER: ${{ vars.SSH_USER }} SSH_PORT: ${{ vars.SSH_PORT }} DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} run: | ssh -i ~/.ssh/deploy_key -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} \ "cd $(dirname ${DEPLOY_PATH}) && ls -dt public-* | tail -n +6 | xargs rm -rf" continue-on-error: true # Don't fail if cleanup fails - name: Deployment summary run: | echo "✅ Hugo site deployed successfully!" echo "Build time: ${{ steps.timestamp.outputs.BUILD_TIME }}" echo "Deployed to: ${{ vars.SSH_HOST }}"