kvmi 0.5.0

Safe Rust bindings for libkvmi (v6)
name: CI

on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: clone libkvmi
        uses: actions/checkout@v2
        with:
          repository: bitdefender/libkvmi
          path: libkvmi
          ref: bf5776319e1801b59125c994c459446f0ed6837e

      - name: build and install libkvmi
        run: |
          ./bootstrap
          ./configure
          make
          sudo make install
        working-directory: libkvmi

      - name: install stable toolchain with clippy
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          components: clippy
      - uses: actions/checkout@v1

      - name: build and check for clippy warnings
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: -- -D warnings

  format:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1

      - name: rustfmt check
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: -- --check

  debian_package:
    # create a Debian package with cargo deb to distribute the examples
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v1

      - name: clone libkvmi
        uses: actions/checkout@v2
        with:
          repository: bitdefender/libkvmi
          path: libkvmi
          ref: bf5776319e1801b59125c994c459446f0ed6837e

      - name: build and install libkvmi
        run: |
          ./bootstrap
          ./configure
          make
          sudo make install
        working-directory: libkvmi

      - name: install cargo deb dependencies
        run: sudo apt-get install -y dpkg liblzma-dev

      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo

      - name: install cargo deb
        run: cargo install cargo-deb

      - name: build debian package
        run: cargo deb -- --examples

      - name: upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: kvmi_deb
          # kvmi_x.x.x_amd64.deb
          path: target/debian/*

  release:
    # create a Github release
    # only when
    # - push on master
    # - tag starts with 'v*'
    needs: [ format, build, debian_package ]
    runs-on: ubuntu-20.04
    # output these value to be used by other jobs so they can add assets
    outputs:
      upload_url: ${{ steps.step_upload_url.outputs.upload_url }}
      version: ${{ steps.get_version.outputs.version }}

    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v1

      - name: Get the version
        id: get_version
        run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo

      - name: install cargo changelog
        run: cargo install changelog

      - name: generate changelog
        run: changelog -o changelog.md

      - name: Create a Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get_version.outputs.version }}
          release_name: ${{ steps.get_version.outputs.version }}
          body_path: changelog.md

      - id: step_upload_url
        run: echo "::set-output name=upload_url::${{ steps.create_release.outputs.upload_url }}"

  release_debian:
    # add the debian package in the Github release
    needs: [release]
    runs-on: ubuntu-20.04

    steps:
      # the deploy action below depends on a checkout of the repo
      # otherwise it fails trying to remote the 'origin' remote
      # https://github.com/JamesIves/github-pages-deploy-action/issues/335
      - uses: actions/checkout@v2

      # download artifacts
      - uses: actions/download-artifact@v2
        id: download
        with:
          name: kvmi_deb

      - name: get artifact path and name
        id: artefact
        run: |
          PATHNAME=$(find . -maxdepth 1 -name '*.deb')
          NAME=$(basename $PATHNAME)
          echo ::set-output name=path::${PATHNAME}
          echo ::set-output name=name::${NAME}

      - name: Upload Debian package as Release asset
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.upload_url }}
          asset_path: ${{ steps.artefact.outputs.path }}
          asset_name: ${{ steps.artefact.outputs.name }}
          asset_content_type: application/vnd.debian.binary-package

  publish:
    needs: [release]
    runs-on: ubuntu-20.04

    # publish on crates.io
    # only if push on master, and tag is 'v*'
    # this should be triggered by cargo release, which creates a new tag and pushes to the repo
    # cargo release --no-dev-version --skip-publish minor
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: clone libkvmi
        uses: actions/checkout@v2
        with:
          repository: bitdefender/libkvmi
          path: libkvmi
          ref: bf5776319e1801b59125c994c459446f0ed6837e

      - name: build and install libkvmi
        run: |
          ./bootstrap
          ./configure
          make
          sudo make install
        working-directory: libkvmi

      - uses: actions/checkout@v1

      - name: Publish
        shell: bash
        run: |
          cargo publish --token ${{ secrets.CRATES_TOKEN }}