name: Build
on:
  pull_request:
    branches: [ master ]
  push:
    # ci-sandbox is a branch dedicated to testing post-submit code.
    branches: [ master, artifacts-pr ]
    tags:
      - v*
  schedule:
    # run on Mondays at 8AM
    - cron:  '0 8 * * 1'
env:
  # required by Makefile
  UNIX_SHELL_ON_WINDOWS: true

  # PUBLISH_BINARIES=true publishes the binaries to github
  PUBLISH_BINARIES: ${{ secrets.PUBLISH_BINARIES }}

  # where to publish releases for non-tagged commits
  NON_TAG_RELEASE_REPO: ${{ secrets.NON_TAG_RELEASE_REPO }}

  # encrypt various secrets stored as files
  CREDENTIAL_ENCRYPTION_KEY: ${{ secrets.CREDENTIAL_ENCRYPTION_KEY }}
  CREDENTIAL_ENCRYPTION_IV: ${{ secrets.CREDENTIAL_ENCRYPTION_IV }}

  # Apple ID and app-specific password for notarizaton
  APPLEID: ${{ secrets.APPLEID }}
  APPLEIDPASS: ${{ secrets.APPLEIDPASS }}
  KOPIA_UI_NOTARIZE: ${{ secrets.KOPIA_UI_NOTARIZE }}

  # tool to install Windows signing certificate
  WINDOWS_SIGNING_TOOLS_URL: ${{ secrets.WINDOWS_SIGNING_TOOLS_URL }}
  WINDOWS_SIGN_USER: ${{ secrets.WINDOWS_SIGN_USER }}
  WINDOWS_SIGN_AUTH: ${{ secrets.WINDOWS_SIGN_AUTH }}
  WINDOWS_CERT_SHA1: ${{ secrets.WINDOWS_CERT_SHA1 }}
  WINDOWS_SIGN_TOOL: ${{ secrets.WINDOWS_SIGN_TOOL }}

  # macOS signing certificate (base64-encoded), used by Electron Builder
  CSC_LINK: ${{ secrets.CSC_LINK }}
  CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
  MACOS_SIGNING_IDENTITY: ${{ secrets.MACOS_SIGNING_IDENTITY }}

  # used to publish docker images
  DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
  DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}

  # used in Azure tests
  KOPIA_AZURE_TEST_CONTAINER: ${{ secrets.KOPIA_AZURE_TEST_CONTAINER }}
  KOPIA_AZURE_TEST_STORAGE_ACCOUNT: ${{ secrets.KOPIA_AZURE_TEST_STORAGE_ACCOUNT }}
  KOPIA_AZURE_TEST_STORAGE_KEY: ${{ secrets.KOPIA_AZURE_TEST_STORAGE_KEY }}

  # used in B2 tests
  KOPIA_B2_TEST_BUCKET: ${{ secrets.KOPIA_B2_TEST_BUCKET }}
  KOPIA_B2_TEST_KEY: ${{ secrets.KOPIA_B2_TEST_KEY }}
  KOPIA_B2_TEST_KEY_ID: ${{ secrets.KOPIA_B2_TEST_KEY_ID }}

  # used in GCS tests
  KOPIA_GCS_CREDENTIALS_FILE: ${{ secrets.KOPIA_GCS_CREDENTIALS_FILE }}
  KOPIA_GCS_TEST_BUCKET: ${{ secrets.KOPIA_GCS_TEST_BUCKET }}

  # used in S3 tests
  KOPIA_S3_TEST_ENDPOINT: ${{ secrets.KOPIA_S3_TEST_ENDPOINT }}
  KOPIA_S3_TEST_ACCESS_KEY_ID: ${{ secrets.KOPIA_S3_TEST_ACCESS_KEY_ID }}
  KOPIA_S3_TEST_SECRET_ACCESS_KEY: ${{ secrets.KOPIA_S3_TEST_SECRET_ACCESS_KEY }}
  KOPIA_S3_TEST_BUCKET: ${{ secrets.KOPIA_S3_TEST_BUCKET }}
  KOPIA_S3_TEST_REGION: ${{ secrets.KOPIA_S3_TEST_REGION }}
  KOPIA_S3_TEST_STS_ACCESS_KEY_ID: ${{ secrets.KOPIA_S3_TEST_STS_ACCESS_KEY_ID }}
  KOPIA_S3_TEST_STS_SECRET_ACCESS_KEY: ${{ secrets.KOPIA_S3_TEST_STS_SECRET_ACCESS_KEY }}
  KOPIA_S3_TEST_SESSION_TOKEN: ${{ secrets.KOPIA_S3_TEST_SESSION_TOKEN }}

  KOPIA_S3_CREDS: ${{ secrets.KOPIA_S3_CREDS }}
  KOPIA_S3_WASABI_CREDS: ${{ secrets.KOPIA_S3_WASABI_CREDS }}
  KOPIA_S3_WASABI_VERSIONED_CREDS: ${{ secrets.KOPIA_S3_WASABI_VERSIONED_CREDS }}

  # used in rclone tests
  KOPIA_RCLONE_EMBEDDED_CONFIG_B64: ${{ secrets.KOPIA_RCLONE_EMBEDDED_CONFIG_B64 }}

  # used in SFTP tests
  KOPIA_SFTP_TEST_HOST: ${{ secrets.KOPIA_SFTP_TEST_HOST }}
  KOPIA_SFTP_TEST_PORT: ${{ secrets.KOPIA_SFTP_TEST_PORT }}
  KOPIA_SFTP_TEST_USER: ${{ secrets.KOPIA_SFTP_TEST_USER }}
  KOPIA_SFTP_TEST_PATH: ${{ secrets.KOPIA_SFTP_TEST_PATH }}
  KOPIA_SFTP_KEYFILE: ${{ secrets.KOPIA_SFTP_KEYFILE }}
  KOPIA_SFTP_KNOWN_HOSTS_FILE: ${{ secrets.KOPIA_SFTP_KNOWN_HOSTS_FILE }}

  # used in WebDAV tests
  KOPIA_WEBDAV_TEST_URL: ${{ secrets.KOPIA_WEBDAV_TEST_URL }}
  KOPIA_WEBDAV_TEST_USERNAME: ${{ secrets.KOPIA_WEBDAV_TEST_USERNAME }}
  KOPIA_WEBDAV_TEST_PASSWORD: ${{ secrets.KOPIA_WEBDAV_TEST_PASSWORD }}

  # Code Coverage token
  CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        include:
          - os: [self-hosted, ARM64]
          - os: [self-hosted, ARMHF]
    name: Make
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ contains(matrix.os, 'self-hosted') }}
    steps:
    - name: Set up Go.
      uses: actions/setup-go@v2
      with:
        go-version: ^1.16
      id: go
      if: ${{ !contains(matrix.os, 'ARMHF') }}
    - name: Install GoLang for ARMHF
      run: "echo /usr/local/go/bin >> $GITHUB_PATH; mkdir -p /usr/local/go && curl -s -L https://golang.org/dl/go1.16.linux-armv6l.tar.gz | tar -C /usr/local -xz"
      if: ${{ contains(matrix.os, 'ARMHF') }}
    - name: Install Windows-specific packages
      run: "choco install --no-progress -y make zip unzip curl"
      if: ${{ contains(matrix.os, 'windows') }}
    - name: Install macOS-specific packages
      run: "sudo xcode-select -r"
      if: ${{ contains(matrix.os, 'macos') }}
    - name: Check out code into the Go module directory
      uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: Setup
      run: make -j4 ci-setup
    - name: Build 
      run: make ci-build
    - name: Tests
      run: make ci-tests
      continue-on-error: ${{ github.event_name != 'pull_request' }}
    - name: Integration Tests
      run: make -j2 ci-integration-tests
      continue-on-error: ${{ github.event_name != 'pull_request' }}
    - name: Publish
      run: make ci-publish
    - name: Upload Kopia Artifacts
      uses: actions/upload-artifact@v2
      with:
        name: kopia
        path: |
          dist/*.md
          dist/*.rb
          dist/*.zip
          dist/*.tar.gz
          dist/*.rpm
          dist/*.deb
          dist/*.exe
          dist/kopia-ui/*.zip
          dist/kopia-ui/*.tar.gz
          dist/kopia-ui/*.dmg
          dist/kopia-ui/*.rpm
          dist/kopia-ui/*.deb
          dist/kopia-ui/*.exe
          dist/kopia-ui/*.AppImage
          dist/kopia-ui/*.yml
        if-no-files-found: ignore
    - name: Upload Kopia Binary
      uses: actions/upload-artifact@v2
      with:
        name: kopia_binaries
        path: |
          dist/*/kopia
          dist/*/kopia.exe
        if-no-files-found: ignore
  publish:
    name: Stage And Publish Artifacts
    runs-on: ubuntu-latest
    needs: build
    if: ${{ github.event_name != 'pull_request' }}
    steps:
    - 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: Download Artifacts
      uses: actions/download-artifact@v2
      with:
        name: kopia
        path: dist
    - name: Download Kopia Binaries
      uses: actions/download-artifact@v2
      with:
        name: kopia_binaries
        path: dist_binaries
    - name: Display structure of downloaded files
      run: ls -lR dist/ dist_binaries/
    - name: Install CI Credentials
      run: make -j4 ci-credentials
    - name: Stage Release
      run: make stage-release
    - name: Push Github Release
      run: make push-github-release
      env:
        GITHUB_TOKEN: ${{secrets.GH_TOKEN}}
    - name: Publish Other Packages
      run: make publish-packages
      env:
        GITHUB_TOKEN: ${{secrets.GH_TOKEN}}
