Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f3e1258
Showing
5 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# snoot.club/post-receive | ||
|
||
a post receive deploy hook for snoots | ||
|
||
## usage | ||
|
||
put `npx @snootclub/post-receive` in your serve-side repo's `hooks/post-receive` | ||
|
||
## snoot usage | ||
|
||
for default snoot behaviour you can do something like this inside your snoot container: | ||
|
||
```sh | ||
git init --bare /repo | ||
>/repo/hooks/post-receive cat <<. | ||
#!/bin/sh | ||
npx @snootclub/post-receive | ||
. | ||
chmod +x /repo/hooks/post-receive | ||
cd /application | ||
git remote add origin /repo | ||
git add . | ||
git push | ||
``` | ||
|
||
now you'll be able to do this on your local machine: | ||
|
||
```sh | ||
git clone ssh://snoot.club:$MY_PORT/repo snoot | ||
``` | ||
|
||
and when you push up any changes in `snoot` it will install them, | ||
build them and make them live. | ||
|
||
## advanced usage | ||
|
||
this isn't really specific to snoots and there are some options avaiable | ||
|
||
### --deploymentDirectory | ||
|
||
the directory to use as the deployment target, where files are served from | ||
by the webserver. | ||
|
||
default `/application` | ||
|
||
### --deploymentBranch | ||
|
||
the only brank acceptable to deploy from. if changes are pushed to any branch | ||
other than this, they won't be deployed ^_^ | ||
|
||
default `master` | ||
|
||
### --repoDirectory | ||
|
||
the source bare repo directory used when deploying. by default this uses the | ||
directory the process is running from, which will be the current repo when | ||
used in a git hook :) | ||
|
||
default `process.cwd()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/env node | ||
let getStream = require("get-stream") | ||
let yargs = require("yargs") | ||
let execa = require("execa") | ||
let path = require("path") | ||
let fs = require("fs-extra") | ||
|
||
let { | ||
argv | ||
} = yargs.options({ | ||
deploymentDirectory: { | ||
alias: ["d", "directory"], | ||
default: "/application", | ||
type: "string" | ||
}, | ||
deploymentBranch: { | ||
alias: ["b", "branch", "brank"], | ||
default: "master", | ||
type: "string" | ||
}, | ||
repoDirectory: { | ||
alias: ["r", "repo"], | ||
// the post-receive runs in the repo | ||
default: process.cwd(), | ||
type: "string" | ||
} | ||
}) | ||
|
||
void async function postーreceive () { | ||
let input = await getStream(process.stdin) | ||
|
||
// post-receive gets called with `${previousRevisionHash} ${newRevisionHash} ${ref}` | ||
let [, , ref] = input.split(/\s+/) | ||
// ref takes the form `/refs/heads/${name}` | ||
let [, brank] = ref.match(/refs\/heads\/(.*)/) | ||
|
||
|
||
if (brank != argv.brank) { | ||
return console.log(`not going to make ${brank} live. only ${argv.brank} is made live.`) | ||
} | ||
|
||
// cool, we have new code in the live branch !! | ||
if (!await fs.pathExists(argv.deploymentDirectory)) { | ||
console.warn(`warn: deploy directory "${argv.deploymentDirectory}" didn't exist. making it`) | ||
await fs.mkdirp(argv.deploymentDirectory) | ||
} | ||
|
||
await execa("git", [ | ||
`--work-tree=${argv.deploymentDirectory}`, | ||
`--git-dir=${argv.repo}`, | ||
"checkout", | ||
"-f", | ||
brank | ||
]) | ||
|
||
let packageJsonPath = path.resolve(argv.deploymentDirectory, "package.json") | ||
|
||
let hasPackageJson = await fs.pathExists(packageJsonPath) | ||
|
||
if (!hasPackageJson) { | ||
return console.warn("no package.json so considering this snoot deployed") | ||
} | ||
|
||
await execa("npm", [ | ||
"install", | ||
`--prefix=${argv.deploymentDirectory}` | ||
]) | ||
|
||
let package = require(packageJsonPath) | ||
|
||
if (!package.scripts || !package.scripts.build) { | ||
return console.warn("no package.json#scripts#build so considering this snoot deployed!!") | ||
} | ||
|
||
await execa("npm", [ | ||
"run-script", | ||
`--prefix=${argv.deploymentDirectory}`, | ||
"build" | ||
]) | ||
|
||
console.log("snoot deployed, installed && built !!") | ||
}() | ||
|
Oops, something went wrong.