Skip to content

Commit

Permalink
Workflow CI docker push
Browse files Browse the repository at this point in the history
  • Loading branch information
vanshpatelx committed Feb 8, 2025
1 parent 0eaa521 commit 2e65041
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
26 changes: 11 additions & 15 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
fetch-depth: 0

- name: Detect changed microservices
id: filter
Expand Down Expand Up @@ -46,10 +46,6 @@ jobs:
runs-on: ubuntu-latest
environment: opinex
if: needs.detect-changes.outputs.services != '[]'
strategy:
matrix:
service: ${{ fromJson(needs.detect-changes.outputs.services) }}

steps:
- name: Checkout Code
uses: actions/checkout@v3
Expand Down Expand Up @@ -137,23 +133,23 @@ jobs:
cd packages/test
npm run test
- name: Convert JSON to space-separated list
id: format_services
run: |
services_list=$(echo '${{ needs.detect-changes.outputs.services }}' | jq -r '.[]' | tr '\n' ' ')
echo "services_list=$services_list" >> "$GITHUB_ENV"
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Push Docker Image
env:
SERVICE: ${{ matrix.service }}
VERSION: ${{ github.sha }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
- name: Tag Images
run: |
IMAGE_NAME_TAG="${DOCKER_USERNAME}/${SERVICE}-service:${VERSION}"
echo "Pushing image: $IMAGE_NAME_TAG"
docker push "$IMAGE_NAME_TAG"
cd scripts/
chmod +x tag_images.sh
./tag_images.sh vanshpatel latest $services_list
Expand Down
6 changes: 1 addition & 5 deletions scripts/build_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# 🌟 Backend Docker Image Build Script
# ============================================================

DOCKER_REPO="vanshpatel"

# 🎨 Define Colors for UI Formatting
GREEN=$(printf "\e[32m")
YELLOW=$(printf "\e[33m")
Expand Down Expand Up @@ -79,11 +77,9 @@ for SERVICE in "${SERVICES[@]}"; do
fi

IMAGE_NAME="${SERVICE}-service:${VERSION}"
IMAGE_NAME_TAG="${DOCKER_REPO}/${IMAGE_NAME}"


printf "${CYAN}${BUILD} Building image for ${BOLD}$SERVICE${RESET}...${RESET}\n"
docker build -f "$SERVICE_DOCKERFILE" -t "$IMAGE_NAME_TAG" .
docker build -f "$SERVICE_DOCKERFILE" -t "$IMAGE_NAME" .

if [[ $? -eq 0 ]]; then
printf "${GREEN}${CHECK} Successfully built ${BOLD}$IMAGE_NAME${RESET}\n"
Expand Down
42 changes: 42 additions & 0 deletions scripts/tag_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Colors for better visibility
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No color

# Check if minimum required arguments are provided
if [ "$#" -lt 3 ]; then
echo -e "${RED}Usage: $0 <username> <version> <service1> <service2> ...${NC}"
exit 1
fi

# Extract CLI inputs
USERNAME=$1
VERSION=$2
shift 2 # Remove first two arguments (username and version) from the list

# Iterate over provided service names
for SERVICE in "$@"; do
OLD_IMAGE="${SERVICE}-service"
NEW_IMAGE="${USERNAME}/${OLD_IMAGE}:${VERSION}"

# Check if the image exists
if docker images | awk '{print $1}' | grep -q "^${OLD_IMAGE}$"; then
echo -e "${GREEN}Found image: $OLD_IMAGE${NC}"

# Tag the image with the new format
docker tag "$OLD_IMAGE" "$NEW_IMAGE"
echo -e "${GREEN}Tagged $OLD_IMAGE as $NEW_IMAGE${NC}"

# Push the image to the registry
if docker push "$NEW_IMAGE"; then
echo -e "${GREEN}Successfully pushed $NEW_IMAGE${NC}"
else
echo -e "${RED}Failed to push $NEW_IMAGE! Exiting...${NC}"
exit 1
fi
else
echo -e "${RED}Image $OLD_IMAGE not found, skipping...${NC}"
fi
done

0 comments on commit 2e65041

Please sign in to comment.