|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | set -o errexit |
4 | | -set -o nounset |
5 | 4 | set -o pipefail |
6 | 5 | set -o xtrace |
7 | 6 |
|
8 | | -ADD_DATA_PART=${ADD_DATA_PART:-false} |
9 | | - |
10 | | -# These are the env vars that should be passed in from docker |
11 | | -echo "MOUNT: $MOUNT" |
12 | | -echo "SOURCE_IMAGE: $SOURCE_IMAGE" |
13 | | -echo "SCRIPT: $SCRIPT" |
14 | | -echo "ADD_DATA_PART: $ADD_DATA_PART" |
15 | | - |
16 | | -# If ADD_DATA_PART is true then add a data partiton |
17 | | -if [ $ADD_DATA_PART != false ]; then |
18 | | - source ./add-partition.sh $SOURCE_IMAGE |
19 | | - # The add-partition script runs mount |
| 7 | +if [ ! -z "$TRAVIS_BRANCH" ]; then |
| 8 | + # For tag builds TRAVIS_BRANCH is set to the tag name |
| 9 | + BRANCH=$TRAVIS_BRANCH |
| 10 | + # For PR builds branch is the target branch |
| 11 | + if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then |
| 12 | + # Change the branch name for PR builds so we don't create a tag |
| 13 | + BRANCH="${BRANCH}-${TRAVIS_PULL_REQUEST}" |
| 14 | + fi |
| 15 | + # Only Travis should push images, so we only need to use the tag with Travis |
| 16 | + TAG=$TRAVIS_TAG |
| 17 | +elif [ ! -z "$CI_COMMIT_REF_NAME" ]; then |
| 18 | + # This is also run in GitLab CI, for build and test only. |
| 19 | + BRANCH="$CI_COMMIT_REF_NAME" |
20 | 20 | else |
21 | | - # Or mount is run here directly |
22 | | - source ./mount.sh $SOURCE_IMAGE |
| 21 | + exit 1 |
23 | 22 | fi |
24 | 23 |
|
25 | | -# The add-partition or mount scripts must set LOOP_DEV and ROOTFS_DIR |
26 | | -echo "LOOP_DEV: $LOOP_DEV" |
27 | | -echo "ROOTFS_DIR: $ROOTFS_DIR" |
28 | | - |
29 | | -# Bind mount the directory specified as MOUNT. This is for scripts to run |
30 | | -# inside Raspbian and data to copy in |
31 | | -mkdir ${ROOTFS_DIR}/${MOUNT} |
32 | | -mount --bind $MOUNT ${ROOTFS_DIR}/${MOUNT} |
33 | | - |
34 | | -# Apply ld.so.preload fix |
35 | | -sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload |
36 | | - |
37 | | -# Copy qemu binary |
38 | | -cp /usr/bin/qemu-arm-static ${ROOTFS_DIR}/usr/bin/ |
39 | | - |
40 | | -# Enable qemu-arm |
41 | | -update-binfmts --enable qemu-arm |
42 | | - |
43 | | -# Chroot to the mounted Raspbian environment and run the SCRIPT |
44 | | -chroot ${ROOTFS_DIR} $SCRIPT |
45 | | - |
46 | | -# Revert ld.so.preload fix |
47 | | -sed -i 's/^#CHROOT //g' ${ROOTFS_DIR}/etc/ld.so.preload |
| 24 | +# Set TAG to false if unset |
| 25 | +TAG="${TAG:-false}" |
| 26 | +# Delay setting this until all used variables are set |
| 27 | +set -o nounset |
48 | 28 |
|
49 | | -# Unmount everything |
50 | | -if [ $ADD_DATA_PART != false ]; then |
51 | | - umount ${ROOTFS_DIR}/data |
| 29 | +echo "BUILD - Will now build Docker container" |
| 30 | +docker build -t lumastar/raspbian-customiser:$BRANCH . |
| 31 | + |
| 32 | +echo "TEST - Will now test built Docker container" |
| 33 | +IMAGE_LINK=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-10-11/2018-10-09-raspbian-stretch-lite.zip |
| 34 | +cd test/ |
| 35 | +wget -nv $IMAGE_LINK |
| 36 | +IMAGE_ZIP=$(basename $IMAGE_LINK) |
| 37 | +unzip -o $IMAGE_ZIP |
| 38 | +rm $IMAGE_ZIP |
| 39 | +cd .. |
| 40 | +docker run --privileged --rm \ |
| 41 | + -e MOUNT=/test \ |
| 42 | + -e SOURCE_IMAGE=/test/${IMAGE_ZIP%.zip}.img \ |
| 43 | + -e SCRIPT=/test/test.sh \ |
| 44 | + -e ADD_DATA_PART=true \ |
| 45 | + --mount type=bind,source="$(pwd)"/test,destination=/test \ |
| 46 | + lumastar/raspbian-customiser:$BRANCH |
| 47 | + |
| 48 | +if [ "$TAG" != "false" ]; then |
| 49 | + # Only push image if TAG is not false |
| 50 | + echo "DEPLOY - Will now push Docker image to Quay.io repository as $TAG" |
| 51 | + echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin quay.io |
| 52 | + docker tag lumastar/raspbian-customiser:$BRANCH quay.io/lumastar/raspbian-customiser:$TAG |
| 53 | + docker push quay.io/lumastar/raspbian-customiser:$TAG |
52 | 54 | fi |
53 | | -umount ${ROOTFS_DIR}/{dev/pts,dev,sys,proc,boot,${MOUNT},} |
54 | | -losetup -d $LOOP_DEV |
55 | | -echo "SUCCESSFULLY UNMOUNTED IMG" |
0 commit comments