Skip to content

Commit

Permalink
support for automated builds
Browse files Browse the repository at this point in the history
  • Loading branch information
andyshinn committed Feb 22, 2015
1 parent 0ee02e1 commit 9a4de21
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootfs.tar.xz
images
/images
/**/rootfs.tar.xz
3 changes: 0 additions & 3 deletions Dockerfile

This file was deleted.

69 changes: 48 additions & 21 deletions build
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash

declare BUILD_MASTER="${BUILD_MASTER}"
declare BUILD_IMAGE="${BUILD_IMAGE:-alpine-builder}"
declare BUILD_PREFIX="${BUILD_PREFIX:-alpine-build-}"
declare PREFIXES="${PREFIXES:-gliderlabs/alpine}"
declare MIRROR="${MIRROR:-http://dl-4.alpinelinux.org/alpine}"
declare VERSIONS="${VERSIONS:-versions/*}"
declare VERSIONS="${VERSIONS:-versions/**/tags}"

build() {
declare version_files="${*:-$VERSIONS}"
: "${version_files:?}"
declare tag_files="${*:-$VERSIONS}"
: "${tag_files:?}"

if [[ -z $(which docker) ]]; then
echo "Missing docker client which is required for building"
Expand All @@ -17,42 +18,67 @@ build() {

docker build -t "$BUILD_IMAGE" builder

for file in $version_files; do
for file in $tag_files; do
local tags="$(cat "$file")"
local release="$(basename "$file")"
local release="$(basename "$(dirname "$file")")"
local build="${BUILD_PREFIX}${release}"
local master="$(echo "$PREFIXES" | cut -d' ' -f1):master"
local version_dir="$(dirname "$file")"

: "${build:?}" "${tags:?}" "${release:?}"

docker rm "$build" 2>/dev/null || true

docker run --name "$build" "$BUILD_IMAGE" \
-s \
-c \
-r "$release" \
-m "$MIRROR"
docker cp "$build":/rootfs.tar.xz .
docker build -t "$master" .

for tag in $tags; do
for prefix in $PREFIXES; do
docker tag -f "$master" "${prefix}:${tag}"
if [[ "$CIRCLE_BUILD_NUM" ]]; then
mkdir -p images
docker save "${prefix}:${tag}" \
| gzip -c > "images/${prefix//\//_}_${tag}_build-${CIRCLE_BUILD_NUM}.tar.gz"
fi
docker cp "$build":/rootfs.tar.xz "./$version_dir/"

if [[ "$BUILD_MASTER" ]]; then
docker build -t "$master" "./$version_dir/"

for tag in $tags; do
for prefix in $PREFIXES; do
docker tag -f "$master" "${prefix}:${tag}"
if [[ "$CIRCLE_BUILD_NUM" ]]; then
mkdir -p images
docker save "${prefix}:${tag}" \
| gzip -c > "images/${prefix//\//_}_${tag}_build-${CIRCLE_BUILD_NUM}.tar.gz"
fi
done
done
done
docker rmi "$master" || true
fi

docker rm "$build" || true
docker rmi "$master" || true
rm -f rootfs.tar.xz
done
}

commit() {
declare tag_files="${*:-$VERSIONS}"
local build_num="${CIRCLE_BUILD_NUM:-nobuild}"

for file in $tag_files; do
local release="$(basename "$(dirname "$file")")"
local version_dir="$(dirname "$file")"
local current_branch=$(git rev-parse --abbrev-ref HEAD)

: "${release:?}"

git checkout -B "rootfs-$release"
git add -f -- "./$version_dir/rootfs.tar.xz"
git commit -m "pushing release $release for build $build_num"
git push -f origin "rootfs-$release"
git checkout "$current_branch"
done
}

test() {
declare version_files="${*:-$VERSIONS}"
declare tag_files="${*:-$VERSIONS}"
local repo="$(echo "$PREFIXES" | cut -d' ' -f1)"
for file in $version_files; do
for file in $tag_files; do
local tag="$(head -1 "$file")"
if docker inspect "${repo}:${tag}" >/dev/null 2>&1; then
docker run "${repo}:${tag}" apk add --update openssl || exit 1
Expand All @@ -65,6 +91,7 @@ main() {
cmd="$1"
case "$cmd" in
test) shift; test "$@";;
commit) shift; commit "$@";;
*) build "$@";;
esac
}
Expand Down
3 changes: 2 additions & 1 deletion builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN apt-get update \
&& apt-get clean \
&& rm -rf /var/lib/{dpkg,apt,cache,log}

ADD ./mkimage-alpine.sh /mkimage.sh
COPY scripts/mkimage-alpine.sh /mkimage.sh
COPY scripts/apk-install /apk-install

