Skip to content

Commit ab71b5e

Browse files
committed
updating ci container to support non web containers and push to a latest tag and badge
1 parent 69e7d8a commit ab71b5e

File tree

4 files changed

+76
-25
lines changed

4 files changed

+76
-25
lines changed

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ RUN \
99
docker && \
1010
echo "**** Install python deps ****" && \
1111
pip install --no-cache-dir \
12+
requests \
1213
selenium \
1314
docker \
1415
boto3 \
16+
anybadge \
1517
jinja2 && \
1618
echo "**** cleanup ****" && \
1719
rm -rf \

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ The container can be run locally, but it is meant to be integrated into the Linu
3131
sudo docker run --rm -i \
3232
-v /var/run/docker.sock:/var/run/docker.sock \
3333
-e IMAGE="linuxserver/<dockerimage>" \
34-
-e DELAY_START=<time in seconds> \
3534
-e TAGS="<single tag or array seperated by |>" \
3635
-e META_TAG=<manifest main dockerhub tag> \
37-
-e PORT=<port web application listens on internal docker port> \
38-
-e SSL=<true/false> \
3936
-e BASE=<alpine or debian based distro> \
4037
-e SECRET_KEY=<Digital Ocean spaces secret> \
4138
-e ACCESS_KEY=<Digital Ocean spaces key> \
@@ -44,6 +41,10 @@ sudo docker run --rm -i \
4441
-e WEB_PATH="<optional, format /yourpath>" \
4542
-e DO_REGION=<optional, custom Digital Ocean Region> \
4643
-e DO_BUCKET=<optional, custom Digital Ocean Bucket> \
44+
-e WEB_SCREENSHOT=<optional, set to false if not a web app> \
45+
-e DELAY_START=<optional, time in seconds to delay before taking screenshot> \
46+
-e PORT=<optional, port web application listens on internal docker port> \
47+
-e SSL=<optional , use ssl for the screenshot true/false> \
4748
-t lsiodev/ci:latest \
4849
python /ci/ci.py
4950
```

ci/ci.py

+67-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import time
66
import sys
77
import docker
8+
import requests
9+
import anybadge
810
from selenium import webdriver
911
from selenium.common.exceptions import ErrorInResponseException,TimeoutException
1012
from jinja2 import Template
@@ -66,6 +68,10 @@ def convert_env(vars):
6668
global dockerenv
6769
global region
6870
global bucket
71+
global screenshot
72+
global port
73+
global ssl
74+
global testdelay
6975
try:
7076
webauth = os.environ["WEB_AUTH"]
7177
except KeyError:
@@ -86,24 +92,33 @@ def convert_env(vars):
8692
bucket = os.environ["DO_BUCKET"]
8793
except KeyError:
8894
bucket = 'lsio-ci'
89-
95+
try:
96+
screenshot = os.environ["WEB_SCREENSHOT"]
97+
except KeyError:
98+
screenshot = 'false'
99+
try:
100+
port = os.environ["PORT"]
101+
except KeyError:
102+
port = '80'
103+
try:
104+
ssl = os.environ["SSL"]
105+
except KeyError:
106+
ssl = 'false'
107+
try:
108+
testdelay = os.environ["DELAY_START"]
109+
except KeyError:
110+
testdelay = '5'
90111

91112
# Make sure all needed env variables are set
92113
def check_env():
93114
try:
94115
global image
95-
global testdelay
96116
global tags
97117
global meta_tag
98-
global port
99-
global ssl
100118
global base
101119
global spaces_key
102120
global spaces_secret
103121
image = os.environ["IMAGE"]
104-
testdelay = os.environ["DELAY_START"]
105-
port = os.environ["PORT"]
106-
ssl = os.environ["SSL"]
107122
base = os.environ["BASE"]
108123
spaces_key = os.environ["ACCESS_KEY"]
109124
spaces_secret = os.environ["SECRET_KEY"]
@@ -130,9 +145,13 @@ def create_dir():
130145
# Take a screenshot using the webdriver
131146
def take_screenshot(endpoint,container_tag):
132147
try:
148+
requests.get(endpoint, timeout=3)
133149
driver.get(endpoint)
134150
driver.get_screenshot_as_file(outdir + container_tag + '.png')
135151
report_tests.append(['Screenshot ' + container_tag,'PASS'])
152+
except (requests.Timeout, requests.ConnectionError, KeyError) as e:
153+
report_tests.append(['Screenshot ' + container_tag,'FAIL CONNECTION ERROR'])
154+
mark_fail()
136155
except ErrorInResponseException as error:
137156
report_tests.append(['Screenshot ' + container_tag,'FAIL SERVER ERROR'])
138157
mark_fail()
@@ -167,16 +186,17 @@ def container_test(tag):
167186
elif logsfound == False:
168187
report_tests.append(['Startup ' + tag,'FAIL INIT NOT FINISHED'])
169188
mark_fail()
170-
# Sleep for the user specified amount of time
171-
time.sleep(int(testdelay))
172-
# Take a screenshot
173-
if ssl == 'true':
174-
proto = 'https://'
175-
else:
176-
proto = 'http://'
177-
container.reload()
178-
ip = container.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
179-
take_screenshot(proto + webauth + '@' + ip + ':' + port + webpath ,tag)
189+
if screenshot == 'true':
190+
# Sleep for the user specified amount of time
191+
time.sleep(int(testdelay))
192+
# Take a screenshot
193+
if ssl == 'true':
194+
proto = 'https://'
195+
else:
196+
proto = 'http://'
197+
container.reload()
198+
ip = container.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
199+
take_screenshot(proto + webauth + '@' + ip + ':' + port + webpath ,tag)
180200
# Dump package information
181201
if base == 'alpine':
182202
command = 'apk info -v'
@@ -220,13 +240,23 @@ def report_render():
220240
meta_tag=meta_tag,
221241
image=image,
222242
bucket=bucket,
223-
region=region)
243+
region=region,
244+
screenshot=screenshot)
224245
with open(outdir + 'report.md', 'w') as f:
225246
f.write(markdown)
226247

248+
# Render the markdown file for upload
249+
def badge_render():
250+
try:
251+
badge = anybadge.Badge('CI', report_status, thresholds={'PASS': 'green', 'FAIL': 'red'})
252+
badge.write_badge(outdir + 'badge.svg')
253+
except Exception as error:
254+
print(error)
255+
227256
# Upload report to DO Spaces
228257
def report_upload():
229258
destination_dir = image + '/' + meta_tag + '/'
259+
latest_dir = image + '/latest/'
230260
spaces = session.client(
231261
's3',
232262
region_name=region,
@@ -241,16 +271,33 @@ def report_upload():
241271
bucket,
242272
destination_dir + 'index.html',
243273
ExtraArgs={'ContentType': "text/html", 'ACL': "public-read"})
274+
spaces.upload_file(
275+
index_file,
276+
bucket,
277+
latest_dir + 'index.html',
278+
ExtraArgs={'ContentType': "text/html", 'ACL': "public-read"})
244279
except Exception as error:
245280
core_fail('Upload Error ' + str(error))
246281
# Loop for all others
247282
for filename in os.listdir(outdir):
283+
# Set content types for files
284+
if filename.lower().endswith('.svg'):
285+
CT = 'image/svg+xml'
286+
elif filename.lower().endswith('.png'):
287+
CT = 'image/png'
288+
elif filename.lower().endswith('.md'):
289+
CT = 'text/markdown'
248290
try:
249291
spaces.upload_file(
250292
outdir + filename,
251293
bucket,
252294
destination_dir + filename,
253-
ExtraArgs={'ACL': "public-read"})
295+
ExtraArgs={'ContentType': CT,'ACL': "public-read"})
296+
spaces.upload_file(
297+
outdir + filename,
298+
bucket,
299+
latest_dir + filename,
300+
ExtraArgs={'ContentType': CT,'ACL': "public-read"})
254301
except Exception as error:
255302
core_fail('Upload Error ' + str(error))
256303

@@ -266,6 +313,7 @@ def report_upload():
266313
# Quit selenium webdriver
267314
driver.quit()
268315
report_render()
316+
badge_render()
269317
report_upload()
270318
# Exit based on test results
271319
if report_status == 'pass':

ci/results.template

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
## {{ image }}:{{ container["tag"] }}
1111
### Build Version: {{ container["build_version"] }}
1212

13-
### Screenshot
14-
15-
[![{{ container["tag"] }}](https://{{ bucket }}.{{ region }}.digitaloceanspaces.com/{{ image }}/{{ meta_tag }}/{{ container["tag"] }}.png =600x*)](https://{{ bucket }}.{{ region }}.digitaloceanspaces.com/{{ image }}/{{ meta_tag }}/{{ container["tag"] }}.png)
13+
{% if screenshot == 'true' %}### Screenshot
1614

15+
[![{{ container["tag"] }}]({{ container["tag"] }}.png =600x*)]({{ container["tag"] }}.png)
16+
{% endif %}
1717
### Logs
1818

1919
<details><summary>Expand</summary>

0 commit comments

Comments
 (0)