Deploying Hugo onto staging website from feature branch

2 min read 05-10-2024
Deploying Hugo onto staging website from feature branch


Deploying Hugo onto a Staging Website from a Feature Branch

Problem: You're working on a feature branch in your Hugo project, and you want to test it on a staging website before merging it into the main branch.

Rephrased: You're making changes to your website (built with Hugo) and want to see them live on a test environment before pushing them to the production website.

Scenario & Original Code

Let's imagine you're working on a new blog post feature in your Hugo website. You've created a branch named feature/blog-post to work on this feature. Now you want to deploy this branch to a staging website for testing.

Traditional Deployment (Using git push directly to staging):

git checkout feature/blog-post
git push origin feature/blog-post

The problem with this approach:

  • Overwriting Staging: This directly pushes the feature branch to the staging branch, overwriting any existing code on the staging environment.
  • Deployment Issues: If the feature branch has dependencies or conflicts with the current staging branch, it can cause the staging website to break.

A Better Approach: Using Pull Requests and CI/CD Pipelines

A more reliable and structured approach involves using pull requests and CI/CD pipelines:

  1. Create a Pull Request: After completing your feature, create a pull request from your feature/blog-post branch to your staging branch. This allows for code review and ensures your feature is ready for deployment.
  2. Configure CI/CD Pipeline: Set up a continuous integration and continuous deployment (CI/CD) pipeline for your staging environment. This pipeline should automatically build and deploy your website whenever a new pull request is merged into the staging branch.

Example CI/CD pipeline using GitHub Actions:

name: Deploy to Staging

on:
  push:
    branches:
      - staging

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build Website
      run: hugo --minify
    - name: Deploy to Staging
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: public
        publish_branch: staging

Benefits of this Approach:

  • Automated Deployment: CI/CD pipelines automate the deployment process, reducing manual errors and ensuring consistency.
  • Collaboration: Pull requests facilitate code review and collaboration, making sure the feature is well-tested and integrated before deployment.
  • Version Control: Changes are tracked and versioned, allowing for rollbacks if necessary.

Additional Considerations

  • Environment Variables: Consider using environment variables for sensitive information like API keys or database credentials.
  • Caching: Implement caching mechanisms for your website to improve performance.
  • Testing: Thoroughly test your feature on the staging environment before merging into the main branch.

Conclusion

Deploying your Hugo project to a staging website from a feature branch requires a structured approach. Using pull requests and CI/CD pipelines allows for safe, automated, and collaborative deployment, ensuring that your features are thoroughly tested before being pushed to production. This approach also ensures a smooth development workflow and minimizes potential deployment issues.