name: Docker

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    
    - name: setup node.js
      uses: actions/setup-node@v2
      with:
        node-version: '16'

    - name: setup dotnet8
      uses: actions/setup-dotnet@v2
      env:
          GITHUB_TOKEN: '${{ secrets.ACTIONS_TOKEN }}'
      with:
        dotnet-version: 8.0.x
        
    - name: restore projects
      run: 
        dotnet restore ./src/linker
        
    - name: docker login
      uses: docker/login-action@v2.1.0
      with:
        username: '${{secrets.DOCKER_USERNAME}}'
        password: '${{secrets.DOCKER_PASSWORD}}'
    
    - name: docker buildx
      uses: docker/setup-buildx-action@v2.5.0
      
    - name: chmod shell
      run: chmod +x publish-docker.sh
    - name: publish projects
      run: ./publish-docker.sh

    - name: create docker image manifest
      run: |
        docker pull --platform linux/arm/v7 snltty/linker-musl-arm:latest &&  \
        docker tag snltty/linker-musl-arm:latest snltty/linker-musl:arm &&  \
        docker push snltty/linker-musl:arm &&  \
        docker pull --platform linux/arm64 snltty/linker-musl-arm64:latest &&  \
        docker tag snltty/linker-musl-arm64:latest snltty/linker-musl:arm64 &&  \
        docker push snltty/linker-musl:arm64 &&  \
        docker pull --platform linux/amd64 snltty/linker-musl-x64:latest &&  \
        docker tag snltty/linker-musl-x64:latest snltty/linker-musl:amd64 &&  \
        docker push snltty/linker-musl:amd64 &&  \
        docker manifest create snltty/linker-musl:latest snltty/linker-musl:amd64 snltty/linker-musl:arm64 snltty/linker-musl:arm && \
        docker manifest create snltty/linker-musl:v{{version}} snltty/linker-musl:amd64 snltty/linker-musl:arm64 snltty/linker-musl:arm && \
        docker manifest push snltty/linker-musl:latest && \
        docker manifest push snltty/linker-musl:v{{version}} && \
        docker pull --platform linux/arm/v7 snltty/linker-debian-arm:latest &&  \
        docker tag snltty/linker-debian-arm:latest snltty/linker-debian:arm &&  \
        docker push snltty/linker-debian:arm &&  \
        docker pull --platform linux/arm64 snltty/linker-debian-arm64:latest &&  \
        docker tag snltty/linker-debian-arm64:latest snltty/linker-debian:arm64 &&  \
        docker push snltty/linker-debian:arm64 &&  \
        docker pull --platform linux/amd64 snltty/linker-debian-x64:latest &&  \
        docker tag snltty/linker-debian-x64:latest snltty/linker-debian:amd64 &&  \
        docker push snltty/linker-debian:amd64 &&  \
        docker manifest create snltty/linker-debian:latest snltty/linker-debian:amd64 snltty/linker-debian:arm64 snltty/linker-debian:arm && \
        docker manifest create snltty/linker-debian:v{{version}} snltty/linker-debian:amd64 snltty/linker-debian:arm64 snltty/linker-debian:arm && \
        docker manifest push snltty/linker-debian:latest &&  \
        docker manifest push snltty/linker-debian:v{{version}}