Deploying a Hugo Application with GitHub Actions
GitHub Actions is a powerful tool for automating processes in GitHub repositories. In this article, we will discuss how to deploy a Hugo-based website using GitHub Actions.
Configuring GitHub Actions
Create a file named
.github/workflows/deploy.ymlin your repository.Add the following configuration to the
deploy.ymlfile:
name: Deploy website
on:
  push:
    branches:
      - master
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Get files
        uses: actions/checkout@v3
        with: 
          submodules: true
          fetch-depth: 0
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.101.0'
          extended: true
      - name: Build
        run: hugo 
      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with: 
          key: ${{ secrets.SSH_KEY }}
          known_hosts: ${{ secrets.KNOWN_HOSTS }}
      - name: Deploy
        run: rsync -avhe "ssh -p ${{ secrets.SSH_PORT }}" --rsync-path="sudo rsync" ./public/ ${{ secrets.SSH_TARGET }}:${{ secrets.SSH_PATH }} --delete
Adding Variables in GitHub Repository
To add variables like secrets.SSH_PORT to your GitHub repository:
Go to the settings of your repository on GitHub.
In the “Secrets” section, you can add new variables. For example, to add
SSH_PORT:- Click on “New repository secret.”
 - Give it the name 
SSH_PORT. - Enter the appropriate SSH port value.
 
You can do the same for other secrets like
SSH_KEY,KNOWN_HOSTS,SSH_TARGET, andSSH_PATH, which are used in the deployment script.
Now, after each code push to the master branch, GitHub Actions will automatically trigger the deployment process of your Hugo-based website to the target server using the provided secrets.
