编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

项目部署后验证是否部署成功的方法

wxchong 2024-08-03 02:34:25 开源技术 33 ℃ 0 评论

我们项目发布用了Jenkins 。前端 和java 都是。经常遇到发布workflow 成功了,线上却还是旧的版本的情况。为了避免这种薛定谔的猫式 发布。产品提了一个需求,要求前端和后盾发布后要能看到当前的版本号。

版本号 ,这种可不是后台配置的0.0.1 这种自定义版本。为了和开发衔接。决定就用git 最后一次提交来实现。

百度了一下,查看最近一次提交信息的语句是

git log -n1 --oneline


本地执行了 类似图上的效果。

前端

前端我的想法是,直接在console 里输出这个信息。于是想到了每次前端发布 其实是npm build 出一个index.html 和 一堆压缩后的js。

那我只要替换index.html 输出一段调试信息不就ok了。

然后就去学习了半天的sed 命令。

在我的不断尝试下,终于替换成功了。


其中几点注意的, "s|" 没有用 / 的原因是如果用了这个做分隔符 / 就得转义。

最后是要替换的文件名,不同项目入口文件不一样,前面都一样, 第一行是一个发布 shell 脚本。

这样十几家部署 我也不需要挨个去改 pub.sh 只需要更新Jenkins publish over ssh的配置就行。

后端

这可要了我的老命了。本来 前端的 源码 和 build 过的成品 是在一台服务器的。结果 java的不在一台服务器 是从Jenkins 打包好jar 复制过去再执行 pub.sh 完成发布。

本来也想着将git log 生成一个文件传输过去,再在服务器代码里读文件的。奈何实力有限。没搞懂那个插件的用法。

后来问了总监,总监说有插件。就找到了git-commit-id-plugin

java 同一个插件也是各种版本,网上教程五花八门。

因为项目环境是jdk1.8 所以选了4.9.9 版本 能支持的最新版本。


一开始就pom里 配插件 抄了好多教程的配置, 打包啥也没生成。后来,还是去官方文档的配置拿过来可以生成。然后又由于要读取后暴露出来。ini 的格式不方便就加了点配置改成json的。


<plugin>
  <groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>4.9.9</version>
<executions>
  <execution>
  <id>get-the-git-infos</id>
<goals>
  <goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
  <format>json</format>
<dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
  <includeOnlyProperties>
  <includeOnlyProperty>^git.build.(time|version)lt;/includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)lt;/includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>

测试了一下可以生成。按照文档说 git.commit.id.abbrev 可以在pom 里用。

结果试了半天 设置系统变量、环境变量 都没有用。

最后找到了一个 读取jar 内包的文件 显示的。


package com.paizhi.open.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.paizhi.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/version")
@Slf4j
public class VersionController extends BaseController  {

    @GetMapping("/info")
    public Map<String, Object> getVersionInfo() {
        return readGitProperties();
    }

    private Map<String, Object> readGitProperties() {
        InputStream inputStream = null;
        try {
            ClassLoader classLoader = getClass().getClassLoader();
            inputStream = classLoader.getResourceAsStream("git.properties");
            // 读取文件内容,自定义一个方法实现即可
            String versionJson = getStringFromStream(inputStream);
            JSONObject jsonObject = JSON.parseObject(versionJson);
            Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
            if (CollectionUtils.isNotEmpty(entrySet)) {
                return entrySet.stream()
                    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (o, n) -> n));
            }
        } catch (Exception e) {
            log.error("get git version info fail", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                log.error("close inputstream fail", e);
            }
        }
        return new HashMap<>();
    }

    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */

    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static String getStringFromStream(InputStream inputStream) {
        String ret = null;
        try {
            ret = new String(readInputStream(inputStream), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
}

这样接口就写好了,测试了 可显示,之前是ini 格式的时候 重新生成一定要先clean 不然会报错 不是合法的json。


至此,产品经理的需求就完成了,在bug里 贴上其他工程api 的域名, 以后发布了 让她自己测试去。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表