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

Commit 92c6304

Browse files
committed
rad-ci: Initial commit
Signed-off-by: xphoniex <dj.2dixx@gmail.com>
1 parent 98d698d commit 92c6304

File tree

7 files changed

+527
-1
lines changed

7 files changed

+527
-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 <dev@radicle.xyz>"]
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
file=$1
7+
port=$(cat $file | grep ports | cut -d \" -f2 | cut -d \: -f1)
8+
user=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f2)
9+
user=${user:1}
10+
pass=$(cat $file | grep "CONCOURSE_ADD_LOCAL_USER" | cut -d \: -f3)
11+
12+
cat <<END > $2
13+
CONCOURSE_USER=$user
14+
CONCOURSE_PASS=$pass
15+
CONCOURSE_URL=http://localhost:$port
16+
END
17+

ci/receive-hook

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

0 commit comments

Comments
 (0)