name: DevEnv Docker Image Builder

on:
  workflow_dispatch:

jobs:
  build-and-push:
    name: Build and push DevEnv Docker image
    runs-on: penpot-extended-runner

    steps:
      - name: Set common environment variables
        run: |
          # Each job execution will use its own docker configuration.
          echo "DOCKER_CONFIG=${{ runner.temp }}/.docker-${{ github.run_id }}-${{ github.job }}" >> $GITHUB_ENV

      - name: Checkout code
        uses: actions/checkout@v6

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Login to Docker Registry (push destination)
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.PUB_DOCKER_USERNAME }}
          password: ${{ secrets.PUB_DOCKER_PASSWORD }}

      - name: Login to Docker Hardened Images registry (base image pull)
        uses: docker/login-action@v4
        with:
          registry: dhi.io
          username: ${{ secrets.PUB_DOCKER_USERNAME }}
          password: ${{ secrets.PUB_DOCKER_PASSWORD }}

      - name: Build and push DevEnv Docker image
        uses: docker/build-push-action@v7
        env:
          DOCKER_IMAGE: 'penpotapp/devenv'
        with:
          context: ./docker/devenv/
          file: ./docker/devenv/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          provenance: mode=max
          sbom: true
          tags: ${{ env.DOCKER_IMAGE }}:latest
          cache-from: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache
          cache-to: type=registry,ref=${{ env.DOCKER_IMAGE }}:buildcache,mode=max

      - name: Notify Mattermost
        uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e # v2.1.0
        with:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK }}
          MATTERMOST_CHANNEL: bot-alerts-cicd
          TEXT: |
            🚀 *[PENPOT] New devenv available*
            📄 You may want to update your devenv.
            @alvaro