ENTRYPOINT ["/mkimage.sh"]
13 changes: 13 additions & 0 deletions builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Alpine Linux rootfs Builder

This builder image constructs a Alpine Linux `rootfs.tar.xz` for us to use when building the base Alpine Linux image. The `mkimage-alpine.sh` script does all the heavy lifting. During the configuration of the image we add our `apk-install` convenience script.

## Options

The builder takes several options:

* `-r <release>`: The release tag to use (such as `edge` or `v3.1`).
* `-m <mirror>`: The mirror URL base. Defaults to `http://nl.alpinelinux.org/alpine`.
* `-s`: Saves the rootfs to `/rootfs.tar.xz` so it can be later copied.
* `-c`: Adds the `apk-install` script to the resulting rootfs.
* `-e`: Adds extra `edge/main` and `edge/testing` pins to the repositories file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set -e
}

usage() {
printf >&2 '%s: [-r release] [-m mirror] [-s] [-e]\n' "$0"
printf >&2 '%s: [-r release] [-m mirror] [-s] [-e] [-c]\n' "$0"
exit 1
}

Expand Down Expand Up @@ -45,6 +45,7 @@ conf() {
[ $REL = "edge" ] || printf '%s\n' "@edge $MIRROR/edge/main" >> $ROOTFS/etc/apk/repositories
printf '%s\n' "@testing $MIRROR/edge/testing" >> $ROOTFS/etc/apk/repositories
}
[ $ADD_APK_SCRIPT -eq 1 ] && cp /apk-install $ROOTFS/usr/sbin/apk-install
rm -f $ROOTFS/var/cache/apk/*
}

Expand All @@ -54,7 +55,7 @@ save() {
tar --numeric-owner -C $ROOTFS -c . | xz > rootfs.tar.xz
}

while getopts "hr:m:se" opt; do
while getopts "hr:m:sec" opt; do
case $opt in
r)
REL=$OPTARG
Expand All @@ -68,6 +69,9 @@ while getopts "hr:m:se" opt; do
e)
REPO_EXTRA=1
;;
c)
ADD_APK_SCRIPT=1
;;
*)
usage
;;
Expand All @@ -77,6 +81,7 @@ done
REL=${REL:-edge}
MIRROR=${MIRROR:-http://nl.alpinelinux.org/alpine}
SAVE=${SAVE:-0}
ADD_APK_SCRIPT=${ADD_APK_SCRIPT:-0}
REPO=$MIRROR/$REL/main
REPO_EXTRA=${REPO_EXTRA:-0}
ARCH=$(uname -m)
Expand Down
7 changes: 6 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
general:
artifacts:
- "images"
branches:
ignore:
- /rootfs-.*/

machine:
services:
Expand All @@ -15,7 +18,7 @@ dependencies:
- ./build:
parallel: true
files:
- versions/*
- versions/**/tags
- mkdir -p ~/docker; docker save alpine-builder > ~/docker/builder.tar
- docker images

Expand All @@ -32,3 +35,5 @@ deployment:
parallel: true
- docker push gliderlabs/alpine:
parallel: true
- ./build commit:
parallel: true
2 changes: 1 addition & 1 deletion docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A convenience `build` script is included that builds the image and runs basic te

The image is built using a builder Docker container based on the `debian` image. This builder image lives in the `builder` sub-directory of the project and uses a `mkimage-alpine.sh` script to generate a Alpine Linux `rootfs.tar.gz` file. This file then gets copied to the root of the project so we can build the main Alpine Linux image by just using the `ADD` command to automatically untar the components to the resulting image.

The build script takes a glob of files as an argument. Each of these files describes the version of Alpine Linux to build in the file name and each line of the file are the tags that will be applied to the resulting image. By default, we use the included files in the `versions` directory.
The build script takes a glob of `tags` files as an argument. Each of these files lives in a folder that describes the version of Alpine Linux to build in the parent directory name and each line of the file are the tags that will be applied to the resulting image. By default, we use the included glob of `versions/**/tags`.

## Test

Expand Down
2 changes: 2 additions & 0 deletions versions/edge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ADD rootfs.tar.xz /
File renamed without changes.
2 changes: 2 additions & 0 deletions versions/v2.6/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ADD rootfs.tar.xz /
File renamed without changes.
2 changes: 2 additions & 0 deletions versions/v2.7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ADD rootfs.tar.xz /
File renamed without changes.
2 changes: 2 additions & 0 deletions versions/v3.1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ADD rootfs.tar.xz /
File renamed without changes.

0 comments on commit 9a4de21

Please sign in to comment.