Search code examples
dockergithub-actionscicd

In my repository, github actions keep saying "This job was skipped"


.github

backend

front

I have a folder structure in the repository.

What I want is to have Docker automatically build and push when the source code changes on the front.

When the source code in the backend is changed, I want only the backend part to be automatically built and pushed.

However, in the current situation, skipping occurs even if both parts of the code are changed.

Is there a solution?

- main.yml

name: Docker build and push

on:
  push:
    branches:
      - main
    paths:
      - "front/**"
      - "backend/**"

jobs:
  build_and_push_frontend:
    runs-on: ubuntu-latest
    if: contains(github.event.head_commit.modified, 'front/')
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Meta for Frontend
        id: docker_meta_frontend
        uses: docker/metadata-action@v3
        with:
          images: front

      - name: Build and Push Frontend Docker Image
        uses: docker/build-push-action@v2
        with:
          context: ./front
          file: ./front/Dockerfile
          platforms: linux/amd64
          push: true
          tags: ${{ steps.docker_meta_frontend.outputs.tags }}
          labels: ${{ steps.docker_meta_frontend.outputs.labels }}

  build_and_push_backend:
    runs-on: ubuntu-latest
    if: contains(github.event.head_commit.modified, 'backend/')
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Meta for Backend
        id: docker_meta_backend
        uses: docker/metadata-action@v3
        with:
          images: backend

      - name: Build and Push Backend Docker Image
        uses: docker/build-push-action@v2
        with:
          context: ./backend
          file: ./backend/Dockerfile
          platforms: linux/amd64
          push: true
          tags: ${{ steps.docker_meta_backend.outputs.tags }}
          labels: ${{ steps.docker_meta_backend.outputs.labels }}

Solution

  • As per this discussion and blogpost

    The following attributes have been removed from the commits section of push payloads for Actions:

    added
    removed
    modified
    

    seems like github.event.head_commit.modified has been removed already.

    You can split your github action in two actions or you probably need to use some third party way, for example paths-filter as mentioned in this thread.