GitHub Actions CI/CD Pipeline Configuration #5

Closed
opened 2025-06-01 18:23:19 +02:00 by bennolor · 0 comments
Owner

Description:

Set up GitHub Actions to automate the building and deployment of the Hugo blog whenever changes are pushed to the main branch.

Tasks:

  1. Create SSH key pair for deployment:
    • ssh-keygen -t ed25519 -C "github-actions-deploy"
    • Add public key to authorized_keys on the server
    • Add private key as a GitHub repository secret named SSH_PRIVATE_KEY
  2. Add additional repository secrets:
    • SERVER_HOST: Your server's hostname or IP
    • SERVER_USER: SSH username
    • SERVER_PORT: SSH port (usually 22)
    • DEPLOY_PATH: Path to deployment directory on server
  3. Create GitHub Actions workflow file .github/workflows/deploy.yml:
name: Build and Deploy

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify

      - name: Install SSH Key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          known_hosts: unnecessary
          if_key_exists: replace

      - name: Adding Known Hosts
        run: ssh-keyscan -p ${{ secrets.SERVER_PORT }} -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts

      - name: Deploy with rsync
        run: |
          rsync -avz --delete -e "ssh -p ${{ secrets.SERVER_PORT }}" \
            ./public/ ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:${{ secrets.DEPLOY_PATH }}

      - name: Restart Docker container
        run: |
          ssh -p ${{ secrets.SERVER_PORT }} ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
            "cd ${{ secrets.DEPLOY_PATH }} && docker-compose -f docker-compose.prod.yml up -d --build"
  1. Test the GitHub Actions workflow:
    • Make a small change to the repository
    • Push to main branch
    • Monitor the GitHub Actions tab to ensure the workflow runs successfully
  2. Verify that changes appear on the live site

Definition of Done:

  • SSH key pair created and configured
  • GitHub repository secrets configured
  • GitHub Actions workflow file created and committed
  • CI/CD pipeline successfully builds and deploys the site
  • Changes to the main branch automatically reflect on the live site
## **Description:** Set up GitHub Actions to automate the building and deployment of the Hugo blog whenever changes are pushed to the main branch. ## **Tasks:** 1. Create SSH key pair for deployment: - `ssh-keygen -t ed25519 -C "github-actions-deploy"` - Add public key to authorized_keys on the server - Add private key as a GitHub repository secret named SSH_PRIVATE_KEY 2. Add additional repository secrets: - SERVER_HOST: Your server's hostname or IP - SERVER_USER: SSH username - SERVER_PORT: SSH port (usually 22) - DEPLOY_PATH: Path to deployment directory on server 3. Create GitHub Actions workflow file `.github/workflows/deploy.yml`: ```yaml name: Build and Deploy on: push: branches: [ main ] jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: submodules: true fetch-depth: 0 - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: 'latest' extended: true - name: Build run: hugo --minify - name: Install SSH Key uses: shimataro/ssh-key-action@v2 with: key: ${{ secrets.SSH_PRIVATE_KEY }} known_hosts: unnecessary if_key_exists: replace - name: Adding Known Hosts run: ssh-keyscan -p ${{ secrets.SERVER_PORT }} -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts - name: Deploy with rsync run: | rsync -avz --delete -e "ssh -p ${{ secrets.SERVER_PORT }}" \ ./public/ ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:${{ secrets.DEPLOY_PATH }} - name: Restart Docker container run: | ssh -p ${{ secrets.SERVER_PORT }} ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \ "cd ${{ secrets.DEPLOY_PATH }} && docker-compose -f docker-compose.prod.yml up -d --build" ``` 4. Test the GitHub Actions workflow: - Make a small change to the repository - Push to main branch - Monitor the GitHub Actions tab to ensure the workflow runs successfully 5. Verify that changes appear on the live site ## **Definition of Done:** - SSH key pair created and configured - GitHub repository secrets configured - GitHub Actions workflow file created and committed - CI/CD pipeline successfully builds and deploys the site - Changes to the main branch automatically reflect on the live site
bennolor added this to the Initial Development and Deployment project 2025-06-01 18:29:57 +02:00
bennolor added this to the Release milestone 2025-06-01 18:29:59 +02:00
bennolor started working 2025-06-01 20:17:00 +02:00
bennolor worked for 1 hour 24 minutes 2025-06-01 21:41:47 +02:00
bennolor moved this to Done in Initial Development and Deployment on 2025-06-01 21:42:18 +02:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Total Time Spent: 1 hour 24 minutes
bennolor
1 hour 24 minutes
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: bennolor/hugoblog#5