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

Commit e87c37d

Browse files
committed
rad-ci: Initial commit
Signed-off-by: xphoniex <[email protected]>
1 parent 0987585 commit e87c37d

File tree

7 files changed

+572
-1
lines changed

7 files changed

+572
-1
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ assets = [
2020
["target/release/rad-checkout", "usr/bin/rad-checkout", "755"],
2121
["target/release/rad-untrack", "usr/bin/rad-untrack", "755"],
2222
["target/release/git-remote-rad", "usr/bin/git-remote-rad", "755"],
23+
["target/release/rad-ci", "usr/bin/rad-ci", "755"],
2324
["radicle-tools.1.gz", "usr/share/man/man1/radicle-tools.1.gz", "644"],
2425
]
2526

@@ -48,6 +49,7 @@ rad-untrack = { path = "./untrack" }
4849
rad-help = { path = "./help" }
4950
rad-ls = { path = "./ls" }
5051
rad-rm = { path = "./rm" }
52+
rad-ci = { path = "./ci" }
5153
ethers = { version = "0.6.2" }
5254
link-identities = { version = "0" }
5355
radicle-git-helpers = { version = "0" }
@@ -76,7 +78,8 @@ members = [
7678
"track",
7779
"untrack",
7880
"proof-generator",
79-
"authorized-keys"
81+
"authorized-keys",
82+
"ci"
8083
]
8184

8285
[patch.crates-io.link-crypto]

ci/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "rad-ci"
3+
version = "0.1.0"
4+
authors = ["The Radicle Team <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
description = "Install or uninstall ci on your seed node"
8+
9+
[dependencies]
10+
anyhow = "1.0"
11+
lexopt = { version = "0.2" }
12+
rad-common = { path = "../common" }
13+
rad-terminal = { path = "../terminal" }
14+
url = { version = "*" }
15+
ssh2 = "0.9.3"
16+
whoami = "1.2.1"

ci/extract-env

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
set -ue
4+
5+
file=$1
6+
port=$(cat $file | grep ports | cut -d \" -f2 | cut -d \: -f1)
7+
user=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f2 | cut -d ' ' -f2)
8+
pass=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f3)
9+
10+
cat <<END > $2
11+
CONCOURSE_USER=$user
12+
CONCOURSE_PASS=$pass
13+
CONCOURSE_URL=http://localhost:$port
14+
END
15+

ci/post-receive-ok

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/sh
2+
3+
echo "**** ***** Running Radicle CI Hook ***** ****"
4+
5+
if [ -f "$GIT_PROJECT_ROOT/etc/.ci.env" ]; then
6+
. "$GIT_PROJECT_ROOT/etc/.ci.env"
7+
fi
8+
9+
run_pipeline() {
10+
local tag=$(echo $GIT_NAMESPACE | cut -c 31-)
11+
local urn="$RADICLE_NAME-$tag"
12+
# login
13+
fly -t local login -c $CONCOURSE_URL -u $CONCOURSE_USER -p $CONCOURSE_PASS
14+
# create pipeline
15+
if [ -f "$GIT_PROJECT_ROOT/secrets/$GIT_NAMESPACE.yml" ]; then
16+
fly -t local validate-pipeline -c /tmp/$GIT_NAMESPACE/.rad/concourse.yml
17+
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
18+
else
19+
fly -t local set-pipeline -p $urn -c /tmp/$GIT_NAMESPACE/.rad/concourse.yml --non-interactive
20+
fi
21+
# unpause pipeline
22+
fly -t local unpause-pipeline -p $urn
23+
# trigger a job
24+
# TODO: trigger all jobs
25+
fly -t local trigger-job --job $urn/job
26+
}
27+
28+
delete_team() {
29+
local tag=$(echo $GIT_NAMESPACE | cut -c 31-)
30+
local urn="$RADICLE_NAME-$tag"
31+
# login
32+
fly -t local login -c $CONCOURSE_URL -u $CONCOURSE_USER -p $CONCOURSE_PASS
33+
# destroy pipeline
34+
fly -t local destroy-pipeline -p $urn -n
35+
}
36+
37+
check_ci_deletion() {
38+
local urn=$1
39+
local commit=$2
40+
local branch=$3
41+
# initial commit has prev_commit as 00..00
42+
if [ "$commit" = "0000000000000000000000000000000000000000" ]; then
43+
return;
44+
fi
45+
46+
# checkout into that commit
47+
unset GIT_DIR && cd /tmp/$urn && git checkout $commit -f -q
48+
49+
if [ -f "/tmp/$urn/.rad/concourse.yml" ]; then
50+
echo "ci yml was deleted in this commit"
51+
delete_team $urn
52+
fi
53+
54+
# revert checkout
55+
unset GIT_DIR && cd /tmp/$urn && git checkout $branch -f
56+
}
57+
58+
clone_or_pull() {
59+
local urn=$GIT_NAMESPACE
60+
if [ -e "/tmp/$urn" ]; then
61+
echo "pulling..."
62+
unset GIT_DIR && cd /tmp/$urn && git pull -f --rebase
63+
else
64+
echo "cloning..."
65+
git clone $GIT_DIR /tmp/$urn
66+
fi
67+
}
68+
69+
while read line
70+
do
71+
echo "stdin: $line"
72+
urn=$GIT_NAMESPACE
73+
prev_commit=$(echo $line | cut -d' ' -f2)
74+
branch=$(echo $line | cut -d' ' -f4)
75+
echo "urn: $urn, project: $RADICLE_NAME, prev_commit: $prev_commit, branch: $branch"
76+
77+
# clone the project locally in /tmp
78+
clone_or_pull
79+
80+
default_branch=$(GIT_DIR=/tmp/$urn/.git git symbolic-ref --short refs/remotes/origin/HEAD)
81+
# remove the `origin` part
82+
default_branch=$(echo $default_branch | cut -c 8-)
83+
84+
if [ "$branch" != "$default_branch" ]; then
85+
echo "skpping, only running on pushes to branch $default_branch"
86+
continue
87+
fi
88+
89+
# check for .rad/concourse.yml
90+
if [ -f "/tmp/$urn/.rad/concourse.yml" ]; then
91+
echo "yml exists."
92+
run_pipeline
93+
else
94+
echo "yml does not exist."
95+
check_ci_deletion $urn $prev_commit $branch
96+
exit 0
97+
fi
98+
99+
done
100+
101+
echo "Exiting..."
102+
exit 0

0 commit comments

Comments
 (0)