通过CI ci设计案例


即使是最小的侧项目也值得它的CI/CD管道

通过CI ci设计案例


前言
使用当今的工具 , 建立一个简单的CI/CD管道并不困难 。即使是做一个小项目 , 这样做也是学习很多东西的好方法 。Docker、GitLab和Portainer是用于此设置的一些很棒的组件 。
一个Demo小项目这里我们以https://sophia.events/为案例 , 我们保证页面的event为最新的 。页面的events在这里[1]
免责声明:这是一个简单的项目 , 但是项目的复杂性在这里并不重要 。我们将详细介绍的CI/CD管道的组件可以以几乎相同的方式用于更复杂的项目 。不过 , 它们非常适合微服务 。
废话不说 , 直接上代码
{"events": [{"title": "All Day DevOps 2018","desc": "We're back with 100, 30-minute practitioner-led sessions and live Q&A on Slack. Our 5 tracks include CI/CD, Cloud-Native Infrastructure, DevSecOps, Cultural Transformations, and Site Reliability Engineering. 24 hours. 112 speakers. Free online.","date": "17 octobre 2018, online event","ts": "20181017T000000","link": "https://www.alldaydevops.com/","sponsors": [{"name": "all-day-devops"}]},{"title": "Création d'une Blockchain d'entreprise (lab) & introduction aux smart contracts","desc": "Venez avec votre laptop ! Nous vous proposons de nous rejoindre pour réaliser la création d'un premier prototype d'une Blockchain d'entreprise (Lab) et avoir une introduction aux smart contracts.","ts": "20181004T181500","date": "4 octobre à 18h15 au CEEI","link": "https://www.meetup.com/fr-FR/IBM-Cloud-Cote-d-Azur-Meetup/events/254472667/","sponsors": [{"name": "ibm"}]},…]}
将mustache[2]模板应用于此文件 , 以生成最终的web页面 。
基于Docker的多层构建生成web 页面之后 , 将它们复制到nginx image中——部署在目标机器上的image 。
然后构建完成了两个部分 , 由于多阶段构建:
· 资产的生成
· 创建包含资产的最终映像
这是Dockerfile用于构建:
# Generate the assetsFROM node:8.12.0-alpine AS buildCOPY . /buildWORKDIR /buildRUN npm iRUN node clean.jsRUN ./node_modules/mustache/bin/mustache events.json index.mustache > index.html# Build the final image used to serve them FROM nginx:1.14.0COPY --from=build /build/*.html /usr/share/nginx/html/COPY events.json /usr/share/nginx/html/COPY css /usr/share/nginx/html/cssCOPY js /usr/share/nginx/html/jsCOPY img /usr/share/nginx/html/img
本地测试测试目的:只需要clone repo 然后在运行test.sh 。test.sh 会创建一个image然后运行 。
$ git clone :lucj/sophia.events.git$ cd sophia.events$ ./test.shSending build context to Docker daemon 2.588MBStep 1/12 : FROM node:8.12.0-alpine AS build ---> df48b68da02aStep 2/12 : COPY . /build ---> f4005274aadfStep 3/12 : WORKDIR /build ---> Running in 5222c3b6cf12Removing intermediate container 5222c3b6cf12 ---> 81947306e4afStep 4/12 : RUN npm i ---> Running in de4e6182036bnpm notice created a lockfile as package-lock.json. You should commit this file.npm WARN www@1.0.0 No repository field.added 2 packages from 3 contributors and audited 2 packages in 1.675sfound 0 vulnerabilitiesRemoving intermediate container de4e6182036b ---> d0eb4627e01fStep 5/12 : RUN node clean.js ---> Running in f4d3c4745901Removing intermediate container f4d3c4745901 ---> 602987ce7162Step 6/12 : RUN ./node_modules/mustache/bin/mustache events.json index.mustache > index.html ---> Running in 05b5ebd73b89Removing intermediate container 05b5ebd73b89 ---> d982ff9cc61cStep 7/12 : FROM nginx:1.14.0 ---> 86898218889aStep 8/12 : COPY --from=build /build/*.html /usr/share/nginx/html/ ---> Using cache ---> e0c25127223fStep 9/12 : COPY events.json /usr/share/nginx/html/ ---> Using cache ---> 64e8a1c5e79dStep 10/12 : COPY css /usr/share/nginx/html/css ---> Using cache ---> e524c31b64c2Step 11/12 : COPY js /usr/share/nginx/html/js ---> Using cache ---> 1ef9dece9bb4Step 12/12 : COPY img /usr/share/nginx/html/img ---> e50bf7836d2fSuccessfully built e50bf7836d2fSuccessfully tagged registry.gitlab.com/lucj/sophia.events:latest=> web site available on http://localhost:32768

推荐阅读