Skip to main content
  1. Blog/

Creating a CI/CD Pipeline for Automated Website Deployment

Table of Contents

This post shows how to automate the deployment of a Hugo website on a self-hosted Forgejo server, using Forgejo Actions. Forgejo Actions is similar to GitHub Actions. Using a CI/CD pipeline saves time and effort during deployment. When changes to the website are pushed to the repository, the website gets compiled and published automatically. For this to work, the Forgejo Actions runnter must be installed and activated.

Steps:

  1. Activate Actions on the repository:

    • Enable Forgejo Actions for the repository to allow workflow execution. This can usually be done through the repository settings under Settings / Advanced Settings / Enable Repository Actions.
  2. Create a user on the host and enable SSH login:

    • Set up a deployment user and configure SSH access to the server for secure communication.
    • Save the SSH key in the repository settings under Settings / Actions / Secrets
  3. Create a configuration file for the repository in /.forgejo/workflows:

    • Define the CI/CD pipeline configuration to specify the deployment process. This involves creating a YAML file that outlines the steps required to build and deploy the site.
  4. Push changes to the repository for the Action to run:

    • Commit and push changes to the repository. This action triggers the workflow, leading to the automatic deployment of the website.

Here is an example workflow configuration:

name: Deploy website

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: docker

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y rsync wget
          wget https://github.com/gohugoio/hugo/releases/download/v0.128.2/hugo_extended_0.128.2_linux-amd64.deb
          dpkg -i hugo_extended_0.128.2_linux-amd64.deb

      - name: Build Hugo site
        run: |
          hugo

      - name: Deploy to server
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          mkdir -p ~/.ssh
          echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H <IP> >> ~/.ssh/known_hosts
          rsync -av --delete -e "ssh -i ~/.ssh/id_rsa" ./public/ user@<IP>:/opt/webserver/web/website/

After everything is set up, push some changes to the repository to see if it works:

When everything is set up correctly, the workflow should show a lot of green checkmarks to indicate success. The website is now deployed.

Lars Ursprung
Author
Lars Ursprung