From 7162e7c7cbd8705a4b4da3f68b5d378680646e03 Mon Sep 17 00:00:00 2001 From: Charlie Qin Date: Thu, 13 Aug 2020 10:51:55 -0700 Subject: [PATCH 1/2] Added deploy time * Add deploy time using moment-timezone.js * Bump up version number --- README.md | 8 ++++++-- index.js | 36 +++++++++++++++++++++++++++++++----- package.json | 5 ++++- yarn.lock | 12 ++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index aa187fc..446aef3 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,21 @@ This plugin for serverless will improve create a file called `git_version.json` ``` { - gitVersion: "STUFF" + gitVersion: "STUFF", + deployTime: "DEPLOY_TIME" } ``` where `STUFF` is the output of `git describe --tags --dirty`. That way you can `require()` the JSON file in your code to get the git version this deployment was from at runtime. +`DEPLOY_TIME` is the output of Moment JS now() with a time zone that can be specified. ## To use this plugin - `yarn add @hughescr/serverless-plugin-git-version-json --dev` or `npm install --save-dev @hughescr/serverless-plugin-git-version-json` - Add `serverless-plugin-git-version-json` to the `plugins` section of your `serverless.yml` - - Add `versionJSONFile: git_version.json` to the `custom` section of your `serverless.yml` + - Add `versionJSONFile: git_version.json` to the `custom` section of your `serverless.yml`. Defaults to git_version.json if not specified. + - Add `deployTimeZone: {IANA_TIMEZONE_NAME}` to the `custom` section of your `serverless.yml`. A valid IANA time zone must be given. Otherwise, default to UTC. https://en.wikipedia.org/wiki/List_of_tz_database_time_zones eg: @@ -30,6 +33,7 @@ package: custom: versionJSONFile: git_version.json + deployTimeZone: America/Los_Angeles plugins: - serverless-plugin-git-version-json diff --git a/index.js b/index.js index c94c124..87509ef 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,30 @@ const fs = require('fs'); const path = require('path'); const spawnSync = require('child_process').spawnSync; +const moment = require('moment-timezone'); class GitVersionOnDeploy { + get options() { + const options = Object.assign( + { + deployTimeZone: 'utc' + }, this.serverless.service.custom || + {} + ); + + const time_zone = moment.tz.zone(options.deployTimeZone); + if(time_zone === null) { + this.serverless.cli.log( + 'WARNING: Bad time zone, falling back to UTC. You must use IANA timezone names.' + ); + options.deployTimeZone = 'utc'; + } else { + this.serverless.cli.log(`Using time zone: ${options.deployTimeZone}`); + } + + return options; + } + constructor(serverless) { this.serverless = serverless; this.path = serverless.config.servicePath; @@ -17,11 +39,11 @@ class GitVersionOnDeploy { this.filePath = path.join(this.path, this.versionJSON); this.hooks = { - 'offline:start:init': this.writeVersionFile.bind(this), - 'before:deploy:function:deploy': this.writeVersionFile.bind(this), - 'after:deploy:function:deploy': this.deleteVersionFile.bind(this), + 'offline:start:init': this.writeVersionFile.bind(this), + 'before:deploy:function:deploy': this.writeVersionFile.bind(this), + 'after:deploy:function:deploy': this.deleteVersionFile.bind(this), 'before:deploy:createDeploymentArtifacts': this.writeVersionFile.bind(this), - 'after:deploy:createDeploymentArtifacts': this.deleteVersionFile.bind(this), + 'after:deploy:createDeploymentArtifacts': this.deleteVersionFile.bind(this), }; } @@ -34,10 +56,14 @@ class GitVersionOnDeploy { return; } const git_id = gitResults.stdout.trim(); - versionFileContents = `{ "gitVersion": "${git_id}" }`; + + const deploy_time = moment().tz(this.options.deployTimeZone).format('YYYY-MM-DDTHH:mm:ss.SSSZZ'); + + versionFileContents = `{ "gitVersion": "${git_id}", "deployTime": "${deploy_time}"}`; fs.writeFileSync(this.filePath, versionFileContents); this.serverless.cli.log(`Tagged with git version ${git_id}`); + this.serverless.cli.log(`Deploy time ${deploy_time}`); } deleteVersionFile() { diff --git a/package.json b/package.json index da3f307..04194d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hughescr/serverless-plugin-git-version-json", - "version": "1.0.2", + "version": "1.0.3", "description": "A serverless plugin which will temporarily create a file git_version.json containing the output of `git describe --tags --dirty` and include it in the package", "main": "index.js", "repository": { @@ -19,5 +19,8 @@ "devDependencies": { "@hughescr/eslint-config-default": "^2.5.0", "eslint": "^7.6.0" + }, + "dependencies": { + "moment-timezone": "^0.5.31" } } diff --git a/yarn.lock b/yarn.lock index fa644e7..4b57410 100644 --- a/yarn.lock +++ b/yarn.lock @@ -538,6 +538,18 @@ mkdirp@^0.5.1: dependencies: minimist "^1.2.5" +moment-timezone@^0.5.31: + version "0.5.31" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.31.tgz#9c40d8c5026f0c7ab46eda3d63e49c155148de05" + integrity sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA== + dependencies: + moment ">= 2.9.0" + +"moment@>= 2.9.0": + version "2.29.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3" + integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ== + ms@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" From 7b9eb50c6f10e8af3519359fc5b54fae5e573b75 Mon Sep 17 00:00:00 2001 From: Charlie Qin Date: Wed, 14 Oct 2020 11:24:04 -0700 Subject: [PATCH 2/2] Convert versionJSONFile to options * Convert versionJSONfile to options --- index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 87509ef..e654b2a 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,8 @@ class GitVersionOnDeploy { get options() { const options = Object.assign( { - deployTimeZone: 'utc' + deployTimeZone: 'utc', + versionJSONFile: 'git_version.json' }, this.serverless.service.custom || {} ); @@ -20,8 +21,6 @@ class GitVersionOnDeploy { 'WARNING: Bad time zone, falling back to UTC. You must use IANA timezone names.' ); options.deployTimeZone = 'utc'; - } else { - this.serverless.cli.log(`Using time zone: ${options.deployTimeZone}`); } return options; @@ -30,13 +29,7 @@ class GitVersionOnDeploy { constructor(serverless) { this.serverless = serverless; this.path = serverless.config.servicePath; - const custom = serverless.service.custom || {}; - this.versionJSON = custom.versionJSONFile; - if(!this.versionJSON) { - this.serverless.cli.log('Path for git_version.json not specified. Using "git_version.json".'); - this.versionJSON = 'git_version.json'; - } - this.filePath = path.join(this.path, this.versionJSON); + this.filePath = path.join(this.path, this.options.versionJSONFile); this.hooks = { 'offline:start:init': this.writeVersionFile.bind(this), @@ -48,7 +41,7 @@ class GitVersionOnDeploy { } writeVersionFile() { - let versionFileContents = '{ "gitVersion": "'; + let versionFileContents = '{ "gitVersion": "}'; const gitResults = spawnSync('git', ['describe', '--tags', '--dirty'], { cwd: this.path, encoding: 'utf8' }); if(gitResults.status != 0) { this.serverless.cli.log('Error while running "git describe --tags --dirty":'); @@ -57,6 +50,9 @@ class GitVersionOnDeploy { } const git_id = gitResults.stdout.trim(); + this.serverless.cli.log(`Writing out to: ${this.options.versionJSONFile}`); + this.serverless.cli.log(`Using time zone: ${this.options.deployTimeZone}`); + const deploy_time = moment().tz(this.options.deployTimeZone).format('YYYY-MM-DDTHH:mm:ss.SSSZZ'); versionFileContents = `{ "gitVersion": "${git_id}", "deployTime": "${deploy_time}"}`; @@ -68,7 +64,7 @@ class GitVersionOnDeploy { deleteVersionFile() { fs.unlinkSync(this.filePath); - this.serverless.cli.log(`Deleted ${this.versionJSON}`); + this.serverless.cli.log(`Deleted ${this.options.versionJSONFile}`); } }