name: Docker stable

on:
  workflow_dispatch:
  # Rebuild when changing the stable branch
  push:
      branches:
            - stable
#  release:
#    types: [published, created, edited]

jobs:
  docker-simgrid:

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: build
        run: |
          grep SIMGRID_VERSION_PATCH CMakeLists.txt| grep -q 'SIMGRID_VERSION_PATCH "0"' || (echo "Only run this action on stable source";exit 1)
          sgversion=$(grep set.SIMGRID_VERSION_MINOR CMakeLists.txt|sed 's/[^"]*"//'|sed 's/".*$//')
          echo "sgversion=3.$sgversion" >> $env:GITHUB_ENV
          cd tools/docker
          make stable
          docker push --all-tags simgrid/stable
      - name: Send the failure Message
        if: ${{ failure() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#FF0000",
               "text": "Failure when building STABLE docker v${{ env.sgversion }}! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
               "icon": "https://cdn3.iconfinder.com/data/icons/system-basic-vol-4-1/20/icon-note-attention-alt3-512.png"
            }]}
      - name: Send the success Message
        if: ${{ success() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#00FF00",
               "text": "Docker STABLE docker v${{ env.sgversion }} built and pushed successfully! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
            }]}

  docker-tuto-s4u:

    # the Dockerfile builds upon the docker-unstable image, so add a dependency
    needs: docker-simgrid
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: build docker tuto-s4u
        run: |
          cd tools/docker
          make tuto-s4u
          docker push simgrid/tuto-s4u
      - name: Send the failure Message
        if: ${{ failure() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#FF0000",
               "text": "Failure when building docker tuto-s4u! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
               "icon": "https://cdn3.iconfinder.com/data/icons/system-basic-vol-4-1/20/icon-note-attention-alt3-512.png"
            }]}
      - name: Send the success Message
        if: ${{ success() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#00FF00",
               "text": "Docker tuto-s4u built and pushed successfully! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
            }]}

  docker-tuto-smpi:
    # the Dockerfile builds upon the docker-stable image, so add a dependency
    needs: docker-simgrid

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: build tuto SMPI
        run: |
          grep SIMGRID_VERSION_PATCH CMakeLists.txt| grep -q 'SIMGRID_VERSION_PATCH "0"' || (echo "Only run this action on stable source";exit 1)
          cd tools/docker
          make tuto-smpi
          docker push simgrid/tuto-smpi
      - name: Send the failure Message
        if: ${{ failure() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#FF0000",
               "text": "Failure when building docker tuto SMPI! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
               "icon": "https://cdn3.iconfinder.com/data/icons/system-basic-vol-4-1/20/icon-note-attention-alt3-512.png"
            }]}
      - name: Send the success Message
        if: ${{ success() }}
        uses: mattermost/action-mattermost-notify@master
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: ${{ secrets.MATTERMOST_CHANNEL}}
          PAYLOAD: |-
            {
             "channel": "bot-office",
             "attachments": [{
               "color": "#00FF00",
               "text": "Docker tuto SMPI built and pushed successfully! See ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
            }]}

