Skip to content

chnages in week3 and 4 #4

chnages in week3 and 4

chnages in week3 and 4 #4

name: Build and Push Docker Image
on:
push:
paths:
- 'Learn/week3/**'
- 'Learn/week4/**'
- 'Learn/week5/**'
workflow_dispatch:
inputs:
directory:
description: 'Select the directory (week) to build and push'
required: true
type: choice
options:
- week3
- week4
- week5
- all
image_tag:
description: 'Tag for the Docker image (e.g., v1.0, latest)'
required: true
default: 'latest'
type: string
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
directories: ${{ steps.set-dirs.outputs.directories }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Check changed directories
id: filter
if: github.event_name == 'push'
uses: dorny/paths-filter@v3
with:
filters: |
week3: 'Learn/week3/**'
week4: 'Learn/week4/**'
week5: 'Learn/week5/**'
- id: set-dirs
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
if [ "${{ github.event.inputs.directory }}" = "all" ]; then
echo 'directories=["week3", "week4", "week5"]' >> $GITHUB_OUTPUT
else
echo 'directories=["${{ github.event.inputs.directory }}"]' >> $GITHUB_OUTPUT
fi
else
# For push events, build array based on filter results
DIRS=()
if [ "${{ steps.filter.outputs.week3 }}" = "true" ]; then DIRS+=("\"week3\""); fi
if [ "${{ steps.filter.outputs.week4 }}" = "true" ]; then DIRS+=("\"week4\""); fi
if [ "${{ steps.filter.outputs.week5 }}" = "true" ]; then DIRS+=("\"week5\""); fi
# Join array with commas and wrap in brackets
JSON_DIRS=$(IFS=,; echo "[${DIRS[*]}]")
echo "directories=$JSON_DIRS" >> $GITHUB_OUTPUT
fi
docker-build-push:
needs: prepare
runs-on: ubuntu-latest
strategy:
matrix:
directory: ${{ fromJson(needs.prepare.outputs.directories) }}
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Lowercase repository owner
id: prep
run: |
echo "OWNER_LOWERCASE=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_OUTPUT
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "IMAGE_TAG=${{ github.event.inputs.image_tag }}" >> $GITHUB_OUTPUT
else
echo "IMAGE_TAG=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
fi
- name: Build and Push
uses: docker/build-push-action@v6
with:
context: ./Learn/${{ matrix.directory }}
file: ./Learn/${{ matrix.directory }}/Dockerfile
push: true
tags: |
ghcr.io/${{ steps.prep.outputs.OWNER_LOWERCASE }}/playground-${{ matrix.directory }}:${{ steps.prep.outputs.IMAGE_TAG }}
ghcr.io/${{ steps.prep.outputs.OWNER_LOWERCASE }}/playground-${{ matrix.directory }}:latest