grelsolar 0.0.0

Sync solar data from Solar-Log to Home Assistant.
Documentation
name: CI

on:
  push:
    branches: ["main"]
    tags: ["*.*.*"]
  pull_request:
    branches: ["main"]

permissions:
  contents: read

env:
  RUSTFLAGS: -D warnings
  CARGO_TERM_COLOR: always

jobs:

  lint:
    name: Lint
    runs-on: ubuntu-latest
    permissions:
      security-events: write # to upload sarif results
    steps:
    - name: checkout
      uses: actions/checkout@v4

    - name: install rust toolchain
      uses: dtolnay/rust-toolchain@stable

    - name: cache rust
      uses: Swatinem/rust-cache@v2

    - name: install clippy-sarif
      uses: taiki-e/cache-cargo-install-action@v2
      with:
        tool: clippy-sarif

    - name: install sarif-fmt
      uses: taiki-e/cache-cargo-install-action@v2
      with:
        tool: sarif-fmt

    - name: cargo clippy sarif
      run: >
        cargo clippy --all-targets --message-format=json
        | clippy-sarif
        | tee clippy-results.sarif
        | sarif-fmt
      continue-on-error: true

    - name: codeql upload sarif
      uses: github/codeql-action/upload-sarif@v3
      with:
        sarif_file: clippy-results.sarif
        wait-for-processing: true

    - name: cargo clippy report
      run: cargo clippy --all-targets --keep-going --

    - name: cargo fmt
      run: cargo fmt --all -- --check

    - name: cargo check
      run: cargo check --all-targets

  version:
    name: Determine Build Version
    runs-on: ubuntu-latest
    outputs:
      BUILD_VERSION: ${{ steps.version.outputs.BUILD_VERSION }}
    steps:
      - name: checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # fetch all history for versioning

      - name: setup python
        uses: actions/setup-python@v4

      - name: git fetch tags
        run: git fetch --tags

      - name: build version from git
        uses: mtkennerly/dunamai-action@v1
        if: github.ref_type == 'branch'
        id: version
        with:
          env-var: BUILD_VERSION
          args: --pattern default-unprefixed --bump --style semver

      - name: build version from tag
        if: startsWith(github.ref, 'refs/tags/')
        id: version_tag
        run: |
          echo "BUILD_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

      - name: set output
        if: github.ref_type == 'branch'
        run: echo "BUILD_VERSION=${BUILD_VERSION}" >> $GITHUB_OUTPUT


  build:
    name: Build and Test
    runs-on: ubuntu-latest
    needs: [version]
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: install rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: cache rust
        uses: Swatinem/rust-cache@v2

      - name: install cargo-llvm-cov
        uses: taiki-e/cache-cargo-install-action@v2
        with:
          tool: cargo-llvm-cov

      - name: install cargo-nextest
        uses: taiki-e/cache-cargo-install-action@v2
        with:
          tool: cargo-nextest

      - name: install cargo-edit
        uses: taiki-e/cache-cargo-install-action@v2
        with:
          tool: cargo-edit

      - name: set version
        run: cargo set-version ${{ steps.version.outputs.BUILD_VERSION }}

      - name: cargo build
        run: cargo build --all-targets

      - name: cargo test doc
        run: cargo test --doc

      - name: test
        run: cargo nextest run --all-features --all-targets --profile ci

      - name: upload test results to codecov
        uses: codecov/test-results-action@v1
        with:
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: run coverage
        run: cargo llvm-cov nextest

      - name: generate coverage report
        run: cargo llvm-cov report --lcov --output-path lcov.info

      - name: upload coverage to codecov
        uses: codecov/codecov-action@v5
        with:
          fail_ci_if_error: true
          files: lcov.info
          token: ${{ secrets.CODECOV_TOKEN }}

  cargo:
    name: Cargo Build and Publish
    if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
    needs: [lint, build, version]
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: install rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: cache rust
        uses: Swatinem/rust-cache@v2

      - name: install cargo-edit
        uses: taiki-e/cache-cargo-install-action@v2
        with:
          tool: cargo-edit

      - name: set version
        run: cargo set-version ${{ steps.version.outputs.BUILD_VERSION }}

      - name: cargo publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }} --allow-dirty

  docker:
    name: Docker Build and Publish
    if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
    needs: [lint, build]
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        platform:
          - linux/amd64
          - linux/arm64
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: docker login
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: docker buildx
        uses: docker/setup-buildx-action@v3

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

      - name: docker metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            grelinfo/grelsolar
          tags: |
            type=ref,event=branch
            type=raw,value=${{ steps.version.outputs.BUILD_VERSION }}

      - name: docker build and push
        uses: docker/build-push-action@v6
        with:
          platforms: ${{ matrix.platform }}
          file: Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}