Automatically Deploying Frontend Projects with GitLab CI/CD
Preface I previously wrote a series of posts on GitLab installation and usage as well as GitLab CI/CD configuration and usage, and I've already set up continuous integration for the AspNetCore backend projects. After tasting the benefits, I now want to add automated deployment for frontend projects as well, so after some tinkering, this article came about~ Previous articles: GitLab Installation, Configuration, and Usage Continuous Integration Guide: GitLab CI/CD Tool Configuration and Usage Similar to how I handled the AspNetCore projects before, I still use docke
目录7 个章节
Preface
I previously wrote a series of posts on GitLab installation and usage as well as GitLab CI/CD configuration and usage, and I've already set up continuous integration for the AspNetCore backend projects. After tasting the benefits, I now want to add automated deployment for frontend projects as well, so after some tinkering, this article came about~
Previous articles:
- GitLab Installation, Configuration, and Usage
- Continuous Integration Guide: GitLab CI/CD Tool Configuration and Usage
Similar to how I handled the AspNetCore projects before, I still use Docker for deployment, independent of the server environment—just convenient and elegant~
Docker Configuration
This was my first time using Docker to build a frontend project, and I ran into many pitfalls, but I eventually got it working by referring to my previous DjangoStarter project and the AspNetCore Dockerfile~
The idea is two containers: one Node container to compile the project, and another Nginx to serve static files. In other words, after I build the project, I provide the web service directly inside the container, instead of copying it out and putting it into a shared Nginx on the server.
Then I set up forwarding in the server's Nginx, so the experience is the same as before. (Or you can use it directly without forwarding, depending on the actual situation.)
Using Nginx in Docker is better—this shows in aspects such as configuration complexity, permission management, security/audit checks, and the like. I won't elaborate; just use it and you're done~
Dockerfile
FROM node:lts-alpine as build
RUN mkdir /code
WORKDIR /code
ADD package.json /code
RUN npm install --registry=http://registry.npmmirror.com
ADD . /code
RUN npm run build
FROM nginx:alpine as final
LABEL maintainer="DealiAxy <feedback@deali.cn>"
COPY --from=build /code/dist/ /usr/share/nginx/html/
EXPOSE 80
Note that the COPY inside the Nginx container must have the --from parameter set, otherwise it won't be able to read the files built in the node container~
Once done, this Dockerfile is fixed, and you won't need to modify it in the future unless there are special circumstances~
docker-compose.yml
Not much to say. Actually this compose file can be omitted; I just don't want to type docker commands and am being lazy~
version: '3.4'
services:
web:
restart: always
build:
context: .
ports:
- "8080:80"
GitLab CI Configuration
Nothing special
variables:
GIT_STRATEGY: none
stages:
- deploy
deploy_job:
stage: deploy
only:
- master
script:
- cd /home/gitlab-runner/projects/0_frontend/dashboard-front
- rm -rf dashboard-front
- git clone http://127.0.0.1:8080/deali/dashboard-front.git
- cd dashboard-front
- docker-compose up --build -d
Summary
All set. The first time I did this I still ran into many pitfalls, but luckily I didn't give up and then solved the problems step by step.
After that, for each project you only need to modify the docker-compose.yml file to specify the exposed port and the .gitlab-ci.yml file to specify the folder to build. Much more convenient than the original manual build and publish.
References
Note: The Dockerfile in this reference article is wrong; please follow this article.
Setting aside the errors, there are still some parts of this reference article that can be useful—please verify them yourself~
- Deploying frontend applications with Docker: https://zhuanlan.zhihu.com/p/102853707
_EOF_
Automatically Deploying Frontend Projects with GitLab CI/CD
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Coolify: Vercel 的开源版私有化部署平替版
又一个Vercel平替,开源自托管PaaS。自动构建部署是像模像样,但界面简陋得让人想翻白眼。网络要求还挑剔,国内服务器就别凑热闹了。
2026-02-05 · DevOps · 约 1 分钟
GitLab的安装、配置、使用
前言 上周去参与“中国数字经济创新发展大会”了,然后又忙新项目的事情,博客又有一段时间没有更新,今天周一事情比较少,立刻开始写文,最近有挺多值得记录的东西~ 进入正文,最近我们搭了个Gitlab,并且把正在开发的项目代码都迁移到自建的Gitlab上,整个过程还是比较流畅的~~(就是有点费内存~~ PS:立即下单给这台服务器买了四条16G内存组四通道~ 在此之前,我们团队一直使用Gitee企业版作为
2022-07-21 · DevOps · 约 1 分钟
ExceptionLess的安装、配置、使用
前言 Exceptionless 是一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,ASP.NET Core,Web API,Web Forms,WPF,Console,ASP.NET MVC 等技术开发的应用程序中,并且提供了REST接口可以应用在 Javascript,Node.js 中。(基本就是.Net技术栈的一套东西) 项目地址:https://github.com/ex
2022-07-21 · DevOps · 约 1 分钟
关于DevOps
前天写了Gitlab安装配置的文章,把之前在Gitee企业版上的大部分项目迁移到本地Gitlab管理,用着还不错~ 随着项目的规模逐渐变大以及手头上开发维护的项目逐渐变多,DevOps的重要性也逐渐凸显,目前我们开发、测试和部署的流程还是相对原始,意识到这点之后我开了一个新的博客分类(DevOps),边学边记录~ https://zh.m.wikipedia.org/zh-sg/DevOps
2022-07-21 · DevOps · 约 1 分钟
讨论
共 0 条加载评论中…