Skip to content

Commit

Permalink
Add first build
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Oct 5, 2024
1 parent ebeac27 commit 5d5a2ee
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 82 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/action_publish-images-beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Docker Publish (Beta Images)

on:
workflow_dispatch:
push:
branches:
- main
paths:
- src/**
jobs:
build-beta-images:
uses: ./.github/workflows/service_docker-build-and-publish.yml
secrets: inherit
with:
release_type: 'beta'
16 changes: 16 additions & 0 deletions .github/workflows/action_publish-images-production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Docker Publish (Production Images)

on:
workflow_dispatch:
release:
types: [released]
# Commenting out until ready
# schedule:
# - cron: '0 8 * * 2'

jobs:
build-production-images:
uses: ./.github/workflows/service_docker-build-and-publish.yml
secrets: inherit
with:
release_type: 'latest'
85 changes: 85 additions & 0 deletions .github/workflows/service_docker-build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Build and Publish Docker Images

on:
workflow_call:
inputs:
release_type:
type: string
required: true
description: 'Release type (latest, beta, edge, dev, etc)'
default: 'edge'

jobs:
setup-matrix:
runs-on: ubuntu-24.04
outputs:
ansible-version-map-json: ${{ steps.get-ansible-versions.outputs.ansible-version-map-json }}
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Assemble Ansible versions into the matrix 😎
id: get-ansible-versions
run: |
MATRIX_JSON=$(yq -o=json ansible-versions.yml | jq -c '{include: [.ansible_variations[] | .versions[] as $version | .python_versions[] as $python | .base_os[] as $os | {ansible_variation: .name, ansible_version: $version.version, python_version: $python, base_os: $os.name, latest_stable: ($version.latest_stable // false)}]}')
echo "ansible-version-map-json=${MATRIX_JSON}" >> $GITHUB_OUTPUT
echo "${MATRIX_JSON}" | jq '.'
build-and-push:
needs: setup-matrix
runs-on: ubuntu-24.04
strategy:
matrix: ${{fromJson(needs.setup-matrix.outputs.ansible-version-map-json)}}
steps:
- name: Check out code.
uses: actions/checkout@v4

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: ${{ inputs.authenticate_with_ghcr }}
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: "📦 Assemble the Docker Tags"
run: |
bash build.sh \
--variation ${{ matrix.ansible_variation }} \
--version ${{ matrix.ansible_version }} \
--python ${{ matrix.python_version }} \
--os ${{ matrix.base_os }} \
--release_type ${{ inputs.release_type }} \
--print-tags-only
- name: Build and push
uses: docker/build-push-action@v6
with:
file: src/Dockerfile
cache-from: type=gha,mode=max
cache-to: type=gha,mode=max
build-args: |
ANSIBLE_VERSION=${{ matrix.ansible_version }}
PYTHON_VERSION=${{ matrix.python_version }}
BASE_OS_VERSION=${{ matrix.base_os }}
PACKAGE_DEPENDENCIES=${{ env.PACKAGE_DEPENDENCIES }}
platforms: |
linux/amd64
linux/arm/v7
linux/arm64/v8
pull: true
push: true
tags: ${{ env.DOCKER_TAGS }}}
outputs: type=image,name=target,annotation-index.org.opencontainers.image.description=Run Ansible anywhere with the power of Docker
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ⚠️ This is a very alpha work-in-progress
Don't report bugs yet. I am still working on it 😃
2 changes: 0 additions & 2 deletions ansible-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
################################################
ansible_variations:
- name: "ansible-core"
tag_name: "core"
versions:
- version: "2.17"
latest_stable: true
Expand Down Expand Up @@ -35,7 +34,6 @@ ansible_variations:
- name: bullseye

- name: "ansible"
tag_name: "ansible"
latest_stable: true
versions:
- version: "10"
Expand Down
166 changes: 93 additions & 73 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ function ui_set_green {
printf $'\033[0;32m'
}

function ui_set_red {
printf $'\033[0;31m'
}

function ui_reset_colors {
printf "\e[0m"
}
Expand All @@ -42,6 +38,30 @@ function echo_color_message (){
ui_reset_colors
}

# Add this function near the top of the script, with other utility functions
save_to_github_env() {
local key=$1
local value=$2
local is_multiline=${3:-false}

if [[ $CI == "true" ]]; then
if [[ -n "$GITHUB_ENV" ]]; then
if [[ "$is_multiline" == "true" ]]; then
echo "${key}<<EOF" >> "$GITHUB_ENV"
echo "$value" >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"
else
echo "${key}=${value}" >> "$GITHUB_ENV"
fi
echo_color_message green "✅ Saved ${key} to GITHUB_ENV"
else
echo_color_message yellow "⚠️ GITHUB_ENV is not set. Skipping writing ${key} to GITHUB_ENV."
fi
else
echo_color_message yellow "Not running in CI environment. Skipping writing to GITHUB_ENV."
fi
}

check_vars() {
message=$1
shift
Expand Down Expand Up @@ -251,67 +271,71 @@ generate_tags() {
printf '%s\n' "${tags[@]}" | sort -u
}

# Add these functions near the top of the script, after other utility functions
get_os_family() {
yq e ".operating_system_distributions[] | select(.versions[].name == \"$BASE_OS\") | .name" "$ANSIBLE_VERSIONS_FILE"
}

get_package_dependencies() {
local os_family=$1
yq e ".operating_system_distributions[] | select(.name == \"$os_family\") | .versions[] | select(.name == \"$BASE_OS\") | .package_dependencies[]" "$ANSIBLE_VERSIONS_FILE" | tr '\n' ',' | sed 's/,$//'
}

build_docker_image() {
tags=($(generate_tags))
tags=($(generate_tags))

# Get package dependencies from ansible-versions.yml
local os_family=$(yq e ".operating_system_distributions[] | select(.versions[].name == \"$BASE_OS\") | .name" "$ANSIBLE_VERSIONS_FILE")
local package_dependencies=$(yq e ".operating_system_distributions[] | select(.name == \"$os_family\") | .versions[] | select(.name == \"$BASE_OS\") | .package_dependencies[]" "$ANSIBLE_VERSIONS_FILE" | tr '\n' ' ')

# Get the full patch version
local full_ansible_version=$(lookup_pypi_version "$ANSIBLE_VARIATION" "$ANSIBLE_VERSION")

build_args=(
--build-arg ANSIBLE_VARIATION="$ANSIBLE_VARIATION"
--build-arg ANSIBLE_VERSION="$full_ansible_version"
--build-arg PYTHON_VERSION="$PYTHON_VERSION"
--build-arg BASE_OS_VERSION="$BASE_OS"
--build-arg PACKAGE_DEPENDENCIES="'$package_dependencies'"
)

for tag in "${tags[@]}"; do
build_args+=(--tag "$tag")
done
# Use the centralized functions to get OS family and package dependencies
local os_family=$(get_os_family)
local package_dependencies=$(get_package_dependencies "$os_family")

# Get the full patch version
local full_ansible_version=$(lookup_pypi_version "$ANSIBLE_VARIATION" "$ANSIBLE_VERSION")

build_args=(
--build-arg ANSIBLE_VARIATION="$ANSIBLE_VARIATION"
--build-arg ANSIBLE_VERSION="$full_ansible_version"
--build-arg PYTHON_VERSION="$PYTHON_VERSION"
--build-arg BASE_OS_VERSION="$BASE_OS"
)

if [ -n "$package_dependencies" ]; then
build_args+=(--build-arg PACKAGE_DEPENDENCIES="$package_dependencies")
fi

echo_color_message yellow "🐳 Building Docker Image with tags:"
printf '%s\n' "${tags[@]}"

# Set default platform if not specified
if [ -z "$PLATFORM" ]; then
PLATFORM="linux/amd64"
fi

# Add platform to build args
build_args+=(--platform "$PLATFORM")

# Construct the Docker command as an array
docker_command=(
docker build
"${DOCKER_ADDITIONAL_BUILD_ARGS[@]}"
"${build_args[@]}"
--file "src/Dockerfile"
"$PROJECT_ROOT_DIR"
)

# Show the Docker command
echo_color_message yellow "Docker command to be executed:"
echo "${docker_command[*]}"

# Execute the Docker command
"${docker_command[@]}"

if [[ "$CI" == "true" ]]; then
if [[ -n "$GITHUB_ENV" ]]; then
echo "DOCKER_TAGS<<EOF" >> "$GITHUB_ENV"
printf '%s\n' "${tags[@]}" >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"
echo_color_message green "✅ Saved Docker Tags to GITHUB_ENV"
else
echo_color_message yellow "⚠️ GITHUB_ENV is not set. Skipping writing to GITHUB_ENV."
for tag in "${tags[@]}"; do
build_args+=(--tag "$tag")
done

echo_color_message yellow "🐳 Building Docker Image with tags:"
printf '%s\n' "${tags[@]}"

# Set default platform if not specified
if [ -z "$PLATFORM" ]; then
PLATFORM="linux/amd64"
fi
else
echo_color_message yellow "Not running in CI environment. Skipping writing to GITHUB_ENV."
fi

# Add platform to build args
build_args+=(--platform "$PLATFORM")

# Construct the Docker command as an array
docker_command=(
docker --debug buildx build
"${DOCKER_ADDITIONAL_BUILD_ARGS[@]}"
"${build_args[@]}"
--file "src/Dockerfile"
"$PROJECT_ROOT_DIR"
)

# Show the Docker command
echo_color_message yellow "Docker command to be executed:"
echo "${docker_command[*]}"

# Execute the Docker command
"${docker_command[@]}"

# Echo out the tags at the end
echo_color_message green "✅ Docker image built successfully with the following tags:"
printf '%s\n' "${tags[@]}" | sort
}

help_menu() {
Expand Down Expand Up @@ -486,18 +510,14 @@ print_tags() {
printf '%s\n' "${tags[@]}" | sort

# Save to GitHub's environment
if [[ $CI == "true" ]]; then
if [[ -n "$GITHUB_ENV" ]]; then
echo "DOCKER_TAGS<<EOF" >> "$GITHUB_ENV"
printf '%s\n' "${tags[@]}" >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"
echo_color_message green "✅ Saved Docker Tags to GITHUB_ENV"
else
echo_color_message yellow "⚠️ GITHUB_ENV is not set. Skipping writing to GITHUB_ENV."
fi
else
echo_color_message yellow "Not running in CI environment. Skipping writing to GITHUB_ENV."
fi
save_to_github_env "DOCKER_TAGS" "$(printf '%s\n' "${tags[@]}")" true

# Use the centralized functions to get OS family and package dependencies
local os_family=$(get_os_family)
local package_dependencies=$(get_package_dependencies "$os_family")

# Save PACKAGE_DEPENDENCIES to GitHub's environment
save_to_github_env "PACKAGE_DEPENDENCIES" "$package_dependencies"
}

# Main execution
Expand Down
12 changes: 5 additions & 7 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# First stage to declare global ARGs
ARG PYTHON_VERSION
ARG BASE_OS_VERSION
ARG PYTHON_VERSION='3.12'
ARG BASE_OS_VERSION='bullseye'

# Main build stage
FROM python:${PYTHON_VERSION}-${BASE_OS_VERSION}

# Redeclare ARGs after FROM for this stage
ARG PYTHON_VERSION
ARG BASE_OS_VERSION
ARG ANSIBLE_VARIATION
ARG ANSIBLE_VERSION
ARG PACKAGE_DEPENDENCIES
ARG ANSIBLE_VARIATION=''
ARG ANSIBLE_VERSION=''
ARG PACKAGE_DEPENDENCIES=''

COPY --chown=root:root --chmod=755 src/rootfs /

Expand Down

0 comments on commit 5d5a2ee

Please sign in to comment.