Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -30,6 +33,7 @@ package:

custom:
versionJSONFile: git_version.json
deployTimeZone: America/Los_Angeles

plugins:
- serverless-plugin-git-version-json
Expand Down
50 changes: 36 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,68 @@
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":');
this.serverless.cli.log(gitResults.stderr);
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}`);
}
}

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -19,5 +19,8 @@
"devDependencies": {
"@hughescr/eslint-config-default": "^2.5.0",
"eslint": "^7.6.0"
},
"dependencies": {
"moment-timezone": "^0.5.31"
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down