Skip to content

Commit

Permalink
Add docs, parameterize the directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrogers committed Apr 17, 2020
1 parent 20456ae commit 0b15562
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# GitHub Action - Synchronize Branches

This GitHub Action (written in JavaScript) checks out a branch with the same name as the context (`github.ref`) branch in each repository in a directory. This helps synchronize multi-repo builds when working on related feature branches in different repositories.

## Usage

### Inputs

- `path`: Directory containing the repositories to synchronize. Defaults to the workspace working directory.
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Synchronize Branches
description: Synchronize branches in a multi-repo build
inputs:
path:
description: The directory containing the repositories to synchronize
required: true
default: '.'
runs:
using: node12
main: dist/index.js
branding:
icon: git-branch
color: green
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"clean": "shx rm -rf dist/*",
"debug": "node --inspect-brk -r dotenv/config src/main.js",
"lint": "eslint . --cache --fix",
"start": "node -r dotenv/config src/main.js",
"test": "jest"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { synchronizeBranches } = require("./synchronize");

async function main() {
try {
await synchronizeBranches();
const path = core.getInput("path", { required: true });
await synchronizeBranches(path);
} catch (e) {
core.setFailed(e.message);
}
Expand Down
15 changes: 8 additions & 7 deletions src/synchronize.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ async function getChildRepositories(path) {
}

/**
* Attempt to fetch a matching branch in every git repository in the current directory.
* Attempt to fetch a matching branch in every git repository in a directory
* @param {string} path the directory containing the repositories to synchronize
*/
async function synchronizeBranches() {
async function synchronizeBranches(path) {
if (context.ref === "refs/heads/master" || context.ref.startsWith("refs/tags/")) {
core.info(`Skipping synchronize for ${context.ref}`);
return;
}

const branch = basename(context.ref);
const children = (await getChildRepositories(".")).filter(
(path) => basename(path) !== context.repo.repo
const children = (await getChildRepositories(path)).filter(
(child) => basename(child) !== context.repo.repo
);

for (let i = 0; i < children.length; ++i) {
const path = children[i];
const name = basename(path);
const git = simpleGit(path);
const child = children[i];
const name = basename(child);
const git = simpleGit(child);

// try to fetch a branch with the same name
// eslint-disable-next-line no-await-in-loop
Expand Down

0 comments on commit 0b15562

Please sign in to comment.