name: Generate a PiSCSI OS image, based upon the official Rapsberry Pi OS

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the rpi_image_creation branch
  push:
    branches: [ rpi_image_creation ]
  pull_request:
    branches: [ rpi_image_creation ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    # TODO: I don't think this step is needed....
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout PiSCSI
        uses: actions/checkout@v2
        with:
          path: PISCSI
      
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout PiSCSI
        uses: actions/checkout@v2
        with:
          repository: akuker/pi-gen
          path: pi-gen
      
      - name: Install Raspberry Pi build toolchain
        run: sudo apt-get install coreutils quilt parted qemu-user-static debootstrap zerofree zip dosfstools libarchive-tools libcap2-bin grep rsync xz-utils file git curl bc qemu-utils kpartx

      - name: Configure the build
        run: |
          echo -----------------------------------------------------
          echo "IMG_NAME=PiSCSI-$GITHUB_RUN_ID" > config
          echo "TARGET_HOSTNAME=piscsi" >> config
          echo "ENABLE_SSH=1" >> config
          echo "LOCALE_DEFAULT=en_US.UTF-8" >> config
          echo "KEYBOARD_KEYMAP=us" >> config
          echo "KEYBOARD_LAYOUT=\"English (US)\"" >> config
          echo -----------------------------------------------------
          cat config
        working-directory: pi-gen

      - name: Run the Raspberry Pi build generation tool
        run: sudo ./build.sh
        working-directory: pi-gen

      - name: List the files in the deploy directory
        run: |
          echo -----------------------------------------------------
          ls -alh pi-gen/deploy/
          echo -----------------------------------------------------
          
      - name: Archive the build artifacts
        uses: actions/upload-artifact@v2.2.4
        with:
          # Artifact name
          name: raspberry-pi-image
          # A file, directory or wildcard pattern that describes what to upload
          path: pi-gen/deploy/*
