Minimal .gitlab-ci.yml file for deploying a plain HTML website

2 min read 05-10-2024
Minimal .gitlab-ci.yml file for deploying a plain HTML website


Streamlining Your Website Deployment: A Minimal .gitlab-ci.yml for Plain HTML

Deploying a simple static HTML website shouldn't be a complicated affair. You want a straightforward process that gets your website online quickly and effortlessly. This is where GitLab CI/CD comes in, and with a minimal .gitlab-ci.yml file, you can automate the entire deployment workflow.

The Scenario: A Plain HTML Website

Let's say you have a basic HTML website stored in a GitLab repository. It's just a few files, no dynamic content or backend logic. You want to push your changes to a web server automatically whenever you commit to the repository.

Here's a basic example of a .gitlab-ci.yml file that can achieve this:

image: alpine:latest

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - echo "Deploying website..."
    - rsync -avz public/ /var/www/html/ 
  only:
    - master

Breaking Down the Code

  • image: alpine:latest: This specifies the Docker image used for running your CI/CD pipeline. alpine:latest is a lightweight Linux distribution, ideal for simple deployments.
  • stages: - deploy: Defines a single stage in the pipeline named "deploy". This helps organize the build process.
  • deploy:: This section defines the "deploy" job, which runs the deployment script.
  • script:: This is where the deployment commands go:
    • echo "Deploying website...": A simple message to show that the deployment is in progress.
    • rsync -avz public/ /var/www/html/: This is the core command. rsync is a powerful tool for synchronizing files between directories. In this case, it copies all files and directories from the public/ directory in your GitLab repository to /var/www/html/ on the server. The flags -avz ensure that the files are transferred with attributes preserved, verbosely, and compressed.
  • only: - master: This ensures that the deploy job only runs when you push to the master branch. You can adjust this to any branch you desire.

Making it Even Simpler

The above .gitlab-ci.yml is a starting point. You can customize it to match your specific needs. For example, you could:

  • Use a different Docker image: If your deployment requires specific software or libraries, choose a Docker image that already includes them.
  • Add a build stage: If you have a more complex build process, you can introduce a build stage before deploy.
  • Integrate with other tools: GitLab CI/CD integrates with various tools and services, allowing you to add features like automatic testing or deployment to cloud providers.

Benefits of Minimal .gitlab-ci.yml

  • Automation: Automating deployment frees you from manual tasks, saving time and reducing errors.
  • Consistency: Every deployment uses the same script, ensuring consistent results and predictable behavior.
  • Efficiency: The lean script focuses on the core task of deployment, minimizing overhead and speeding up the process.
  • Scalability: As your project grows, you can expand the .gitlab-ci.yml to accommodate more complex workflows.

Resources for Further Exploration

By using a minimal .gitlab-ci.yml file, you can easily automate the deployment of your simple HTML website, ensuring a smooth and efficient workflow. This allows you to focus on creating amazing content instead of wrestling with complex deployment processes.