Skip to content

Commit b118cd5

Browse files
committed
Building the Docker image with Github Actions
1 parent 7863e59 commit b118cd5

File tree

11 files changed

+164
-117
lines changed

11 files changed

+164
-117
lines changed

.github/workflows/push.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
on:
2+
push:
3+
branches-ignore:
4+
- release
5+
6+
jobs:
7+
build:
8+
strategy:
9+
matrix:
10+
build_cmd:
11+
- ./build-latest.sh
12+
- PRERELEASE=true ./build-latest.sh
13+
- ./build-branches.sh
14+
docker_from:
15+
- '' # use the default of the DOCKERFILE
16+
- python:3.7-alpine
17+
- python:3.8-alpine
18+
- python:3.9-rc-alpine
19+
fail-fast: false
20+
runs-on: ubuntu-latest
21+
name: Builds new Netbox Docker Images
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v1
25+
- name: Build the image from '${{ matrix.docker_from }}' with '${{ matrix.build_cmd }}'
26+
run: ${{ matrix.build_cmd }}
27+
env:
28+
DOCKER_FROM: ${{ matrix.docker_from }}
29+
GH_ACTION: enable
30+
- name: Test the image
31+
run: IMAGE="${FINAL_DOCKER_TAG}" ./test.sh

.github/workflows/release.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
on:
2+
push:
3+
branches:
4+
- release
5+
schedule:
6+
- cron: '45 5 * * *'
7+
8+
jobs:
9+
build:
10+
strategy:
11+
matrix:
12+
build_cmd:
13+
- ./build-latest.sh
14+
- PRERELEASE=true ./build-latest.sh
15+
- ./build-branches.sh
16+
fail-fast: false
17+
runs-on: ubuntu-latest
18+
name: Builds new Netbox Docker Images
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v1
22+
- name: Build the image with '${{ matrix.build_cmd }}'
23+
run: ${{ matrix.build_cmd }}
24+
env:
25+
GH_ACTION: enable
26+
- name: Test the image
27+
run: IMAGE="${FINAL_DOCKER_TAG}" ./test.sh
28+
- name: Login to the Docker Registry
29+
run: |
30+
echo "::add-mask::$DOCKERHUB_USERNAME"
31+
echo "::add-mask::$DOCKERHUB_PASSWORD"
32+
docker login -u "$DOCKERHUB_USERNAME" --password "${DOCKERHUB_PASSWORD}" "${DOCKER_REGISTRY}"
33+
env:
34+
DOCKERHUB_USERNAME: ${{ secrets.dockerhub_username }}
35+
DOCKERHUB_PASSWORD: ${{ secrets.dockerhub_password }}
36+
- name: Push the image
37+
run: ${{ matrix.build_cmd }} --push-only
38+
- name: Logout of the Docker Registry
39+
run: docker logout "${DOCKER_REGISTRY}"

build.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
6363
echo " Default: undefined"
6464
echo " DRY_RUN Prints all build statements instead of running them."
6565
echo " Default: undefined"
66+
echo " GH_ACTION If defined, special 'echo' statements are enabled that set the"
67+
echo " following environment variables in Github Actions:"
68+
echo " - FINAL_DOCKER_TAG: The final value of the DOCKER_TAG env variable"
69+
echo " Default: undefined"
6670
echo ""
6771
echo "Examples:"
6872
echo " ${0} master"
@@ -92,7 +96,7 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
9296
fi
9397

9498
###
95-
# Determining the build command to use
99+
# Enabling dry-run mode
96100
###
97101
if [ -z "${DRY_RUN}" ]; then
98102
DRY=""
@@ -102,7 +106,7 @@ else
102106
fi
103107

104108
###
105-
# variables for fetching the source
109+
# Variables for fetching the source
106110
###
107111
SRC_ORG="${SRC_ORG-netbox-community}"
108112
SRC_REPO="${SRC_REPO-netbox}"
@@ -111,7 +115,7 @@ URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}.git}"
111115
NETBOX_PATH="${NETBOX_PATH-.netbox}"
112116

113117
###
114-
# fetching the source
118+
# Fetching the source
115119
###
116120
if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ] ; then
117121
echo "🌐 Checking out '${NETBOX_BRANCH}' of netbox from the url '${URL}' into '${NETBOX_PATH}'"
@@ -150,15 +154,15 @@ if [ ! -f "${DOCKERFILE}" ]; then
150154
fi
151155

152156
###
153-
# variables for labelling the docker image
157+
# Variables for labelling the docker image
154158
###
155159
BUILD_DATE="$(date -u '+%Y-%m-%dT%H:%M+00:00')"
156160

157161
if [ -d ".git" ]; then
158162
GIT_REF="$(git rev-parse HEAD)"
159163
fi
160164

161-
# read the project version from the `VERSION` file and trim it, see https://stackoverflow.com/a/3232433/172132
165+
# Read the project version from the `VERSION` file and trim it, see https://stackoverflow.com/a/3232433/172132
162166
PROJECT_VERSION="${PROJECT_VERSION-$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' VERSION)}"
163167

164168
# Get the Git information from the netbox directory
@@ -169,7 +173,7 @@ if [ -d "${NETBOX_PATH}/.git" ]; then
169173
fi
170174

