Implementing CI/CD for a Static HTML/CSS Website with GitHub Actions
Learn how to set up a CI/CD pipeline for a simple HTML/CSS website using GitHub Actions. We’ll explore creating automated linting workflows, tracking errors, and ensuring a seamless deployment process.
Why CI/CD for a Static Website?
CI/CD brings immense benefits to static sites by automating quality checks and ensuring code consistency:
- Enforcing Code Quality: Automatically check code styling and quality using linters.
- Catching Errors Early: Detect common HTML/CSS issues, such as unclosed tags or misformatted CSS.
- Saving Time: Automate repetitive tasks to focus on development.
Setting Up CI/CD with GitHub Actions
Follow these steps to create a linting workflow:
1. Create the Workflow File
In our project directory, create .github/workflows/lint.yml with the following content:
name: Lint HTML and CSS
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install HTMLHint and Stylelint
run: |
npm install -g htmlhint
npm install -g stylelint stylelint-config-standard
- name: Add Stylelint Configuration
run: |
echo '{
"extends": "stylelint-config-standard",
"rules": {
"alpha-value-notation": "percentage",
"color-function-notation": "modern",
"selector-class-pattern": "^[a-z0-9\\-]+$",
"comment-whitespace-inside": "always",
"length-zero-no-unit": true,
"no-duplicate-selectors": true,
"declaration-empty-line-before": "never",
"comment-empty-line-before": "always"
}
}' > .stylelintrc.json
- name: Add HTMLHint Configuration
run: |
echo '{
"tag-pair": true,
"attr-value-double-quotes": true,
"doctype-first": true,
"id-unique": true
}' > .htmlhintrc
- name: Run HTMLHint
run: |
htmlhint "**/*.html" > htmlhint-report.txt || true
- name: Display HTMLHint Report
run: cat htmlhint-report.txt
- name: Run Stylelint
run: |
stylelint "**/*.css" --fix > stylelint-report.txt || true
- name: Display Stylelint Report
run: cat stylelint-report.txt
- name: Commit changes made by auto-fix
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "Stylelint auto-fix"
git push
fi
2. Workflow Details
The lint.yml file sets up a linting workflow that runs on every push or pull request to the main branch. Here’s a breakdown of each step:
- Trigger: Runs on pushes and pull requests targeting the
mainbranch. - Checkout Repository: Pulls the code from our repository for analysis.
- Install Linters: Installs
htmlhintfor HTML andstylelintfor CSS checks. - Run Linters:
htmlhintandstylelintanalyze our HTML and CSS files, listing any errors or warnings. - Commit Auto-Fixes: If
stylelintcan fix any CSS issues automatically, those changes are committed back to the repository. - Upload Reports: Lint reports are uploaded for reference as artifacts.
3. Tracking and Fixing Errors
Each time we push code to the main branch, GitHub Actions runs the linting workflow and outputs results. Here's how to address issues:
- View the results in the Actions tab on our GitHub repository.
- Check the output reports (e.g.,
htmlhint-report.txtorstylelint-report.txt) for errors and warnings. - Locate errors using the file name and line number provided by the linters.
- Fix the issues in our code and push the changes back to the repository to rerun the checks.
Benefits of CI/CD for a Static Website
By implementing CI/CD workflows for a static website, we gain several benefits:
- Quality Assurance: Automated checks ensure consistent code quality across the project.
- Reduced Manual Effort: Linters and automated fixes save time and reduce human errors.
- Collaboration Ready: Enforces a uniform coding standard for all contributors.
Conclusion
Setting up a CI/CD pipeline with GitHub Actions for a static HTML/CSS website is a powerful way to automate quality checks and streamline development. By integrating tools like htmlhint and stylelint, we can ensure that our code remains clean, consistent, and error-free with every update.
Additional Resources
Check out these resources: