Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
rad-ci: Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: xphoniex <[email protected]>
  • Loading branch information
xphoniex committed Feb 14, 2022
1 parent 0987585 commit dd46205
Show file tree
Hide file tree
Showing 7 changed files with 572 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ assets = [
["target/release/rad-checkout", "usr/bin/rad-checkout", "755"],
["target/release/rad-untrack", "usr/bin/rad-untrack", "755"],
["target/release/git-remote-rad", "usr/bin/git-remote-rad", "755"],
["target/release/rad-ci", "usr/bin/rad-ci", "755"],
["radicle-tools.1.gz", "usr/share/man/man1/radicle-tools.1.gz", "644"],
]

Expand Down Expand Up @@ -48,6 +49,7 @@ rad-untrack = { path = "./untrack" }
rad-help = { path = "./help" }
rad-ls = { path = "./ls" }
rad-rm = { path = "./rm" }
rad-ci = { path = "./ci" }
ethers = { version = "0.6.2" }
link-identities = { version = "0" }
radicle-git-helpers = { version = "0" }
Expand Down Expand Up @@ -76,7 +78,8 @@ members = [
"track",
"untrack",
"proof-generator",
"authorized-keys"
"authorized-keys",
"ci"
]

[patch.crates-io.link-crypto]
Expand Down
16 changes: 16 additions & 0 deletions ci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "rad-ci"
version = "0.1.0"
authors = ["The Radicle Team <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
description = "Install or uninstall ci on your seed node"

[dependencies]
anyhow = "1.0"
lexopt = { version = "0.2" }
rad-common = { path = "../common" }
rad-terminal = { path = "../terminal" }
url = { version = "*" }
ssh2 = "0.9.3"
whoami = "1.2.1"
15 changes: 15 additions & 0 deletions ci/extract-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -ue

file=$1
port=$(cat $file | grep ports | cut -d \" -f2 | cut -d \: -f1)
user=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f2 | cut -d ' ' -f2)
pass=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f3)

cat <<END > $2
CONCOURSE_USER=$user
CONCOURSE_PASS=$pass
CONCOURSE_URL=http://localhost:$port
END

102 changes: 102 additions & 0 deletions ci/post-receive-ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh

echo "**** ***** Running Radicle CI Hook ***** ****"

if [ -f "$GIT_PROJECT_ROOT/etc/.ci.env" ]; then
. "$GIT_PROJECT_ROOT/etc/.ci.env"
fi

run_pipeline() {
local tag=$(echo $GIT_NAMESPACE | cut -c 30-)
local urn="$RADICLE_NAME-$tag"
# login
fly -t local login -c $CONCOURSE_URL -u $CONCOURSE_USER -p $CONCOURSE_PASS
# create pipeline
if [ -f "$GIT_PROJECT_ROOT/secrets/$GIT_NAMESPACE.yml" ]; then
fly -t local validate-pipeline -c /tmp/$GIT_NAMESPACE/.rad/concourse.yml
fly -t local set-pipeline -p $urn -c /tmp/$GIT_NAMESPACE/.rad/concourse.yml --non-interactive -l $GIT_PROJECT_ROOT/secrets/$GIT_NAMESPACE.yml > /dev/null
else
fly -t local set-pipeline -p $urn -c /tmp/$GIT_NAMESPACE/.rad/concourse.yml --non-interactive
fi
# unpause pipeline
fly -t local unpause-pipeline -p $urn
# trigger a job
# TODO: trigger all jobs
fly -t local trigger-job --job $urn/job
}

delete_team() {
local tag=$(echo $GIT_NAMESPACE | cut -c 31-)
local urn="$RADICLE_NAME-$tag"
# login
fly -t local login -c $CONCOURSE_URL -u $CONCOURSE_USER -p $CONCOURSE_PASS
# destroy pipeline
fly -t local destroy-pipeline -p $urn -n
}

check_ci_deletion() {
local urn=$1
local commit=$2
local branch=$3
# initial commit has prev_commit as 00..00
if [ "$commit" = "0000000000000000000000000000000000000000" ]; then
return;
fi

# checkout into that commit
unset GIT_DIR && cd /tmp/$urn && git checkout $commit -f -q

if [ -f "/tmp/$urn/.rad/concourse.yml" ]; then
echo "ci yml was deleted in this commit"
delete_team $urn
fi

# revert checkout
unset GIT_DIR && cd /tmp/$urn && git checkout $branch -f
}

clone_or_pull() {
local urn=$GIT_NAMESPACE
if [ -e "/tmp/$urn" ]; then
echo "pulling..."
unset GIT_DIR && cd /tmp/$urn && git pull -f --rebase
else
echo "cloning..."
git clone $GIT_DIR /tmp/$urn
fi
}

while read line
do
echo "stdin: $line"
urn=$GIT_NAMESPACE
prev_commit=$(echo $line | cut -d' ' -f2)
branch=$(echo $line | cut -d' ' -f4)
echo "urn: $urn, project: $RADICLE_NAME, prev_commit: $prev_commit, branch: $branch"

# clone the project locally in /tmp
clone_or_pull

default_branch=$(GIT_DIR=/tmp/$urn/.git git symbolic-ref --short refs/remotes/origin/HEAD)
# remove the `origin` part
default_branch=$(echo $default_branch | cut -c 8-)

if [ "$branch" != "$default_branch" ]; then
echo "skpping, only running on pushes to branch $default_branch"
continue
fi

# check for .rad/concourse.yml
if [ -f "/tmp/$urn/.rad/concourse.yml" ]; then
echo "yml exists."
run_pipeline
else
echo "yml does not exist."
check_ci_deletion $urn $prev_commit $branch
exit 0
fi

done

echo "Exiting..."
exit 0
Loading

0 comments on commit dd46205

Please sign in to comment.