Skip to content

Commit

Permalink
Merge pull request #72 from docksal/develop
Browse files Browse the repository at this point in the history
Release 1.10.0
  • Loading branch information
lmakarov authored Apr 13, 2021
2 parents e579f33 + 0af235f commit 61ad481
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 102 deletions.
63 changes: 63 additions & 0 deletions .github/scripts/docker-tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

# Generates docker images tags for the docker/build-push-action@v2 action depending on the branch/tag.
# Image tag format:
# develop => image:[version-]edge[-flavor]
# master => image:[version][-][flavor]
# semver tag => image:[version-]major.minor[-flavor]

declare -a registryArr
registryArr+=("docker.io") # Docker Hub
registryArr+=("ghcr.io") # GitHub Container Registry

declare -a imageTagArr

# Join arguments with hyphen (-) as a delimiter
# Usage: join <arg1> [<argn>]
join() {
local IFS='-' # join delimiter
echo "$*"
}

# feature/* => sha-xxxxxxx
# Note: disabled
#if [[ "${GITHUB_REF}" =~ "refs/heads/feature/" ]]; then
# GIT_SHA7=$(echo ${GITHUB_SHA} | cut -c1-7) # Short SHA (7 characters)
# imageTagArr+=("${IMAGE}:${VERSION}-sha-${GIT_SHA7}")
#fi

# develop => version-edge
if [[ "${GITHUB_REF}" == "refs/heads/develop" ]]; then
tag=$(join ${VERSION} edge ${FLAVOR})
imageTagArr+=("${IMAGE}:${tag}")
fi

# master => version
if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
tag=$(join ${VERSION} ${FLAVOR})
imageTagArr+=("${IMAGE}:${tag}")
fi

