forked from linuxserver/pipeline-triggers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger-logic.sh
executable file
·225 lines (208 loc) · 7.42 KB
/
trigger-logic.sh
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! /bin/bash
# Jenkins build trigger for external release changes and package difference
#################################
## Current Build trigger types ##
#################################
# alpine_package - an array of alpine packages is passed and all their versions are taken from head an md5 is generated to tag their version
# ubuntu_package - an array of alpine packages is passed and all their versions are taken from head an md5 is generated to tag their version
# deb_repo - One or more external repos is used to check an array of packages versions and generate an md5 hash
# alpine_repo - One external repo is used to check an array of packages versions and generate an md5 hash
# external_blob - An external file blob is downloaded and an md5 is generated to determine the external version
################
# Set Varibles #
################
# Set Parameters
for i in "$@"
do
case $i in
-TRIGGER_TYPE=*)
TRIGGER_TYPE="${i#*=}"
shift
;;
-LS_USER=*)
LS_USER="${i#*=}"
shift
;;
-LS_REPO=*)
LS_REPO="${i#*=}"
shift
;;
-EXT_GIT_BRANCH=*)
EXT_GIT_BRANCH="${i#*=}"
shift
;;
-EXT_USER=*)
EXT_USER="${i#*=}"
shift
;;
-EXT_REPO=*)
EXT_REPO="${i#*=}"
shift
;;
-EXT_NPM=*)
EXT_NPM="${i#*=}"
shift
;;
-EXT_PIP=*)
EXT_PIP="${i#*=}"
shift
;;
-EXT_BLOB=*)
EXT_BLOB="${i#*=}"
shift
;;
-BUILDS_DISCORD=*)
BUILDS_DISCORD="${i#*=}"
shift
;;
-DIST_IMAGE=*)
DIST_IMAGE="${i#*=}"
shift
;;
-DIST_TAG=*)
DIST_TAG="${i#*=}"
shift
;;
-DIST_PACKAGES=*)
DIST_PACKAGES="${i#*=}"
shift
;;
-DIST_REPO=*)
DIST_REPO="${i#*=}"
shift
;;
-DIST_REPO_PACKAGES=*)
DIST_REPO_PACKAGES="${i#*=}"
shift
;;
-JENKINS_USER=*)
JENKINS_USER="${i#*=}"
shift
;;
-JENKINS_API_KEY=*)
JENKINS_API_KEY="${i#*=}"
shift
;;
esac
done
# Get the current release info
LS_RELEASE=$(curl -s https://api.github.com/repos/${LS_USER}/${LS_REPO}/releases/latest | jq -r '. | .name')
EXTERNAL_TAG=$(echo ${LS_RELEASE} | awk -F'-pkg-' '{print $1}')
PACKAGE_TAG=$(echo ${LS_RELEASE} | grep -o -P '(?<=-pkg-).*(?=-ls)')
LS_VERSION=$(echo ${LS_RELEASE} | sed 's/^.*-ls//g')
#############
# Functions #
#############
# Send a message explaining the build trigger and reason to discord
function tell_discord {
curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 9802903,
"description": "**Build Triggerd** \n**Reason:**: '"${TRIGGER_REASON}"' \n"}],
"username": "Jenkins"}' ${BUILDS_DISCORD}
}
function tell_discord_fail {
curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,
"description": "**Trigger Failed** \n**Reason:**: '"${FAILURE_REASON}"' \n"}],
"username": "Jenkins"}' ${BUILDS_DISCORD}
}
# Trigger the build for this triggers job
function trigger_build {
curl -X POST \
https://pipeline.linuxserver.io/job/${LS_REPO}/job/master/build \
--user ${JENKINS_USER}:${JENKINS_API_KEY}
tell_discord
}
####################
# Package triggers #
####################
# This is an Alpine package trigger
if [ "${TRIGGER_TYPE}" == "alpine_package" ]; then
echo "This is an alpine package trigger"
# Pull the latest alpine image
docker pull alpine:${DIST_TAG}
# Determine the current tag
CURRENT_PACKAGE=$(docker run --rm alpine:${DIST_TAG} sh -c 'apk update --quiet\
&& apk info '"${DIST_PACKAGES}"' | md5sum | cut -c1-8')
# If the current tag does not match the external release then trigger a build
if [ "${CURRENT_PACKAGE}" != "${PACKAGE_TAG}" ]; then
TRIGGER_REASON="An Alpine base package change was detected for ${LS_REPO} old md5:${PACKAGE_TAG} new md5:${CURRENT_PACKAGE}"
trigger_build
else
echo "Nothing to do release is up to date"
fi
fi
# This is an Ubuntu package trigger
if [ "${TRIGGER_TYPE}" == "ubuntu_package" ]; then
echo "This is an ubuntu package trigger"
# Pull the latest ubuntu image
docker pull ubuntu:${DIST_TAG}
# Determine the current tag
CURRENT_PACKAGE=$(docker run --rm ubuntu:${DIST_TAG} sh -c\
'apt-get --allow-unauthenticated update -qq >/dev/null 2>&1 &&\
apt-cache --no-all-versions show '"${DIST_PACKAGES}"' | md5sum | cut -c1-8')
# If the current tag does not match the external release then trigger a build
if [ "${CURRENT_PACKAGE}" != "${PACKAGE_TAG}" ]; then
TRIGGER_REASON="An Ubuntu base package change was detected for ${LS_REPO} old md5:${PACKAGE_TAG} new md5:${CURRENT_PACKAGE}"
trigger_build
else
echo "Nothing to do release is up to date"
fi
fi
######################################
# External Software Release Triggers #
######################################
# This is a debian repo package trigger
if [ "${TRIGGER_TYPE}" == "deb_repo" ]; then
echo "This is an deb repo package trigger"
# Pull the latest image
docker pull ${DIST_IMAGE}:${DIST_TAG}
# Determine the current tag
CURRENT_PACKAGE=$(docker run --rm ${DIST_IMAGE}:${DIST_TAG} bash -c\
'echo -e "'"${DIST_REPO}"'" > /etc/apt/sources.list.d/check.list \
&& apt-get --allow-unauthenticated update -qq >/dev/null 2>&1\
&& apt-cache --no-all-versions show '"${DIST_REPO_PACKAGES}"' | md5sum | cut -c1-8')
# If the current tag does not match the external release then trigger a build
if [ "${CURRENT_PACKAGE}" != "${EXTERNAL_TAG}" ]; then
TRIGGER_REASON="A Debian package update has been detected for the ${LS_REPO} old md5:${EXTERNAL_TAG} new md5:${CURRENT_PACKAGE}"
trigger_build
else
echo "Nothing to do release is up to date"
fi
fi
# This is an external file blob release
if [ "${TRIGGER_TYPE}" == "external_blob" ]; then
echo "This is an External file blob package trigger"
# Determine the current md5 for the file blob
# Make sure the remote file returns a 200 status or fail
if [ $(curl -I -sL -w "%{http_code}" "${EXT_BLOB}" -o /dev/null) == 200 ]; then
CURRENT_MD5=$(curl -s -L "${EXT_BLOB}" | md5sum | cut -c1-8)
else
FAILURE_REASON='Unable to get the URL:'"${EXT_BLOB}"' for '"${LS_REPO}"' make sure URLs used to trigger are up to date'
tell_discord_fail
exit 0
fi
# If the current tag does not match the external release then trigger a build
if [ "${CURRENT_MD5}" != "${EXTERNAL_TAG}" ]; then
echo "ext: ${EXTERNAL_TAG}"
echo "current:${CURRENT_MD5}"
TRIGGER_REASON='An external file change was detected for '"${LS_REPO}"' at the URL:'"${EXT_BLOB}"' old md5:'"${EXTERNAL_TAG}"' new md5:'"${CURRENT_MD5}"
trigger_build
else
echo "Nothing to do release is up to date"
fi
fi
# This is an Alpine repo trigger
if [ "${TRIGGER_TYPE}" == "alpine_repo" ]; then
echo "This is an alpine package trigger"
# Pull the latest alpine image
docker pull alpine:${DIST_TAG}
# Determine the current tag
CURRENT_PACKAGE=$(docker run --rm alpine:${DIST_TAG} sh -c 'apk update --quiet --repository '"${DIST_REPO}"'\
&& apk info --repository '"${DIST_REPO}"' '"${DIST_REPO_PACKAGES}"' | md5sum | cut -c1-8')
# If the current tag does not match the external release then trigger a build
if [ "${CURRENT_PACKAGE}" != "${EXTERNAL_TAG}" ]; then
TRIGGER_REASON="An Alpine repo package change was detected for ${LS_REPO} old md5:${EXTERNAL_TAG} new md5:${CURRENT_PACKAGE}"
trigger_build
else
echo "Nothing to do release is up to date"
fi
fi