What are the steps to set up a continuous deployment pipeline using GitLab CI for a Vue.js project?

12 June 2024

The latest digital trends continue to emphasize the importance of effective project management and seamless deployment processes. As a development team, you are always seeking tools that can automate mundane tasks, reduce the risk of errors, and increase efficiency. In this context, GitLab CI emerges as an excellent tool for continuous integration and deployment. Integrating this with a Vue.js project can position you for streamlined project development and deployment. GitLab provides a unique configuration file, .gitlab-ci.yml, that allows you to define the deployment pipeline, enhancing the overall build process. This article explains the step by step process to set up a continuous deployment pipeline using GitLab CI for a Vue.js project.

Understanding GitLab CI/CD

Before we delve into the steps, let's take a moment to understand GitLab's Continuous Integration/Continuous Deployment (CI/CD) feature. It's a method that encourages developers to incorporate their changes to the main project code base frequently. The feature uses automated pipelines to build, test, and deploy applications, improving the speed, efficiency, and reliability of the development process.

The .gitlab-ci.yml file is an integral part of GitLab CI/CD. It is a YAML file where you can define specific pipelines, jobs, and stages of testing and deployment. In the context of Vue.js, a popular JavaScript framework, GitLab CI/CD offers an efficient way to automate the testing and deployment process.

Setting Up Your GitLab CI/CD Pipeline

Setting up your GitLab CI/CD pipeline for a Vue.js project involves several steps. The first step is to ensure that your project resides in a GitLab repository. Once this is done, you can start configuring your .gitlab-ci.yml file.

Creation and Configuration of .gitlab-ci.yml File

Begin by creating a new .gitlab-ci.yml file at the root of your Vue.js project. This file will include all the instructions that GitLab will follow to build and deploy your application. The instructions are written in stages and jobs.

  • Stages are used to define the stages that tasks can be grouped into. If no stages are defined, the stages are defined as .build, .test, and .deploy.

  • Jobs are the commands that GitLab will execute. Each job must have a unique name and will run independently in a separate Docker container.

Here's a simple example of a .gitlab-ci.yml file:

  - build
  - deploy

  stage: build
  image: node:latest
    - npm install
    - npm run build
      - dist/
    - master

  stage: deploy
  image: alpine:latest
    - echo "Deploying Application"
    - master

Understanding the .gitlab-ci.yml File

In this .gitlab-ci.yml file, there are two stages: build and deploy. The build stage uses the node:latest Docker image to run the npm install and npm run build scripts. The artifacts keyword specifies the directory where the build files will be stored. The only keyword specifies that this stage will only run on the master branch.

The deploy stage is where you would add your deployment commands. For the sake of this example, it simply prints "Deploying Application" to the console.

Configuring the Docker Runner

After the configuration of the .gitlab-ci.yml file, the next step is to set up a Docker runner. GitLab runner is an application that processes your CI jobs and sends the results back to GitLab. It is installed on a machine where you want to run your jobs and can run multiple jobs concurrently.

To install the runner, follow the official GitLab documentation. Once the runner is installed, you can register it to your GitLab instance using the specific runner token available in your GitLab CI/CD settings. After registration, the runner is ready to pick up and execute jobs.

Building and Deploying the Application

At this point, you have set up your GitLab CI/CD pipeline. Now, whenever you push your code to the master branch, the pipeline will automatically trigger. The build stage will install all your npm dependencies and build your Vue.js application. The results of the build, located in the dist/ folder, will be stored as artifacts.

After the build stage completes successfully, the pipeline will move to the deploy stage. This stage will execute your deploy script. You can customize this script to deploy your application to your desired platform.

In summary, integrating GitLab CI in your Vue.js project streamlines your build and deployment processes. It allows for faster and more reliable project development, which is critical in today's fast-paced tech world. Understanding and correctly configuring your .gitlab-ci.yml file is crucial for this process. Make sure to properly set up your Docker runner to execute the jobs in your pipeline. With these in place, you can efficiently automate your project workflows, allowing you to focus on writing code and delivering value.

Integrating Secure SSH Key for Deployment

As you prepare to deploy your Vue.js application, it's essential to ensure secure access to your server. This is where the SSH key comes in handy. Secure Shell (SSH) is a network protocol that allows secure remote login from one computer to another. It provides strong password authentication and public key authentication. The SSH key pair consists of a private key which is kept secret and a public key which is shared.

To generate an SSH key, you can use the ssh-keygen command in your terminal. Once the key pair is generated, you need to add the public key to your server for authentication. The private key should be securely stored and is needed to connect to the server.

# Generate a new SSH key pair
ssh-keygen -t rsa -b 4096 -C "[email protected]"

# Display the public key
cat ~/.ssh/id_rsa.pub

Next, you need to add the private key to your GitLab repository. Go to your GitLab project's Settings > CI/CD > Variables. Click on Add Variable. Enter SSH_PRIVATE_KEY for the Key and paste your private key in the Value field. Make sure the Protect variable and Mask variable options are checked. This ensures that your private key is securely stored and not exposed in the job logs.

When it comes to using the SSH key in your .gitlab-ci.yml file, you can add a before_script section which sets up the SSH agent and adds your private key.

  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d 'r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

With this, your Vue.js application is ready for secure deployment using GitLab CI.

Setting up a continuous deployment pipeline using GitLab CI for a Vue.js project is a comprehensive process that simplifies and automates your build and deployment tasks. From understanding the roles of the .gitlab-ci.yml file and the Docker image to writing a build script and using npm install to handle dependencies, each step plays a vital role.

The process also involves secure handling of your SSH key and Docker images, along with thorough testing of your Vue.js project using gitlab yml. The pipeline allows automated testing and deployment upon every merge request, ensuring that your application is always up to date.

Moreover, the use of access token enhances security while the image node supports the running of your application. The npm build, along with the package json, ensures that all dependencies are correctly installed and that your Vue project runs smoothly.

By correctly setting up a spring boot and file root, you can further streamline your processes. This way, you emphasize not only efficient application development but also security and consistency.

Incorporating GitLab CI in your Vue.js project saves you valuable time and resources while ensuring seamless project management. Remember, the aim is to automate as much as possible, reducing manual intervention and the likelihood of errors. This way, you can focus more on coding, improving, and expanding your application.

Copyright 2024. All Rights Reserved