# tags/v1.0.0 => 1.0
if [[ "${GITHUB_REF}" =~ "refs/tags/" ]]; then
# Extract version parts from release tag
IFS='.' read -a release_arr <<< "${GITHUB_REF#refs/tags/}"
releaseMajor=${release_arr[0]#v*} # 2.7.0 => "2"
releaseMinor=${release_arr[1]} # "2.7.0" => "7"
imageTagArr+=("${IMAGE}:$(join ${VERSION} ${FLAVOR})")
imageTagArr+=("${IMAGE}:$(join ${VERSION} ${releaseMajor} ${FLAVOR})")
imageTagArr+=("${IMAGE}:$(join ${VERSION} ${releaseMajor}.${releaseMinor} ${FLAVOR})")
fi

# Build an array of registry/image:tag values
declare -a repoImageTagArr
for registry in ${registryArr[@]}; do
for imageTag in ${imageTagArr[@]}; do
repoImageTagArr+=("${registry}/${imageTag}")
done
done

# Print with new lines for output in build logs
(IFS=$'\n'; echo "${repoImageTagArr[*]}")
# Using newlines in outputs variables does not seem to work, so we'll use comas
(IFS=$','; echo "::set-output name=tags::${repoImageTagArr[*]}")
170 changes: 170 additions & 0 deletions .github/workflows/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
name: Docker Build and Push

on:
schedule:
- cron: '0 10 * * 0' # everyday sunday at 10am
push:
branches:
- master
- develop
- feature/*
tags:
- 'v*.*.*'

defaults:
run:
shell: bash

jobs:
build-test-push:
name: Build, Test, Push
runs-on: ubuntu-20.04

env:
IMAGE: docksal/ci-agent

steps:
-
name: Install prerequisites for tests
run: |
set -xeuo pipefail
# Install bats for tests
git clone https://github.com/bats-core/bats-core.git
cd bats-core
sudo ./install.sh /usr/local
bats -v
-
name: Checkout
uses: actions/checkout@v2
#-
# name: Set up QEMU
# uses: docker/setup-qemu-action@v1
# buildx has some glitches with local upstream (FROM) images. Disabled.
#-
# name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v1
-
name: Check Docker
run: |
docker version
docker info
-
name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ secrets.GHCR_USERNAME }}
password: ${{ secrets.GHCR_TOKEN }}
-
# Calculates docker image tags for the given build context
# The output is used in build and push step as `tags: ${{ steps.docker_meta.outputs.tags }}`
# See https://github.com/crazy-max/ghaction-docker-meta
name: Docker meta
id: docker_meta
uses: crazy-max/ghaction-docker-meta@v1
with:
# List of Docker images to use as base name for tags
images: |
${{ env.IMAGE }}
ghcr.io/${{ env.IMAGE }}
tag-sha: true # add git short SHA as Docker tag
-
# Build for local use
name: Build image (base)
run: make build FLAVOR=base
-
# Build for local use
name: Build image (php)
run: make build FLAVOR=php
-
# Print image info
name: Docker image info
run: |
set -xeuo pipefail
docker image ls | grep "${{ env.IMAGE }}"
docker image inspect "${{ env.IMAGE }}:base-build"
docker image inspect "${{ env.IMAGE }}:php-build"
# Cache image layers in the registry
-
name: Push image cache (base)
uses: docker/build-push-action@v2
env:
IMAGE_CACHE: ghcr.io/${{ env.IMAGE }}:base-build
with:
context: base
file: base/Dockerfile
#platforms: linux/amd64,linux/arm64
tags: ${{ env.IMAGE_CACHE }} # Build cache tag in ghcr.io
push: ${{ github.event_name != 'pull_request' }} # Don't push for PRs
cache-to: type=inline # Write the cache metadata into the image configuration
-
name: Push image cache (php)
uses: docker/build-push-action@v2
env:
IMAGE_CACHE: ghcr.io/${{ env.IMAGE }}:php-build
with:
context: php
file: php/Dockerfile
#platforms: linux/amd64,linux/arm64
tags: ${{ env.IMAGE_CACHE }} # Build cache tag in ghcr.io
push: ${{ github.event_name != 'pull_request' }} # Don't push for PRs
cache-to: type=inline # Write the cache metadata into the image configuration

# Tests
-
name: Test image (base)
run: make test FLAVOR=base
-
name: Test image (php)
run: make test FLAVOR=php

-
# Generate image meta information
name: Docker image tags (base)
id: docker_tags_base
run: make tags FLAVOR=base
-
# Generate image meta information
name: Docker image tags (php)
id: docker_tags_php
run: make tags FLAVOR=php
-
# Push final image to the registry
# This will pick-up the build cache from the local build step
name: Push image (base)
# Don't run if the list of tags is empty
# Note: using tags from docker_tags (custom)
if: ${{ steps.docker_tags_base.outputs.tags != '' }}
uses: docker/build-push-action@v2
with:
context: base
file: base/Dockerfile
#platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_tags_base.outputs.tags }} # Note: using tags from docker_tags (custom script)
labels: ${{ steps.docker_meta.outputs.labels }} # Note: using lables from docker_meta
push: ${{ github.event_name != 'pull_request' }} # Don't push for PRs
cache-to: type=inline # Write the cache metadata into the image configuration

-
# Push final image to the registry
# This will pick-up the build cache from the local build step
name: Push image (php)
# Don't run if the list of tags is empty
# Note: using tags from docker_tags (custom)
if: ${{ steps.docker_tags_php.outputs.tags != '' }}
uses: docker/build-push-action@v2
with:
context: php
file: php/Dockerfile
#platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_tags_php.outputs.tags }} # Note: using tags from docker_tags (custom script)
labels: ${{ steps.docker_meta.outputs.labels }} # Note: using lables from docker_meta
push: ${{ github.event_name != 'pull_request' }} # Don't push for PRs
cache-to: type=inline # Write the cache metadata into the image configuration
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

27 changes: 15 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
-include env_make

VERSION ?= base
IMAGE ?= docksal/ci-agent
FLAVOR ?= base
BUILD_TAG ?= $(FLAVOR)-build

REPO = docksal/ci-agent
NAME = ci-agent
NAME = docksal-ci-agent-$(FLAVOR)

.EXPORT_ALL_VARIABLES:

.PHONY: build test push shell run start stop logs clean release

build:
docker build -t $(REPO):$(VERSION) $(VERSION)
docker build -t $(IMAGE):$(BUILD_TAG) ./$(FLAVOR)

test:
IMAGE=$(REPO):$(VERSION) NAME=$(NAME) tests/$(VERSION).bats
IMAGE=$(IMAGE) BUILD_TAG=$(BUILD_TAG) NAME=$(NAME) ./tests/$(FLAVOR).bats

push:
docker push $(REPO):$(VERSION)
docker push $(IMAGE):$(BUILD_TAG)

shell: clean
docker run --rm --name $(NAME) -it $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION) /bin/bash -oe pipefail
docker run --rm --name $(NAME) -it $(PORTS) $(VOLUMES) $(ENV) $(IMAGE):$(BUILD_TAG) /bin/bash

exec:
# Note: variables defined inside COMMAND get interpreted on the host, unless escaped, e.g. \$${CI_SSH_KEY}.
docker exec $(NAME) /bin/bash -oe pipefail -c "$(COMMAND)"

run: clean
docker run --rm --name $(NAME) $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION)
docker run --rm --name $(NAME) -it $(PORTS) $(VOLUMES) $(ENV) $(IMAGE):$(BUILD_TAG)

start: clean
docker run -d --name $(NAME) $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION) top -b
docker run -d --name $(NAME) $(PORTS) $(VOLUMES) $(ENV) $(IMAGE):$(BUILD_TAG) top -b

stop:
docker stop $(NAME)
Expand All @@ -36,9 +39,9 @@ logs:
docker logs $(NAME)

clean:
docker rm -f $(NAME) || true
docker rm -f $(NAME) >/dev/null 2>&1 || true

release: build
make push -e VERSION=$(VERSION)
tags:
@.github/scripts/docker-tags.sh

default: build
30 changes: 17 additions & 13 deletions base/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM alpine:3.9
FROM alpine:3.13

# Install basic pacakges
RUN set -xe; \
apk add --update --no-cache \
bash \
Expand All @@ -8,25 +9,28 @@ RUN set -xe; \
jq \
make \
openssh \
py2-pip \
rsync \
sudo \
patch \
; \
rm -rf /var/cache/apk/*;

ARG DOCKER_VERSION=18.09.2
ARG DOCKER_COMPOSE_VERSION=1.23.2
# Install docker packages
# Lookup available version for Alpine at
# https://pkgs.alpinelinux.org/packages?name=docker*&branch=v3.13&arch=x86_64
ARG DOCKER_VERSION=20.10.3-r1
ARG DOCKER_COMPOSE_VERSION=1.27.4-r0
RUN set -xe; \
# Install docker cli
curl -fsSL -O "https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz"; \
tar zxf docker-${DOCKER_VERSION}.tgz && mv docker/docker /usr/local/bin && rm -rf docker-${DOCKER_VERSION}*; \
docker --version; \
# Install docker-compose cli (has to be installed via pip on Alpine)
pip install "docker-compose==${DOCKER_COMPOSE_VERSION}" >/dev/null; \
docker-compose --version; \
# Install minio client (mc)
curl -fsSL https://dl.minio.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc; \
apk add --update --no-cache \
docker-cli=${DOCKER_VERSION} \
docker-compose=${DOCKER_COMPOSE_VERSION} \
; \
rm -rf /var/cache/apk/*;

# Install minio client (mc)
ARG MINIO_VERSION="RELEASE.2021-03-23T05-46-11Z"
RUN set -xe; \
curl -fsSL https://dl.min.io/client/mc/release/linux-amd64/archive/mc.${MINIO_VERSION} -o /usr/local/bin/mc; \
chmod +x /usr/local/bin/mc

ENV AGENT_USER=agent
Expand Down
Loading

0 comments on commit 61ad481

Please sign in to comment.