diff --git a/.github/workflows/reusable-docker-publish-infrastructure.yaml b/.github/workflows/reusable-docker-publish-infrastructure.yaml new file mode 100644 index 0000000..7d204b9 --- /dev/null +++ b/.github/workflows/reusable-docker-publish-infrastructure.yaml @@ -0,0 +1,42 @@ +name: Build and Publish Docker Images + +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Get list of changed files + id: changed-files + uses: tj-actions/changed-files@v45 + with: + json: 'true' + + - name: Output changed files + run: | + echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}" + + - name: Build and push Docker images + if: contains(steps.changed-files.outputs.all_changed_files, 'Dockerfile') + env: + REPOSITORY_OWNER: ${{ github.repository_owner }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # Create a list of directories containing Dockerfiles + DOCKERFILE_DIRS=$(echo "${{ steps.changed-files.outputs.files }}" | grep 'Dockerfile' | xargs -I {} dirname {} | sort -u) + + # Build and push Docker images for each directory containing Dockerfile + for DIR in $DOCKERFILE_DIRS; do + echo "Building Docker image in directory $DIR" + IMAGE_NAME="${REPOSITORY_OWNER}/$(basename $DIR):latest" + docker build -t $IMAGE_NAME $DIR + echo $GITHUB_TOKEN | docker login ghcr.io -u $REPOSITORY_OWNER --password-stdin + docker push $IMAGE_NAME + done