diff --git a/README.md b/README.md index 3908e90..b228941 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # git-trigger -git-trigger monitors a git branch and trigger a command defined in config file when changes are detected. +git-trigger monitors a git branch and trigger a shell command defined in config file when changes are detected. ## Usage @@ -53,6 +53,31 @@ module.exports = { } ``` +## Configuration parameters + +- interval: number + - intervals milliseconds for check git branches +- repositories: Array + - url: string + - target git repository + - branch: string + - target branch name + - dir: string + - clone directory + - actions: Array + - when: "changed"|"always" + - gitCmd: string(optional) + - git sub command executed before `run` command + - run: string(optional) + - shell command + +## Environment variables in command + +git-trigger assigns the following environtment variables to a command in `run` parameter. + +- `GIT_TRIGGER_COMMIT_HASH` +- `GIT_TRIGGER_PREV_COMMIT_HASH` + ## Live config update `git-trigger` supports live config update by `SIGUSR1` signal. diff --git a/src/Config.ts b/src/Config.ts index 5e523a3..d731ef7 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -1,6 +1,6 @@ export interface ActionConfig { - gitCmd: string; when: "changed"|"always"; + gitCmd?: string; run?: string; } diff --git a/src/Runner.ts b/src/Runner.ts index 41b8085..3666c72 100644 --- a/src/Runner.ts +++ b/src/Runner.ts @@ -1,5 +1,5 @@ import Timer from "./Timer"; -import { Config, RepositoryConfig } from "./Config"; +import { Config } from "./Config"; import * as fs from "fs-extra"; import * as git from "./git"; import * as execa from "execa"; @@ -48,14 +48,20 @@ export default class Runner { console.error("error: " + repo.dir + " is not " + repo.branch + " branch"); continue; } - const changed = await git.remoteChanged(repo.branch, repo.dir); + const [changed, prevHash] = await git.remoteChanged(repo.branch, repo.dir); for (const act of repo.actions) { if (act.when === "changed" && !changed) { continue; } - await git.exec(act.gitCmd, repo.dir); + if (act.gitCmd) { + await git.exec(act.gitCmd, repo.dir); + } if (act.run) { - await execa.shell(act.run, {cwd: repo.dir}); + const commitHash = await git.commitHash(repo.dir); + await execa.shell(act.run, {cwd: repo.dir, env: { + GIT_TRIGGER_COMMIT_HASH: commitHash, + GIT_TRIGGER_PREV_COMMIT_HASH: prevHash + }}); } } console.log("success: " + JSON.stringify({url: repo.url, branch: repo.branch, dir: repo.dir})); diff --git a/src/git.ts b/src/git.ts index ea9028c..336d990 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,12 +14,20 @@ export async function remoteUrl(cwd: string): Promise { return url; } -export async function remoteChanged(branch: string, cwd: string): Promise { - const {stdout: remote} = await execa("git", ["ls-remote", "origin", "refs/heads/" + branch], {cwd}); - const {stdout: local} = await execa("git", ["rev-parse", "HEAD"], {cwd}); - return remote.indexOf(local) !== 0; +export async function remoteChanged(branch: string, cwd: string): Promise<[boolean, string, string]> { + const remote = await (async () => { + const {stdout} = await execa("git", ["ls-remote", "origin", "refs/heads/" + branch], {cwd}); + return stdout.split("\t")[0].split(" ")[0]; + })(); + const local = await commitHash(cwd); + return [remote !== local, local, remote]; } export async function exec(cmd: string, cwd: string): Promise { await execa("git", cmd.split(" "), {cwd}); } + +export async function commitHash(cwd: string): Promise { + const { stdout } = await execa("git", ["rev-parse", "HEAD"], {cwd}); + return stdout; +}