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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface ActionConfig {
gitCmd: string;
when: "changed"|"always";
gitCmd?: string;
run?: string;
}

Expand Down
14 changes: 10 additions & 4 deletions src/Runner.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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}));
Expand Down
16 changes: 12 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ export async function remoteUrl(cwd: string): Promise<string> {
return url;
}

export async function remoteChanged(branch: string, cwd: string): Promise<boolean> {
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<void> {
await execa("git", cmd.split(" "), {cwd});
}

export async function commitHash(cwd: string): Promise<string> {
const { stdout } = await execa("git", ["rev-parse", "HEAD"], {cwd});
return stdout;
}