在Dockerfile中获取FROM映像的SHA

时间:2019-07-25 19:55:31

标签: docker docker-compose dockerfile

在我的容器映像中,我想访问将要构建映像的映像的SHA。假设地,如果我想将其放入文件中,我可能会做类似的事情:

function myFunction1() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var firstSheet = spreadsheet.getSheetByName('Template');
  var theSheet = spreadsheet.getSheetByName('Copy of Template');
  //check for the sheet with the same name
  if(theSheet != null){
    spreadsheet.setActiveSheet(theSheet);
  } else {
    spreadsheet.setActiveSheet(firstSheet);
    spreadsheet.duplicateActiveSheet();
    theSheet = spreadsheet.getSheetByName('Copy of Template');
    spreadsheet.setActiveSheet(theSheet);
  }

  var fechaHoy = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
  theSheet.getRange('G5').setValue(fechaHoy);
  theSheet.getRange('G5').setHorizontalAlignment("right");
}

function myFunction2() {
  var month = Utilities.formatDate(new Date(), "GMT+1", "MM");
  var year = Utilities.formatDate(new Date(), "GMT+1", "yyyy");
  var fileName = month + "_" + year;
  var files = DriveApp.getFilesByName(fileName);
  var i = 0;

  //check for file with the same name
  while (files.hasNext()) {
    i = i + 1;
    var file = files.next();
  }

  if (i == 0){
    //if file do not exist create in current folder
    var folders=DriveApp.getFoldersByName("Taxi");
    while (folders.hasNext()) {
      var folder = folders.next();
      var file = SpreadsheetApp.create(fileName);
      var copyFile = DriveApp.getFileById(file.getId());
      folder.addFile(copyFile);
      DriveApp.getRootFolder().removeFile(copyFile);
    }
    //then open and make operations
    ops(fileName);
  } else {
    //if file exist open & make operations
    ops(fileName);
  }
}

function ops(fileName){
  var files = DriveApp.getFilesByName(fileName);
  var spreadFrom = SpreadsheetApp.getActiveSpreadsheet();
  var sheetFrom = spreadFrom.getSheetByName('Copy of Template');
  while (files.hasNext()) {
    var spreadTo = SpreadsheetApp.open(files.next());

    //copy the copy_of_template to new sheet
    sheetFrom.copyTo(spreadTo);
    var sheetTo = spreadTo.getSheetByName("Copy of Copy of Template");

    //delete Sheet1 if exist
    var sheet1 = spreadTo.getSheetByName("Sheet1");
    if(sheet1 != null){
      spreadTo.deleteSheet(sheet1);
    }

    //delete Hoja1 if exist
    var sheet11 = spreadTo.getSheetByName("Hoja1");
    if(sheet11 != null){
      spreadTo.deleteSheet(sheet11);
    }

    //change formulas for values
    //invoice number
    var cellFrom = sheetFrom.getRange("G4");
    var cellTo = sheetTo.getRange("G4");
    var x = cellFrom.getValue();
    cellTo.setValue(x);
    //cif cliente
    cellFrom = sheetFrom.getRange("F9");
    cellTo = sheetTo.getRange("F9");
    x = cellFrom.getValue();
    cellTo.setValue(x);
    //direccion cliente
    cellFrom = sheetFrom.getRange("F10");
    cellTo = sheetTo.getRange("F10");
    x = cellFrom.getValue();
    cellTo.setValue(x);
    //cp y poblacion cliente
    cellFrom = sheetFrom.getRange("F11");
    cellTo = sheetTo.getRange("F11");
    x = cellFrom.getValue();
    cellTo.setValue(x);
    //read IVA from Datos
    var sheetDatos = spreadFrom.getSheetByName('Datos');
    cellFrom = sheetDatos.getRange("B2");
    x = cellFrom.getValue();
    var iva = x;
    //change formulas inserting iva instead of old sheet reference
    for(var i = 19; i < 44; i++){
      cellTo = sheetTo.getRange('H' + i);
      var newF = '=if(' + iva+ '*G' + i + '>0, ' + iva + '*G' + i + ', "")';
      cellTo.setFormula(newF);
    }

    //change name of the copied sheet
    var cell1 = sheetFrom.getRange("G4");
    var y = cell1.getValue();
    y = Utilities.formatString('%06d', y);
    sheetTo.setName(y);


    //delete copy_of_template
    spreadFrom.deleteSheet(sheetFrom);

    //increment invoice number from number sheet
    var sheetNumber = spreadFrom.getSheetByName('Numero');
    var cell2 = sheetNumber.getRange("B1");
    var z = cell2.getValue();
    z = z + 1;
    cell2.setValue(z);

    //message ok & finish
    var theSheet = spreadFrom.getSheetByName('Template');
    spreadFrom.setActiveSheet(theSheet);
    Browser.msgBox('Factura guardada con exito. La puedes ver en la carpeta Facturas, el fichero con el nombre de: ' + fileName + ', la hoja numero: ' + y + '.');
  }
}


// Add a custom menu to the active spreadsheet, including a separator and a sub-menu.
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createMenu('FACTURA')
      .addItem('Paso 1 - Crear', 'myFunction1')
      .addSeparator()
      .addItem('Paso 2 - Guardar', 'myFunction2')
      .addToUi();
}

其中变量FROM ubuntu:16.04 RUN apt-get update && apt-get install -y \ build-essential \ build-essential RUN echo ${FROM_SHA} > /etc/source-sha.txt 具有FROM_SHA的SHA的值。是否可以在Dockerfile中获取此值?

我的最终目标是能够检测容器图像是否基于ubuntu:16.04中使用的标记的旧版本。

两个警告:

  1. 该图像是使用FROM构建的,因此类似docker-compose之类的内容不适用于此用例。
  2. 当前docker build --build-arg FROM_SHA=$(some bash craziness)才有效。我的团队中的开发人员希望继续执行此操作,因此不需要解决方案,例如需要脚本来更新docker-compose up文件中的变量值,而该脚本需要手动和/或定期运行。

0 个答案:

没有答案