Skip to content

Commit

Permalink
m: add registry handling and ci/cd
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesjardins90 committed Jun 22, 2024
1 parent 7d1484f commit bdaf808
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 27 deletions.
File renamed without changes.
35 changes: 35 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: cd

on:
push:
tags:
- "[0-9]+.[0-9]+.[0-9]+"

env:
DOCKER_BUILDKIT: true
DOCKER_ENV_REGISTRY: ghcr.io/logisparte

permissions:
packages: write # Required to push docker image to registry

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Clone project
uses: actions/checkout@v4

- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_ENV_REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Pull dev image
run: ./docker/env.sh pull ${{ github.sha }}

- name: Tag dev image with version
run: |
./docker/env.sh tag ${{ github.ref_name }}
./docker/env.sh push ${{ github.ref_name }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:

env:
DOCKER_BUILDKIT: true
DOCKER_ENV_REGISTRY: ghcr.io/logisparte

jobs:
check-branch:
Expand All @@ -15,14 +16,21 @@ jobs:
- name: Clone project
uses: actions/checkout@v4

- name: Initialize development environment
- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_ENV_REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Initialize docker env
run: ./docker/env.sh init

- name: Start development environment
- name: Start docker env
run: ./docker/env.sh up

- name: Lint project
run: ./docker/env.sh exec ./scripts/lint.sh all

- name: Stop development environment
- name: Stop docker env
run: ./docker/env.sh down
36 changes: 36 additions & 0 deletions .github/workflows/ci-master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: ci-master

on:
push:
branches:
- master

env:
DOCKER_BUILDKIT: true
DOCKER_ENV_REGISTRY: ghcr.io/logisparte

permissions:
packages: write # Required to push docker image to registry

jobs:
update-dev-image:
runs-on: ubuntu-latest
steps:
- name: Clone project
uses: actions/checkout@v4

- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_ENV_REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Build new dev image
run: ./docker/env.sh build

- name: Tag new dev image
run: |
./docker/env.sh tag ${{ github.sha }}
./docker/env.sh push ${{ github.sha }}
./docker/env.sh push
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# syntax=docker/dockerfile:1
FROM ubuntu:24.04 AS base

LABEL org.opencontainers.image.authors="@logisparte"
LABEL org.opencontainers.image.source="https://github.com/logisparte/docker-env"
LABEL org.opencontainers.image.title="docker-env-dev" \
org.opencontainers.image.description="Dev image of docker-env" \
org.opencontainers.image.authors="@logisparte" \
org.opencontainers.image.source="https://github.com/logisparte/docker-env" \
org.opencontainers.image.licenses="Apache-2.0"

RUN <<EOF
DEBIAN_FRONTEND=noninteractive \
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# docker-env

Containerized development environments
Encapsulate your project's development environment inside a Docker container

## License

Expand Down
2 changes: 1 addition & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ FILES="$({
done
})"

PRETTIER_FILES="$(echo "$FILES" | grep -e "\.md$" -e "\.yml$" -e "\.yaml$" || true)"
PRETTIER_FILES="$(echo "$FILES" | grep -e "\.md$" -e "\.yaml$" || true)"
if [ -n "$PRETTIER_FILES" ]; then
report --info "Markdown and yaml files >>"
echo "$PRETTIER_FILES" | xargs prettier --write
Expand Down
10 changes: 3 additions & 7 deletions src/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
#

services:
dev-env:
labels:
org.opencontainers.image.title: $COMPOSE_PROJECT_NAME-dev-env
org.opencontainers.image.description:
Development environment image of $COMPOSE_PROJECT_NAME
image: $COMPOSE_PROJECT_NAME-dev-env
dev:
image: $IMAGE_NAME
build:
context: $PWD
dockerfile: $PWD/docker/Dockerfile
environment:
DOCKER_ENV_CURRENT: $COMPOSE_PROJECT_NAME
DOCKER_ENV_CURRENT: $IMAGE_NAME
SSH_AUTH_SOCK: $HOST_SSH_AUTH_SOCK
TERM:
CI:
Expand Down
65 changes: 52 additions & 13 deletions src/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
# limitations under the License.
#

COMPOSE_PROJECT_NAME="$(basename "$PWD")"
export COMPOSE_PROJECT_NAME
PROJECT_NAME="$(basename "$PWD")"
export IMAGE_NAME="$PROJECT_NAME-dev"
if [ -n "$DOCKER_ENV_REGISTRY" ]; then
export IMAGE_NAME="$DOCKER_ENV_REGISTRY/$IMAGE_NAME"
fi

