aboutsummaryrefslogtreecommitdiff
path: root/simpleci.sh
blob: 57c2ff534ca1c12214915209802642544d9a1d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash

SCRIPT_DIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"

JOB_DIR="${SCRIPT_DIR}/jobs"
WORKSPACE_DIR="${SCRIPT_DIR}/workspaces"
LOG_DIR="${SCRIPT_DIR}/logs"

check_for_remote_changes() {
    local DIR=$1
    local BRANCH=$2

    pushd "${DIR}" > /dev/null || { echo "Directory ${DIR} not found!"; return 1; }

    # Fetch the latest changes from the remote
    git fetch

    # Get the local and remote commit hashes for the branch
    LOCAL=$(git rev-parse "${BRANCH}")
    REMOTE=$(git rev-parse "origin/${BRANCH}")

    # Check if local and remote are different
    if [ "${LOCAL}" != "${REMOTE}" ]; then
        echo $(date +"%Y-%m-%d %H:%M:%S") "Remote changes detected in ${DIR}"
        popd > /dev/null
        return 0
    else
        echo $(date +"%Y-%m-%d %H:%M:%S") "No remote changes in ${DIR}"
        popd > /dev/null
        return 1
    fi
}

trigger() {
    REPO=""
    BRANCH="live"
    COMMAND="make publish"
    WORKSPACE=""
    source "${1}"
    if [ ${WORKSPACE} ]; then
	DIR="${WORKSPACE}"
    else
	DIR="$(echo ${REPO##*/} | sed -e 's/\.git$//')"
    fi
    FORCE=${2}
    TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")

#    echo REPO=${REPO}
#    echo BRANCH=${BRANCH}
#    echo COMMAND=${COMMAND}
#    echo DIR=${DIR}
#    echo FORCE=${FORCE}

    
    pushd ${LOG_DIR} > /dev/null
    git checkout -B ${DIR}
    echo > build.log
    popd > /dev/null

    if [ ${REPO} ]; then
        if ! [ -e "${WORKSPACE_DIR}/${DIR}" ] ; then
            git clone -b ${BRANCH} ${REPO} "${WORKSPACE_DIR}/${DIR}" >> ${LOG_DIR}/build.log 2>&1
            FORCE=1
        fi
        if [ "${FORCE}" ] || check_for_remote_changes "${WORKSPACE_DIR}/${DIR}" "${BRANCH}"; then
            pushd "${WORKSPACE_DIR}/${DIR}" > /dev/null
            git pull origin ${BRANCH}   >> ${LOG_DIR}/build.log 2>&1
            ${COMMAND} >> ${LOG_DIR}/build.log 2>&1
            popd > /dev/null
	    HAS_RUN=1
        fi
    fi
    
    pushd ${LOG_DIR} > /dev/null
    if [ "${HAS_RUN}" ] ; then
	git add build.log
	git commit -m "Build ${TIMESTAMP}"
	git push -u origin "${DIR}"
    else
	git checkout build.log
    fi
    popd > /dev/null
}

mkdir -p "${WORKSPACE_DIR}"

if [ $# -gt 0 ] ; then
    for i in $@ ; do
        trigger "${JOB_DIR}/$i" 1
    done
else
    for i in ${JOB_DIR}/* ; do
        trigger "$i"
    done
fi