name: Dockerbuild
on:
    workflow_dispatch: {}
    push:
        branches: [master, staging, develop]
        paths:
            - "dockerbuild/**"
    pull_request:
concurrency: ${{ github.workflow }}-${{ github.ref_name }}
env:
    REGISTRY: ghcr.io
    IMAGE_NAME: ${{ github.repository }}-dockerbuild
permissions: {}
jobs:
    build:
        name: Docker Build
        runs-on: ubuntu-24.04
        permissions:
            contents: read
            packages: write
        steps:
            - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5

            - name: Set up QEMU
              uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3

            - name: Set up Docker Buildx
              uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3
              with:
                  install: true

            - name: Build test image
              uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6
              with:
                  file: dockerbuild/Dockerfile
                  push: false
                  load: true
                  tags: element-desktop-dockerbuild
                  platforms: linux/amd64

            - name: Test image
              run: docker run -v $PWD:/project element-desktop-dockerbuild yarn install

            - name: Log in to the Container registry
              uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
              if: github.event_name != 'pull_request'
              with:
                  registry: ${{ env.REGISTRY }}
                  username: ${{ github.actor }}
                  password: ${{ secrets.GITHUB_TOKEN }}

            - name: Extract metadata for Docker
              id: meta
              if: github.event_name != 'pull_request'
              uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5
              with:
                  images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
                  tags: |
                      type=ref,event=branch
                      type=ref,event=pr

            - name: Build and push Docker image
              if: github.event_name != 'pull_request'
              uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6
              with:
                  file: dockerbuild/Dockerfile
                  push: true
                  tags: ${{ steps.meta.outputs.tags }}
                  labels: ${{ steps.meta.outputs.labels }}
                  platforms: linux/amd64,linux/arm64
