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..e654b2a 100644 --- a/index.js +++ b/index.js @@ -3,30 +3,45 @@ 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', + versionJSONFile: 'git_version.json' + }, 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'; + } + + return options; + } + 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), - '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), }; } 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":'); @@ -34,15 +49,22 @@ class GitVersionOnDeploy { return; } const git_id = gitResults.stdout.trim(); - versionFileContents = `{ "gitVersion": "${git_id}" }`; + + 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}"}`; fs.writeFileSync(this.filePath, versionFileContents); this.serverless.cli.log(`Tagged with git version ${git_id}`); + this.serverless.cli.log(`Deploy time ${deploy_time}`); } deleteVersionFile() { fs.unlinkSync(this.filePath); - this.serverless.cli.log(`Deleted ${this.versionJSON}`); + this.serverless.cli.log(`Deleted ${this.options.versionJSONFile}`); } } 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"