name: Deploy

on:
  push:
    tags: ["v*"]
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:

  publish:
    name: Publish
    runs-on: ubuntu-latest
    steps:

    - name: Checkout
      uses: actions/checkout@v2

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v1

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

    - name: Docker Login
      uses: docker/login-action@v1
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Generate Docker tags
      uses: docker/metadata-action@v3
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      id: meta

    - name: Build and push Docker images
      uses: docker/build-push-action@v2
      with:
        pull: true
        platforms: linux/amd64
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}

  deploy:
    name: Deploy to pricklypear
    runs-on: ubuntu-latest
    needs: publish
    steps:

    - name: Import SSH key
      run: |
        mkdir -p ~/.ssh
        echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_ed25519
        chmod 600 ~/.ssh/id_ed25519
        echo "pricklypear.cacti.makerforce.io ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGd9tL56+YUDQjyGy3iZNGUtjVkvAD442Q6Kt53Zsy8g" >> ~/.ssh/known_hosts

    - name: Re-pull
      run: |
        ssh datetime@pricklypear.cacti.makerforce.io "podman login --username ${{ github.actor }} --password ${{ secrets.GITHUB_TOKEN }} ghcr.io && podman pull ghcr.io/serverwentdown/datetime.link:main"

    - name: Restart
      run: |
        ssh datetime@pricklypear.cacti.makerforce.io "systemctl restart --user container-datetime.service"


# vim: set et ts=2 sw=2:
