summaryrefslogtreecommitdiffhomepage
path: root/ci/ios/buildserver-build-ios.sh
blob: b62207ffd7f740171292cee3dffc6e996da5ee19 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
set -eu
shopt -s nullglob

TAG_PATTERN_TO_BUILD="^ios/"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUILD_DIR="$SCRIPT_DIR/mullvadvpn-app/ios"
LAST_BUILT_DIR="$SCRIPT_DIR/last-built"
mkdir -p "$LAST_BUILT_DIR"

# Convince git to work on our checkout of the app repository, regardless of PWD.
export GIT_WORK_TREE="$SCRIPT_DIR/mullvadvpn-app/"
export GIT_DIR="$GIT_WORK_TREE/.git"
function run_git {
    # `git submodule` needs more info than just $GIT_DIR and $GIT_WORK_TREE.
    # But -C makes it work.
    git -C "$GIT_WORK_TREE" "$@"
}


function build_ref() {
    local tag=$1;
    local current_hash="";
    if ! current_hash=$(run_git rev-parse "$tag^{commit}"); then
        echo "!!!"
        echo "[#] Failed to get commit for $tag"
        echo "!!!"
        return
    fi

    if [ -f "$LAST_BUILT_DIR/commit-$current_hash" ]; then
        # This commit has already been built
        return 0
    fi


    if ! run_git verify-tag "$tag"; then
        echo "!!!"
        echo "[#] $tag failed GPG verification!"
        echo "!!!"
        sleep 60
        return 0
    fi

    run_git reset --hard
    run_git checkout "$tag"
    run_git submodule update
    run_git submodule update --init ios/wireguard-apple || true
    run_git clean -df

    local app_build_version="";
    if ! app_build_version=$(read_app_version); then
        echo "!!!"
        echo "[#] Failed to read app build version for tag $tag ($current_hash)"
        echo "!!!"
        return 0
    fi


    if [ -f "$LAST_BUILT_DIR/build-$app_build_version" ]; then
        echo "!!!"
        echo "[#] App version $app_build_version already built in commit $(cat "${LAST_BUILT_DIR}/build-${app_build_version}")"
        echo "[#] The build version in Configuration/Version.xcconfig should be bumped."
        echo "[#] Failed to build $current_hash"
        echo "!!!"
        return 0
    fi

    echo ""
    echo "[#] $tag: $app_build_version $current_hash, building new packages."

    if "$SCRIPT_DIR"/run-build-and-upload.sh; then
        touch "$LAST_BUILT_DIR"/"commit-$current_hash"
        echo "$current_hash" > "$LAST_BUILT_DIR"/"build-${app_build_version}"
        echo "Successfully built ${app_build_version} ${tag} with hash ${current_hash}"
    fi
}

function read_app_version() {
    project_version=$(sed -n "s/CURRENT_PROJECT_VERSION = \([[:digit:]]\)/\1/p" "$BUILD_DIR/Configurations/Version.xcconfig")
    marketing_version=$(sed -n "s/MARKETING_VERSION = \([[:digit:]]\)/\1/p" "$BUILD_DIR/Configurations/Version.xcconfig")
    echo "${marketing_version}-${project_version}"
    if [ -z "$project_version" ] || [ -z "$marketing_version" ]; then
        exit 1;
    fi
}



function run_build_loop() {
    while true; do
        # Delete all tags. So when fetching we only get the ones existing on the remote
        run_git tag | xargs git tag -d > /dev/null

        run_git fetch --prune --tags --recurse-submodules=no 2> /dev/null || true
        local tags

        # shellcheck disable=SC2207
        tags=( $(run_git tag | grep "$TAG_PATTERN_TO_BUILD") )

        for tag in "${tags[@]}"; do
          build_ref "refs/tags/$tag"
        done

        sleep 240
    done
}

run_build_loop