SQS Processor Application added #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Selective Build and Push Docker Images | |
on: | |
push: | |
paths: | |
- '**/**' # Trigger workflow for any changes in the repo | |
pull_request: | |
paths: | |
- '**/**' # Trigger workflow for pull requests involving any file or folder | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the repository | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
# Step 2: Get a list of changed directories (handle first commit case) | |
- name: Get changed directories | |
id: changes | |
run: | | |
if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then | |
# Get changed files since the previous commit | |
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD) | |
else | |
# If this is the first commit, treat all files as changed | |
CHANGED_FILES=$(git ls-files) | |
fi | |
# Extract unique top-level directories containing changed files | |
CHANGED_DIRS=$(echo "$CHANGED_FILES" | awk -F/ '{print $1}' | sort -u) | |
echo "Changed directories: $CHANGED_DIRS" | |
echo "dirs=$CHANGED_DIRS" >> $GITHUB_ENV | |
# Step 3: Log in to Docker Hub | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# Step 4: Build and Push Docker images for changed directories | |
- name: Build and Push Docker images | |
run: | | |
for dir in $(echo $dirs | tr ',' '\n'); do | |
if [ -d "$dir" ] && [ -f "$dir/Dockerfile" ]; then | |
IMAGE_NAME="juanroldan1989/$(basename $dir)" | |
IMAGE_TAG=$(git rev-parse --short HEAD) | |
echo "Building Docker image for $dir" | |
docker build -t $IMAGE_NAME:$IMAGE_TAG $dir | |
echo "Pushing Docker image for $dir" | |
docker push $IMAGE_NAME:$IMAGE_TAG | |
fi | |
done | |
# Step 5: Clean up Docker images | |
- name: Clean up Docker images | |
run: docker system prune -f |