|
| 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