File: upload-artifacts.sh

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (170 lines) | stat: -rwxr-xr-x 5,134 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh

set -e

builds=build/
packages=${builds}*.deb
folder=`mktemp -u tmp.XXXXXXXXXXXXXXX`
aptly_user="$APTLY_USER"
aptly_password="$APTLY_PASSWORD"
aptly_api="https://aptly-ops.aptly.info"
version=`make version`

action=$1
dist=$2

usage() {
    echo "Usage: $0 ci buster|bullseye|bookworm|focal|jammy|noble" >&2
    echo "       $0 release" >&2
}

# repos and publish must be created beforehand:
#!/bin/sh
#for dist in buster bullseye bookworm focal jammy noble
#do
#        for build in ci release
#        do
#            echo
#            echo "# Creating and publishing $build/$dist"
#            aptly repo create -distribution=$dist -component=main aptly-$build-$dist
#            aptly publish repo -multi-dist -architectures="amd64,i386,arm64,armhf" -acquire-by-hash -component=main \
#                               -distribution=$dist -batch -keyring=aptly.pub \
#                               aptly-$build-$dist \
#                               s3:repo.aptly.info:$build
#       done
#done

if [ -z "$action" ]; then
    usage
    exit 1
fi

if [ "action" = "ci" ] && [ -z "$dist" ]; then
    usage
    exit 1
fi

if [ -z "$aptly_user" ] || [ -z "$aptly_password" ]; then
    usage
    echo Error: please set APTLY_USER and APTLY_PASSWORD
    exit 1
fi

echo "Publishing version '$version' to $action for $dist...\n"

upload()
{
    echo "\nUploading files:"
    for file in $packages; do
        echo " - $file"
        jsonret=`curl -fsS -X POST -F "file=@$file" -u $aptly_user:$aptly_password ${aptly_api}/api/files/$folder`
    done
}

cleanup() {
    echo "\nCleanup..."
    jsonret=`curl -fsS -X DELETE  -u $aptly_user:$aptly_password ${aptly_api}/api/files/$folder`
}
trap cleanup EXIT

sleeptime=5
retries=60
wait_task()
{
    _id=$1
    _success=0
    sleep $sleeptime
    for t in `seq $retries`
    do
        jsonret=`curl -fsS -u $aptly_user:$aptly_password ${aptly_api}/api/tasks/$_id`
        _state=`echo $jsonret | jq .State`
        if [ "$_state" = "2" ]; then
            _success=1
            curl -fsS -X DELETE -u $aptly_user:$aptly_password ${aptly_api}/api/tasks/$_id
            break
        fi
        if [ "$_state" = "3" ]; then
            echo Error: task failed
            return 1
        fi
        sleep $sleeptime
    done
    if [ "$_success" -ne 1 ]; then
        echo Error: task timeout
        return 1
    fi
    return 0
}

add_packages() {
    _aptly_repository=$1
    _folder=$2
    jsonret=`curl -fsS -X POST -u $aptly_user:$aptly_password ${aptly_api}/api/repos/$_aptly_repository/file/$_folder?_async=true`
    _task_id=`echo $jsonret | jq .ID`
    wait_task $_task_id
    if [ "$?" -ne 0 ]; then
        echo "Error: adding packages to $_aptly_repository failed"
        exit 1
    fi
}

update_publish() {
    _publish=$1
    _dist=$2
    jsonret=`curl -fsS -X PUT -H 'Content-Type: application/json' --data \
        '{"AcquireByHash": true, "MultiDist": true,
          "Signing": {"Batch": true, "Keyring": "aptly.repo/aptly.pub", "secretKeyring": "aptly.repo/aptly.sec", "PassphraseFile": "aptly.repo/passphrase"}}' \
        -u $aptly_user:$aptly_password ${aptly_api}/api/publish/$_publish/$_dist?_async=true`
    _task_id=`echo $jsonret | jq .ID`
    wait_task $_task_id
    if [ "$?" -ne 0 ]; then
        echo "Error: publish failed"
        exit 1
    fi
}

if [ "$action" = "ci" ]; then
    if echo "$version" | grep -vq "+"; then
       # skip ci when on release tag
       exit 0
    fi

    aptly_repository=aptly-ci-$dist
    aptly_published=s3:repo.aptly.info:ci

elif [ "$action" = "release" ]; then
    aptly_repository=aptly-release-$dist
    aptly_published=s3:repo.aptly.info:release
fi

upload

echo "\nAdding packages to $aptly_repository ..."
add_packages $aptly_repository $folder

echo "\nUpdating published repo $aptly_published ..."
update_publish $aptly_published $dist

# if [ "$action" = "OBSOLETErelease" ]; then
#     aptly_repository=aptly-release
#     aptly_snapshot=aptly-$version
#     aptly_published=s3:repo.aptly.info:./squeeze
#
#     echo "\nAdding packages to $aptly_repository..."
#     curl -fsS -X POST -u $aptly_user:$aptly_password ${aptly_api}/api/repos/$aptly_repository/file/$folder
#     echo
#
#     echo "\nCreating snapshot $aptly_snapshot from repo $aptly_repository..."
#     curl -fsS -X POST -u $aptly_user:$aptly_password -H 'Content-Type: application/json' --data \
#         "{\"Name\":\"$aptly_snapshot\"}" ${aptly_api}/api/repos/$aptly_repository/snapshots
#     echo
#
#     echo "\nSwitching published repo $aptly_published to use snapshot $aptly_snapshot..."
#     curl -fsS -X PUT -H 'Content-Type: application/json' --data \
#         "{\"AcquireByHash\": true, \"Snapshots\": [{\"Component\": \"main\", \"Name\": \"$aptly_snapshot\"}],
#                                    \"Signing\": {\"Batch\": true, \"Keyring\": \"aptly.repo/aptly.pub\",
#                                                  \"secretKeyring\": \"aptly.repo/aptly.sec\", \"PassphraseFile\": \"aptly.repo/passphrase\"}}" \
#         -u $aptly_user:$aptly_password ${aptly_api}/api/publish/$aptly_published
#     echo
# fi