PROJECT_DOCKER_DIRECTORY="$PWD/docker"
PROJECT_COMPOSE_FILE="$PROJECT_DOCKER_DIRECTORY/compose.yaml"
Expand All @@ -31,16 +34,20 @@ USER_HOST_SUDOER_FILE="$USER_HOST_DIRECTORY/sudoer"
_help() {
{
echo
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo "Usage: $0 COMMAND [OPTIONS] [ARGS...]"
echo
echo "Manage the local dev env container"
echo "Encapsulate your project's development environment inside a Docker container"
echo
echo "Commands:"
echo " init|Prepare user host files and build dev env image"
echo " up|Create and start a persistent dev env container"
echo " down|Stop and remove the dev env container"
echo " exec|Execute a command in the running dev env container"
echo " shell|Open an interactive shell in the running dev env container"
echo " init|Prepare user host files and build dev image"
echo " build|Build dev image"
echo " up|Create and start a persistent dev container"
echo " down|Stop and remove the dev container"
echo " exec|Execute a command in the running dev container"
echo " shell|Open an interactive shell in the running dev container"
echo " tag [TAG]|Tag the dev image"
echo " pull [TAG]|Pull the dev image from the \$DOCKER_ENV_REGISTRY"
echo " push [TAG]|Push the dev image to the \$DOCKER_ENV_REGISTRY"
echo
echo "init options:"
echo " -f, --force|Recreate user host files, even if they already exist"
Expand All @@ -57,6 +64,7 @@ _compose() {
fi

docker compose \
--project-name "$PROJECT_NAME" \
--env-file "$USER_HOST_COMPOSE_ENV_FILE" \
--file "$PROJECT_COMPOSE_FILE" \
"$@"
Expand Down Expand Up @@ -164,8 +172,11 @@ case "$COMMAND" in
init)
shift
_init "$@"
_compose pull --ignore-buildable
_compose build
_compose build --pull dev
;;

build)
_compose build --pull dev
;;

up)
Expand All @@ -178,11 +189,39 @@ case "$COMMAND" in

exec)
shift
_compose exec dev-env "$@"
_compose exec dev "$@"
;;

shell)
_compose exec dev-env "${SHELL:-/bin/sh}" --login
_compose exec dev "${SHELL:-/bin/sh}" --login
;;

tag)
shift
TAG="${1:-latest}"
docker image tag "$IMAGE_NAME" "$IMAGE_NAME:$TAG"
;;

pull)
if [ -z "$DOCKER_ENV_REGISTRY" ]; then
echo "docker-env: 'DOCKER_ENV_REGISTRY' is not set, cannot proceed with pull." >&2
exit 1
fi

shift
TAG="${1:-latest}"
docker image pull "$IMAGE_NAME:$TAG"
;;

push)
if [ -z "$DOCKER_ENV_REGISTRY" ]; then
echo "docker-env: 'DOCKER_ENV_REGISTRY' is not set, cannot proceed with push." >&2
exit 1
fi

shift
TAG="${1:-latest}"
docker image push "$IMAGE_NAME:$TAG"
;;

*)
Expand Down
6 changes: 6 additions & 0 deletions templates/recommended.dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# syntax=docker/dockerfile:1
FROM ubuntu:24.04 AS base

LABEL org.opencontainers.image.title="<BAR>-dev" \
org.opencontainers.image.description="Development environment image of <BAR>" \
org.opencontainers.image.authors="@<FOO>" \
org.opencontainers.image.source="https://github.com/<FOO>/<BAR>" \
org.opencontainers.image.licenses="Apache-2.0"

RUN <<EOF
DEBIAN_FRONTEND=noninteractive \
apt-get update && apt-get install --yes --quiet --no-install-recommends \
Expand Down
10 changes: 10 additions & 0 deletions templates/user.compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
dev:
volumes:
- $HOME/.zshenv:$HOME/.zshenv
- $HOME/.zsh:$HOME/.zsh
- $HOME/.vim:$HOME/.vim
- $HOME/.vimrc:$HOME/.vimrc
- $HOME/.gitconfig:$HOME/.gitconfig
- $HOME/.gitignore_global:$HOME/.gitignore_global
- $HOME/.ssh:$HOME/.ssh

0 comments on commit bdaf808

Please sign in to comment.