From: AJ ONeal Date: Wed, 21 Oct 2020 02:45:51 +0000 (-0600) Subject: add gitdeploy X-Git-Url: https://git.josue.xyz/?p=webi-installers%2F.git;a=commitdiff_plain;h=71e67540a1f2ad3fa74a55152d9375e3fc03a2e5 add gitdeploy --- diff --git a/gitdeploy/README.md b/gitdeploy/README.md new file mode 100644 index 0000000..8933597 --- /dev/null +++ b/gitdeploy/README.md @@ -0,0 +1,155 @@ +--- +title: gitdeploy +homepage: https://github.com/therootcompany/gitdeploy +tagline: | + gitdeploy receives git webhooks and runs build and deploy scripts. +--- + +To update or switch versions, run `webi gitdeploy@stable`. + +## Cheat Sheet + +> gitdeploy makes it easy to build and deploy static sites (or anything else) +> from git webhooks. + +Works with + +- GitHub +- Gitea +- Bitbucket +- and more ... + +To get set up, you'll want to copy the example scripts and dotenv: + +```bash +# The example scripts are a good starting point +rsync -av examples/ scripts/ + +# Edit this (or delete it) +mv scripts/dotenv .env +``` + +```bash +gitdeploy run --listen :4483 --github-secret xxxxx --exec scripts/ +``` + +When gitdeploy receives a webhook it runs `scripts/deploy.sh` with the following +environment variables set: + +```bash +GIT_REPO_ID=github.com/my-org/my-project + +GIT_CLONE_URL=https://github.com/my-org/my-project.git + +GIT_REPO_OWNER=my-org +GIT_REPO_NAME=my-project +GIT_REF_TYPE=branch +GIT_REF_NAME=master + +GIT_DEPLOY_JOB_ID=xxxxxx +``` + +The example `deploy.sh` looks for `deploy.sh` in the directory matching your +repository's URL, like this: + +- ./scripts/github.com/example/project/deploy.sh + +### How to create a build & deploy script + +The deploy scripts should exist in your `scripts/` directory, named after the +repo's name. + +```txt +scripts/github.com/YOUR_ORG/YOUR_PROJECT/deploy.sh +``` + +1. Create a directory that matches the `GIT_REPO_ID`: + ```bash + mkdir -p scripts/github.com/YOUR_ORG/YOUR_PROJECT + ``` +2. Create a `deploy.sh` that builds and deploys your project: + + ```bash + #!/bin/bash + + # Put bash in strict mode or bad things will happen. + set -u + set -e + + # maybe you do different things with different branches + # in this case we just ignore all branches except for master + if [[ "${GIT_REF_NAME}" != "master" ]] + then + echo "Nothing to do for ${GIT_REPO_ID}#${GIT_REF_NAME}" + exit 0 + fi + + # make a temporary directory for the build + my_tmp="$(mktemp -d -t "tmp.XXXXXXXXXX")" + git clone --depth=1 "${GIT_CLONE_URL}" -b "${GIT_REF_NAME}" "${my_tmp}/${GIT_REPO_NAME}" + + pushd "${my_tmp}/${GIT_REPO_NAME}/" + echo "Deploying ${GIT_REPO_ID}#${GIT_REF_NAME} ..." + + # run an example build process + npm ci + npm run build + + # deploy to an example staging site + rsync -av ./ ~/srv/staging.example.com/public/ + popd + + # clean up after the build + rm -rf "${my_tmp}/${GIT_REPO_NAME}/" + ``` + +### How to set up a webhook + +1. Generate a 128-bit random string: + ```bash + xxd -l16 -ps /dev/urandom + ``` +2. Create a new Web Hook on your git platform: + - Github: `https://github.com/YOUR_ORG/YOUR_REPO/settings/hooks/new` + - Gitea: `https://GIT_DOMAIN/YOUR_ORG/YOUR_REPO/settings/hooks/gitea/new` + - Webhook: `https://YOUR_DOMAIN/api/webhooks/gitea` + - BitBucket: + `https://bitbucket.org/YOUR_ORG/YOUR_REPO/admin/addon/admin/bitbucket-webhooks/bb-webhooks-repo-admin` +3. Set the content type to JSON. +4. Add the Webhook URL: + + ```bash + # Github + https://YOUR_DOMAIN/api/webhooks/github + + # Gitea + https://YOUR_DOMAIN/api/webhooks/gitea + + # Bitbucket + https://YOUR_DOMAIN/api/webhooks/bitbucket?access_token=YOUR_SECRET + ``` + +### How to use ENVs (and .env) + +Most of the flags, such as `--port` and `--github-secret` can also be set as +ENVs. You can create a `.env` like this, for example: + +```bash +PORT=4483 + +GITHUB_SECRET=xxxxxxxxxxx +``` + +See the +[examples/dotenv](https://git.rootprojects.org/root/gitdeploy/src/branch/master/examples/dotenv) +for more info. + +### How to use Deploy Keys & Personal Access Tokens + +See the +[Git Credentials Cheat Sheet](https://coolaj86.com/articles/vanilla-devops-git-credentials-cheatsheet/) +at . + +### How to reverse Proxy with HTTPS (Let's Encrypt) + +See the [Caddy (Web Server) Cheat Sheet](https://webinstall.dev/caddy). diff --git a/gitdeploy/install.ps1 b/gitdeploy/install.ps1 new file mode 100644 index 0000000..5d9d232 --- /dev/null +++ b/gitdeploy/install.ps1 @@ -0,0 +1,57 @@ +#!/usr/bin/env pwsh + +##################### +# Install gitdeploy # +##################### + +# Every package should define these variables +$pkg_cmd_name = "gitdeploy" + +$pkg_dst_cmd = "$Env:USERPROFILE\.local\bin\gitdeploy.exe" +$pkg_dst = "$pkg_dst_cmd" + +$pkg_src_cmd = "$Env:USERPROFILE\.local\opt\gitdeploy-v$Env:WEBI_VERSION\bin\gitdeploy.exe" +$pkg_src_bin = "$Env:USERPROFILE\.local\opt\gitdeploy-v$Env:WEBI_VERSION\bin" +$pkg_src_dir = "$Env:USERPROFILE\.local\opt\gitdeploy-v$Env:WEBI_VERSION" +$pkg_src = "$pkg_src_cmd" + +$pkg_download = "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE" + +# Fetch archive +IF (!(Test-Path -Path "$Env:USERPROFILE\Downloads\$Env:WEBI_PKG_FILE")) +{ + # TODO: arch detection + echo "Downloading gitdeploy from $Env:WEBI_PKG_URL to $pkg_download" + & curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part" + & move "$pkg_download.part" "$pkg_download" +} + +IF (!(Test-Path -Path "$pkg_src_cmd")) +{ + echo "Installing gitdeploy" + + # TODO: create package-specific temp directory + # Enter tmp + pushd .local\tmp + + # Remove any leftover tmp cruft + Remove-Item -Path ".\gitdeploy-v*" -Recurse -ErrorAction Ignore + Remove-Item -Path ".\gitdeploy.exe" -Recurse -ErrorAction Ignore + + # Unpack archive file into this temporary directory + # Windows BSD-tar handles zip. Imagine that. + echo "Unpacking $pkg_download" + & tar xf "$pkg_download" + + # Settle unpacked archive into place + echo "Install Location: $pkg_src_cmd" + New-Item "$pkg_src_bin" -ItemType Directory + Move-Item -Path ".\gitdeploy.exe" -Destination "$pkg_src_bin" + + # Exit tmp + popd +} + +echo "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'" +Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore +Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse diff --git a/gitdeploy/install.sh b/gitdeploy/install.sh new file mode 100644 index 0000000..f7b77c0 --- /dev/null +++ b/gitdeploy/install.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +function __init_gitdeploy() { + set -e + set -u + + ##################### + # Install gitdeploy # + ##################### + + # Every package should define these 6 variables + pkg_cmd_name="gitdeploy" + + pkg_dst_cmd="$HOME/.local/bin/gitdeploy" + pkg_dst="$pkg_dst_cmd" + + pkg_src_cmd="$HOME/.local/opt/gitdeploy-v$WEBI_VERSION/bin/gitdeploy" + pkg_src_dir="$HOME/.local/opt/gitdeploy-v$WEBI_VERSION" + pkg_src="$pkg_src_cmd" + + pkg_install() { + # $HOME/.local/opt/gitdeploy-v0.7.1/bin + mkdir -p "$pkg_src_bin" + + # mv ./gitdeploy* "$HOME/.local/opt/gitdeploy-v0.7.1/bin/gitdeploy" + mv ./"$pkg_cmd_name"* "$pkg_src_cmd" + + # chmod a+x "$HOME/.local/opt/gitdeploy-v0.7.1/bin/gitdeploy" + chmod a+x "$pkg_src_cmd" + } + + pkg_get_current_version() { + # 'gitdeploy version' has output in this format: + # gitdeploy v0.7.1 (be68fec) 2020-10-20T22:27:47Z) + # This trims it down to just the version number: + # 0.7.1 + echo "$(gitdeploy --version 2>/dev/null | head -n 1 | cut -d' ' -f2 | sed 's:^v::')" + } + +} + +__init_gitdeploy diff --git a/gitdeploy/releases.js b/gitdeploy/releases.js new file mode 100644 index 0000000..b446e99 --- /dev/null +++ b/gitdeploy/releases.js @@ -0,0 +1,19 @@ +'use strict'; + +var github = require('../_common/github.js'); +var owner = 'therootcompany'; +var repo = 'gitdeploy'; + +module.exports = function (request) { + return github(request, owner, repo).then(function (all) { + // remove checksums and .deb + return all; + }); +}; + +if (module === require.main) { + module.exports(require('@root/request')).then(function (all) { + all = require('../_webi/normalize.js')(all); + console.info(JSON.stringify(all)); + }); +}