Skip to content

Build DevContainer

Build DevContainer #3

name: Build DevContainer
on:
workflow_dispatch:
inputs:
devcontainer_choice:
description: 'DevContainer to build (only those with Dockerfiles)'
required: true
type: choice
options:
- 'just'
- 'default'
default: 'just'
image_tag:
description: 'Image tag (e.g., latest, v1.0.0)'
required: false
default: 'latest'
build_args:
description: 'Build arguments (comma-separated, e.g., INSTALL_ALL=true,GO_VERSION=1.25.1)'
required: false
type: string
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set image name
id: image-name
run: |
devcontainer="${{ github.event.inputs.devcontainer_choice }}"
# Remove -container suffix if it exists
image_name="${devcontainer%-container}"
echo "name=$image_name" >> $GITHUB_OUTPUT
- name: Prepare build arguments
id: build-args
run: |
build_args="${{ github.event.inputs.build_args }}"
# Convert comma-separated format to newline-separated for Docker
if [[ -n "$build_args" ]]; then
build_args=$(echo "$build_args" | tr ',' '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "args<<EOF" >> $GITHUB_OUTPUT
echo "$build_args" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}/${{ steps.image-name.outputs.name }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=raw,value=${{ github.event.inputs.image_tag || 'latest' }}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./.devcontainer/${{ github.event.inputs.devcontainer_choice }}/Dockerfile
platforms: linux/amd64,linux/arm64/v8
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ steps.build-args.outputs.args }}
cache-from: type=gha,scope=${{ github.event.inputs.devcontainer_choice }}
cache-to: type=gha,mode=max,scope=${{ github.event.inputs.devcontainer_choice }}
- name: Output image details
run: |
echo "Built devcontainer: ${{ github.event.inputs.devcontainer_choice }}"
echo "Tags: ${{ steps.meta.outputs.tags }}"