171175
###
172-
# variables for tagging the docker image
176+
# Variables for tagging the docker image
173177
###
174178
DOCKER_REGISTRY="${DOCKER_REGISTRY-docker.io}"
175179
DOCKER_ORG="${DOCKER_ORG-netboxcommunity}"
@@ -193,6 +197,7 @@ echo "🏭 Building the following targets:" "${DOCKER_TARGETS[@]}"
193197
###
194198
# Build each target
195199
###
200+
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT-1}
196201
for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
197202
echo "🏗 Building the target '${DOCKER_TARGET}'"
198203

@@ -203,6 +208,9 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
203208
if [ "${DOCKER_TARGET}" != "main" ]; then
204209
TARGET_DOCKER_TAG="${TARGET_DOCKER_TAG}-${DOCKER_TARGET}"
205210
fi
211+
if [ -n "${GH_ACTION}" ]; then
212+
echo "::set-env name=FINAL_DOCKER_TAG::${TARGET_DOCKER_TAG}"
213+
fi
206214

207215
###
208216
# composing the additional DOCKER_SHORT_TAG,

docker-compose.test.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
version: '3.4'
2+
services:
3+
netbox:
4+
image: ${IMAGE-netboxcommunity/netbox:latest}
5+
depends_on:
6+
- postgres
7+
- redis
8+
env_file: env/netbox.env
9+
user: '101'
10+
volumes:
11+
- ./startup_scripts:/opt/netbox/startup_scripts:z,ro
12+
- ./initializers:/opt/netbox/initializers:z,ro
13+
- ./configuration:/etc/netbox/config:z,ro
14+
- ./reports:/etc/netbox/reports:z,ro
15+
- ./scripts:/etc/netbox/scripts:z,ro
16+
- netbox-nginx-config:/etc/netbox-nginx:z
17+
- netbox-static-files:/opt/netbox/netbox/static:z
18+
- netbox-media-files:/opt/netbox/netbox/media:z
19+
nginx:
20+
command: nginx -c /etc/netbox-nginx/nginx.conf
21+
image: nginx:1.17-alpine
22+
depends_on:
23+
- netbox
24+
ports:
25+
- 8080
26+
volumes:
27+
- netbox-static-files:/opt/netbox/netbox/static:ro
28+
- netbox-nginx-config:/etc/netbox-nginx/:ro
29+
postgres:
30+
image: postgres:11-alpine
31+
env_file: env/postgres.env
32+
redis:
33+
image: redis:5-alpine
34+
command:
35+
- sh
36+
- -c # this is to evaluate the $REDIS_PASSWORD from the env
37+
- redis-server --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose
38+
env_file: env/redis.env
39+
volumes:
40+
netbox-static-files:
41+
driver: local
42+
netbox-nginx-config:
43+
driver: local
44+
netbox-media-files:
45+
driver: local

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ volumes:
5757
driver: local
5858
netbox-media-files:
5959
driver: local
60-
netbox-report-files:
61-
driver: local
6260
netbox-postgres-data:
6361
driver: local
6462
netbox-redis-data:

docker/docker-entrypoint.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
#!/bin/bash
2+
# Runs on every start of the Netbox Docker container
3+
4+
# Stop when an error occures
25
set -e
6+
7+
# Allows Netbox to be run as non-root users
38
umask 002
49

5-
# wait shortly and then run db migrations (retry on error)
6-
while ! ./manage.py migrate 2>&1; do
7-
echo "⏳ Waiting on DB..."
8-
sleep 3
10+
# Try to connect to the DB
11+
DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3}
12+
MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30}
13+
CUR_DB_WAIT_TIME=0
14+
while ! ./manage.py migrate 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do
15+
echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
16+
sleep "${DB_WAIT_TIMEOUT}"
17+
CUR_DB_WAIT_TIME=$(( CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT ))
918
done
19+
if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then
20+
echo "❌ Waited ${MAX_DB_WAIT_TIME}s or more for the DB to become ready."
21+
exit 1
22+
fi
1023

24+
# Create Superuser if required
1125
if [ "$SKIP_SUPERUSER" == "true" ]; then
1226
echo "↩️ Skip creating the superuser"
1327
else
@@ -43,18 +57,19 @@ END
4357
echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
4458
fi
4559

60+
# Run the startup scripts (and initializers)
4661
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
4762
echo "↩️ Skipping startup scripts"
4863
else
4964
echo "import runpy; runpy.run_path('../startup_scripts')" | ./manage.py shell --interface python
5065
fi
5166

52-
# copy static files
67+
# Copy static files
5368
./manage.py collectstatic --no-input
5469

5570
echo "✅ Initialisation is done."
5671

57-
# launch whatever is passed by docker
72+
# Launch whatever is passed by docker
5873
# (i.e. the RUN instruction in the Dockerfile)
5974
#
6075
# shellcheck disable=SC2068

hooks/build

Lines changed: 0 additions & 6 deletions
This file was deleted.

hooks/common

Lines changed: 0 additions & 60 deletions
This file was deleted.

hooks/push

Lines changed: 0 additions & 14 deletions
This file was deleted.

hooks/test

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)