feat: add support for cloning/syncing from a remote repository

This makes it a bit easier for external projects to implement deploy webhooks by exposing the ability to sync from a git repository/sha.
This commit is contained in:
Jose Diaz-Gonzalez
2021-01-05 05:37:22 -05:00
parent db5630eb32
commit c80fcf7b8e
7 changed files with 261 additions and 3 deletions

View File

@@ -3,9 +3,13 @@
> Subcommands new as of 0.12.0
```
git:initialize <app> # Initialize a git repository for an app
git:report [<app>] [<flag>] # Displays a git report for one or more apps
git:set <app> <key> (<value>) # Set or clear a git property for an app
git:allow-host <host> # Adds a host to known_hosts
git:clone [--build] <app> <repository> [<git-ref>] # Creates an app from remote git repo
git:fetch [--build] <app> <repository> [<git-ref>] # Fetch the app at an optional repo object and build the codebase
git:initialize <app> # Initialize a git repository for an app
git:public-key # Outputs the dokku public deploy key
git:report [<app>] [<flag>] # Displays a git report for one or more apps
git:set <app> <key> (<value>) # Set or clear a git property for an app
```
Git-based deployment has been the traditional method of deploying applications in Dokku. As of v0.12.0, Dokku introduces a few ways to customize the experience of deploying via `git push`. A Git-based deployment currently supports building applications via:
@@ -105,3 +109,77 @@ dokku git:set node-js-app keep-git-dir false
# delete the .git directory during builds (default)
dokku git:set node-js-app keep-git-dir ""
```
### Initializing an app repository from a remote repository
> The application must exist before the repository can be initialized
A Dokku app repository can be initialized from a remote git repository via the `git:clone` command. This command should only be used for an initial sync to a remote repository as it may wipe out any local Dokku history that isn't in sync with the remote. Any repository that can be cloned by the `dokku` user can be specified.
```shell
dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git
```
The `git:clone` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit.
```shell
# specify a branch
dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git main
# specify a tag
dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 1
# specify a commit
dokku git:clone node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0
```
By default, this command does not trigger an application build. To do so during a `git:clone`, specify the `--build` flag.
```shell
dokku git:clone --build node-js-app https://github.com/heroku/node-js-getting-started.git
```
### Fetching repository updates from a remote repository
> The application must exist before the repository can be initialized
A Dokku app repository can be updated from a remote git repository via the `git:fetch` command. This command may fail if the specified remote repository cannot be synced from the current Dokku repository state. Any repository that can be cloned by the `dokku` user can be specified.
```shell
dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git
```
The `git:fetch` command optionally takes an optional third parameter containing a git reference, which may be a branch, tag, or specific commit.
```shell
# specify a branch
dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git main
# specify a tag
dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 1
# specify a commit
dokku git:fetch node-js-app https://github.com/heroku/node-js-getting-started.git 97e6c72491c7531507bfc5413903e0e00e31e1b0
```
By default, this command does not trigger an application build. To do so during a `git:fetch`, specify the `--build` flag.
```shell
dokku git:fetch --build node-js-app https://github.com/heroku/node-js-getting-started.git
```
### Allowing remote repository hosts
By default, the Dokku host may not have access to a server containing the remote repository. This can be initialized via the `git:allow-host` command.
```shell
dokku git:allow-host github.com
```
### Verifying the cloning public key
In order to clone a remote repository, the remote server should have the Dokku host's public key configured. This plugin does not currently create this key, but if there is one available, it can be shown via the `git:public-key` command.
```shell
dokku git:public-key
```

View File

@@ -27,7 +27,11 @@ help_desc
fn-help-content() {
declare desc="return help content"
cat <<help_content
git:allow-host <host>, Adds a host to known_hosts
git:clone [--build] <app> <repository> [<git-ref>], Creates an app from remote git repo
git:fetch [--build] <app> <repository> [<git-ref>], Fetch the app at an optional repo object and build the codebase
git:initialize <app>, Initialize a git repository for an app
git:public-key, Outputs the dokku public deploy key
git:report [<app>] [<flag>], Displays a git report for one or more apps
git:set <app> <property> (<value>), Set or clear a git property for an app
help_content

View File

@@ -4,6 +4,102 @@ source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-allow-host() {
declare desc="adds a host to known_hosts"
local cmd="git:allow-host"
[[ "$1" == "$cmd" ]] && shift 1
declare HOST="$1"
[[ -z "$HOST" ]] && dokku_log_fail "Please supply a git host"
ssh-keyscan -t rsa "$HOST" >> "$DOKKU_ROOT/.ssh/known_hosts"
dokku_log_info1 "$HOST added to known hosts"
}
cmd-git-clone() {
declare desc="creates an app from remote git repo"
local cmd="git:clone"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4"
ARGS=()
for arg in "$@"; do
if [[ "$arg" == "--build" ]]; then
FLAG="--build"
continue
fi
ARGS+=("$arg")
done
APP="$1"
GIT_REMOTE="$2"
GIT_REF="$3"
verify_app_name "$APP"
if [[ -z "$GIT_REMOTE" ]]; then
dokku_log_fail "Missing GIT_REMOTE parameter"
return
fi
fn-git-clone "$APP" "$GIT_REMOTE" "$GIT_REF"
if [[ "$FLAG" == "--build" ]]; then
if [[ -n "$GIT_REF" ]]; then
plugn trigger receive-app "$APP" "$GIT_REF"
else
plugn trigger receive-app "$APP"
fi
fi
}
cmd-git-fetch() {
declare desc="fetch the app at an optional repo object and build the codebase"
local cmd="git:fetch"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3" FLAG="$4"
ARGS=()
for arg in "$@"; do
if [[ "$arg" == "--build" ]]; then
FLAG="--build"
continue
fi
ARGS+=("$arg")
done
APP="$1"
GIT_REMOTE="$2"
GIT_REF="$3"
verify_app_name "$APP"
if [[ -z "$GIT_REMOTE" ]]; then
dokku_log_fail "Missing GIT_REMOTE parameter"
return
fi
fn-git-fetch "$APP" "$GIT_REMOTE" "$GIT_REF"
if [[ "$FLAG" == "--build" ]]; then
if [[ -n "$GIT_REF" ]]; then
plugn trigger receive-app "$APP" "$GIT_REF"
else
plugn trigger receive-app "$APP"
fi
fi
}
cmd-git-public-key() {
declare desc="outputs the dokku public deploy key"
local cmd="git:public-key"
[[ "$1" == "$cmd" ]] && shift 1
if [[ ! -f "$DOKKU_ROOT/.ssh/id_rsa.pub" ]]; then
dokku_log_fail "There is no deploy key"
fi
cat "$DOKKU_ROOT/.ssh/id_rsa.pub"
}
cmd-git-report() {
declare desc="displays a git report for one or more apps"
declare cmd="git:report"
@@ -71,6 +167,62 @@ cmd-git-report-single() {
fi
}
fn-git-clone() {
declare desc="creates an app from remote git repo"
declare APP="$1" GIT_REMOTE="$2" GIT_REF="$2"
local APP_CLONE_ROOT="$PLUGIN_DATA_ROOT/$APP"
local APP_ROOT="$DOKKU_ROOT/$APP"
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
if [[ -d "$DOKKU_ROOT/$APP/refs" ]]; then
dokku_log_fail "The clone subcommand can only be executed for new applications"
fi
if [[ -d "$APP_CLONE_ROOT" ]]; then
dokku_log_fail "Clone in progress"
fi
if [[ -z "$GIT_REF" ]]; then
GIT_REF="$(fn-git-deploy-branch "$APP")"
fi
dokku_log_info1_quiet "Creating $APP from $GIT_REMOTE#$GIT_REF"
trap 'rm -rf $APP_CLONE_ROOT > /dev/null' RETURN INT TERM EXIT
git clone --depth 1 -n -qq --branch "$GIT_REF" "$GIT_REMOTE" "$APP_CLONE_ROOT"
if [[ "$GIT_REF" != "master" ]]; then
pushd "$APP" > /dev/null
git checkout -qq -b master
popd > /dev/null
fi
rsync -a "$APP_CLONE_ROOT/.git/" "$APP_ROOT"
fn-git-create-hook "$APP"
}
fn-git-fetch() {
declare APP="$1" GIT_REMOTE="$2" GIT_REF="$3"
local DOKKU_DEPLOY_BRANCH
DOKKU_DEPLOY_BRANCH="$(fn-git-deploy-branch "$APP")"
if ! git check-ref-format --branch "$DOKKU_DEPLOY_BRANCH" >/dev/null 2>&1; then
echo $'\e[1G\e[K'"-----> WARNING: Invalid branch name '$DOKKU_DEPLOY_BRANCH' specified via DOKKU_DEPLOY_BRANCH."
echo $'\e[1G\e[K'"-----> For more details, please see the man page for 'git-check-ref-format.'"
return
fi
pushd "$DOKKU_ROOT/$APP" > /dev/null
git remote rm remote > /dev/null 2>&1 || true
git remote add --mirror=fetch --no-tags remote "$GIT_REMOTE"
if [[ -z "$GIT_REF" ]]; then
git fetch remote "$DOKKU_DEPLOY_BRANCH"
else
git fetch remote
git update-ref refs/heads/master "$GIT_REF"
fi
popd > /dev/null
}
fn-git-last-updated-at() {
declare desc="retrieve the deploy branch for a given application"
declare APP="$1"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-allow-host "$@"

6
plugins/git/subcommands/clone Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-clone "$@"

6
plugins/git/subcommands/fetch Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-fetch "$@"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/git/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-git-public-key "$@"