From 93da602d31f401ca2a60cffaddc26cea55320231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Molta=20Hern=C3=A1ndez?= <83254590+DavidMolta18@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:00:54 -0500 Subject: [PATCH] Update and rename dockerbuidimages.yaml to dockerbuildpush.yaml --- .github/workflows/dockerbuidimages.yaml | 63 ------------------------- .github/workflows/dockerbuildpush.yaml | 57 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 63 deletions(-) delete mode 100644 .github/workflows/dockerbuidimages.yaml create mode 100644 .github/workflows/dockerbuildpush.yaml diff --git a/.github/workflows/dockerbuidimages.yaml b/.github/workflows/dockerbuidimages.yaml deleted file mode 100644 index 3c05d058c..000000000 --- a/.github/workflows/dockerbuidimages.yaml +++ /dev/null @@ -1,63 +0,0 @@ -name: Build and Push Docker Image to ACR - -on: - pull_request: - branches: - - main - -jobs: - build-and-push: - runs-on: ubuntu-latest - - steps: - # 1. Checkout el código - - name: Checkout Code - uses: actions/checkout@v3 - - # 2. Configuración de Java - - name: Set up JDK - uses: actions/setup-java@v3 - with: - java-version: 17 - distribution: 'temurin' - - # 3. Construir la aplicación con Maven - - name: Build Application with Maven - run: | - cd spring-petclinic-vets-service - mvn clean package -DskipTests - - # 4. Login al Azure Container Registry - - name: Login to Azure - uses: azure/login@v1 - with: - creds: ${{ secrets.AZURE_CREDENTIALS }} - - - name: Login to Azure Container Registry - run: az acr login --name petdistriACR - - # 5. Construir y etiquetar la imagen Docker - - name: Build Docker Image - run: | - IMAGE_TAG="pr-${{ github.event.pull_request.number }}" - IMAGE_NAME="petdistriacr.azurecr.io/spring-petclinic-vets-service:$IMAGE_TAG" - docker build -t $IMAGE_NAME ./spring-petclinic-vets-service - echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV - - # 6. Subir la imagen al ACR - - name: Push Docker Image to ACR - run: | - if [ -z "${{ env.IMAGE_NAME }}" ]; then - echo "ERROR: IMAGE_NAME is not set." - exit 1 - fi - docker push ${{ env.IMAGE_NAME }} - - # 7. Publicar el resultado - - name: Output Image Info - run: | - if [ -z "${{ env.IMAGE_NAME }}" ]; then - echo "ERROR: IMAGE_NAME is not set." - exit 1 - fi - echo "Docker image built and pushed successfully: ${{ env.IMAGE_NAME }}" diff --git a/.github/workflows/dockerbuildpush.yaml b/.github/workflows/dockerbuildpush.yaml new file mode 100644 index 000000000..87be18563 --- /dev/null +++ b/.github/workflows/dockerbuildpush.yaml @@ -0,0 +1,57 @@ +name: Build and Push Docker Images to ACR + +on: + pull_request: + branches: + - main + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + # 1. Checkout el código + - name: Checkout Code + uses: actions/checkout@v3 + + # 2. Detectar qué servicios tienen cambios en sus Dockerfiles + - name: Detect Modified Dockerfiles + id: detect-changes + run: | + # Detectar solo servicios que tienen cambios en sus Dockerfiles + MODIFIED_SERVICES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -oE '^[^/]+/Dockerfile' | awk -F/ '{print $1}' | sort -u) + if [ -z "$MODIFIED_SERVICES" ]; then + echo "No modified Dockerfiles detected. Skipping build and push." + exit 0 + fi + echo "MODIFIED_SERVICES=$MODIFIED_SERVICES" >> $GITHUB_ENV + shell: bash + + # 3. Iterar sobre los servicios modificados y construir/subir imágenes + - name: Build and Push Docker Images + if: env.MODIFIED_SERVICES + run: | + for SERVICE in $MODIFIED_SERVICES; do + echo "Detected changes in Dockerfile for service: $SERVICE" + + # Crear etiquetas únicas para la imagen + IMAGE_TAG="pr-${{ github.event.pull_request.number }}" + IMAGE_NAME="petdistriacr.azurecr.io/$SERVICE:$IMAGE_TAG" + + # Construir la imagen Docker + echo "Building Docker image for $SERVICE" + docker build -t $IMAGE_NAME ./$SERVICE + + # Subir la imagen al ACR + echo "Pushing Docker image for $SERVICE to ACR" + docker push $IMAGE_NAME + + # Guardar la información de la imagen + echo "Successfully processed $SERVICE with image: $IMAGE_NAME" + done + shell: bash + + # 4. Publicar un resumen al final + - name: Publish Summary + run: | + echo "Docker images built and pushed successfully for services: $MODIFIED_SERVICES"