GitLab CI/CD is GitLab's continuous integration and continuous delivery service. It helps developers automatically run a series of tasks after every commit, such as compiling, testing, packaging and deploying. MinIO is a high-performance object storage service that offers an Amazon S3-compatible API and supports features like distributed deployment, fault tolerance and encryption. This article shows how to use GitLab CI/CD to publish generated files to MinIO, creating a simple yet efficient automated workflow.
Prerequisites
- A GitLab account with a project created or imported
- A MinIO account with a bucket and access keys (Access Key and Secret Key) created or obtained
- A
.gitlab-ci.ymlfile in the project root to define the GitLab CI/CD configuration
How to Configure Gitlab CI/CD
To automatically publish files to MinIO object storage with Gitlab CI/CD, we need to do the following:
- Create a
.gitlab-ci.ymlfile in the project root. This is a declarative file that defines the Gitlab CI/CD pipeline and jobs. - Define a
deploystage in the.gitlab-ci.ymlfile to handle publishing the files. - In the
deploystage, use themccommand-line tool or another MinIO client to upload the files to MinIO object storage. - In the
deploystage, set some environment variables, such as the MinIO address, keys and bucket name. - Enable Gitlab CI/CD in the project settings and add the corresponding environment variables.
Here we assume the project already has a build script that produces a dist/ directory. If not, adjust things to fit your own project.
We also need to add the following three environment variables in the project settings:
MINIO_URL: the MinIO address, e.g. http://localhost:9000MINIO_ACCESS_KEY: the MinIO access keyMINIO_SECRET_KEY: the MinIO access key
These environment variables can be added under project Settings -> CI/CD -> Variables, where you can also choose whether to protect or mask them.
Here is an example .gitlab-ci.yml file:
stages:
- deploy
before_script:
- curl https://dl.min.io/client/mc/release/linux-amd64/mc -o mc # 下载并安装 MinIO 客户端
- chmod +x mc # 赋予可执行权限
master-deploy:
stage: deploy
only:
- master # 只有master更新的时候才执行命令
script: # 执行的命令
- ./mc config host add minio $MINIO_URL $MINIO_ACCESS_KEY $MINIO_SECRET_KEY # 设置 MinIO 别名和认证信息
- ./mc cp dist/ minio/<bucket-name>/<project-name>/ # 将 dist 目录下的所有文件复制到 MinIO 的指定存储桶和目录下
This file defines a deploy stage
before_scriptdownloads the MinIO client and makes it executable.- In the commands that follow, the first one sets the MinIO address and keys.
- The second command copies all files in the
dist/directory produced by the project build into the specified MinIO bucket.
How to Verify the Result
Once we commit code, Gitlab CI/CD is triggered automatically and runs the jobs defined in the .gitlab-ci.yml file. We can check the pipeline status and logs under the project page -> CI/CD -> Pipelines.