尽管图像的SHA不同,但Git diff没有输出

时间:2018-08-14 09:20:35

标签: node.js git deployment

我正在尝试创建一个部署脚本,该脚本将让我知道我是否在我的master或development分支上部署了我的项目的最新映像。

我正在尝试使用git.diff将部署的映像的SHA1哈希与我的本地存储库进行比较,尽管哈希值明显不同,但是git.diff没有提供任何输出。我不明白这是怎么回事,因为如果SHA1不同,那么肯定要从git.diff显示更改吗?

这是我到目前为止编写的代码:

#!/usr/bin/node

// get an exec function from node we can use to run shell commands
const exec = require('util').promisify(require('child_process').exec);

// check the user supplied the folder name of the repo as an arg
if (!process.argv[2]) {
  console.error('argument missing');
  process.exit(1);
}

// initialize our git client using the repo path arg
const git = require('simple-git/promise')("../../" + process.argv[2]);

var projectNameArray = process.argv[2].split(".");
const projectName = projectNameArray[0] + "-" + projectNameArray[1] + "-" + projectNameArray[2] 
console.log('\x1b[36m%s\x1b[0m', 'Your project name is:', projectName);


// use an IIAFE for async/await
(async () => {  


  // run git rev-parse development and
  var devSha1 = await git.revparse(['development']);
  console.log('\x1b[36m%s\x1b[0m', 'devSha1: ', devSha1);
  devSha1 = devSha1.replace(/(\n)/gm,"");

  // run git rev-parse master
  var masterSha1 = await git.revparse(['master']);
  console.log('\x1b[36m%s\x1b[0m', 'masterSha1: ', masterSha1);
  masterSha1 = masterSha1.replace(/(\n)/gm,"");

  // use kubectl to export pods to JSON and then parse it
  const { stdout, stderr } = await exec(`kubectl get deployment ${projectName} -o json`);  
  const pods = JSON.parse(stdout);
  const imageName = pods.spec.template.spec.containers[0].image;

  //get deployed image has
  const commitHashArray = imageName.split('development-' || 'master-');
  console.log('\x1b[36m%s\x1b[0m', 'Deployed image: ', commitHashArray[1]);

  var diffArray = new Array(devSha1, commitHashArray[1])
  //logic to tell if latest is deployed of if behind
  if (commitHashArray[1] == devSha1){
    console.log('\x1b[32m%s\x1b[0m', 'You have the latest image deployed');
  } else {
    console.log('\x1b[31m%s\x1b[0m', 'You don\'t have the latest image deployed');

    await git.diff(diffArray);
  }

})().then(() => console.log('\x1b[32m%s\x1b[0m', 'Ok')).catch((e) => console.error(e));

这给了我以下控制台输出:

Your project name is: xxx-xxx-xxx
devSha1:  6a7ee89dbefc4508b03d863e5c1f5dd9dce579b4
masterSha1:  4529244ba95e1b043b691c5ef1dc484c7d67dbe2
Deployed image:  446c4ba124f7a12c8a4c91ca8eedde4c3c8652fd
You don't have the latest image deployed
Ok

我不确定我是否从根本上误解了git.diff的工作方式,或者是否还有其他问题在起作用。图像显然不匹配,所以如果有人可以解释为什么此功能没有输出,我将很高兴。

谢谢! :)

0 个答案:

没有答案