rust_lib_example 0.2.0

Rust project example
Documentation
name: CD

on: workflow_dispatch

env:
  CARGO_TERM_COLOR: always
  RUST_VERSION_STABLE: 1.51.0
  CRATE_PATHS: .

jobs:

  check-version:
    runs-on: ubuntu-18.04
    outputs:
      version: ${{ steps.retrieve-version.outputs.version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Check CHANGELOG.md file exists
        run: '[ -f CHANGELOG.md ]'
      - name: Check unreleased version is defined in CHANGELOG.md
        run: grep '^\#\# \[Unreleased\] - yyyy-mm-dd$' CHANGELOG.md
      - name: Check and retrieve version
        id: retrieve-version
        run: bash .github/workflows/scripts/get_version.sh

  test-ubuntu:
    runs-on: ubuntu-18.04
    needs: check-version
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION_STABLE }}
          profile: minimal
          override: true
      - name: Build
        run: cargo build --all-targets
      - name: Test
        run: cargo test

  test-windows:
    runs-on: windows-2019
    needs: check-version
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION_STABLE }}
          profile: minimal
          override: true
      - name: Build
        run: cargo build --all-targets
      - name: Test
        run: cargo test

  test-macos:
    runs-on: macos-10.15
    needs: check-version
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION_STABLE }}
          profile: minimal
          override: true
      - name: Build
        run: cargo build --all-targets
      - name: Test
        run: cargo test

  check-publication:
    environment: Deployment
    runs-on: ubuntu-18.04
    needs: check-version
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION_STABLE }}
          profile: minimal
          override: true
      - name: Check publication
        run: bash .github/workflows/scripts/check_publication.sh
        env:
          CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

  publish:
    environment: Deployment
    runs-on: ubuntu-18.04
    needs:
      - test-ubuntu
      - test-windows
      - test-macos
      - check-publication
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION_STABLE }}
          profile: minimal
          override: true
      - name: Publish
        run: bash .github/workflows/scripts/run_publication.sh
        env:
          CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

  create-release:
    environment: Deployment
    runs-on: ubuntu-18.04
    needs:
      - check-version
      - publish
    steps:
      - name: Install dependencies
        run: sudo apt-get install node-semver -y
      - name: Checkout
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.GIT_TOKEN }}
      - name: Apply version and date in CHANGELOG.md file
        run: sed -i "s/^\#\# \[Unreleased\] - yyyy-mm-dd$/\#\# [${{needs.check-version.outputs.version}}] - $(date +'%Y-%m-%d')/g" CHANGELOG.md
      - name: Push changes and create tag
        uses: EndBug/add-and-commit@v7
        with:
          default_author: github_actions
          message: Release v${{needs.check-version.outputs.version}}
          tag: -a v${{needs.check-version.outputs.version}} -m 'Release v${{needs.check-version.outputs.version}}'
      - name: Add section for next version in CHANGELOG.md
        run: sed -i '/\#\# \[${{needs.check-version.outputs.version}}\]/i \#\# [Unreleased] - yyyy-mm-dd\n' CHANGELOG.md
      - name: Increment version in Cargo.toml
        run: bash .github/workflows/scripts/increment_version.sh ${{needs.check-version.outputs.version}}
      - name: Push changes
        uses: EndBug/add-and-commit@v7
        with:
          default_author: github_actions
          message: Prepare next release
      - name: Extract changelog
        run: sed -n '/\#\# \[${{needs.check-version.outputs.version}}/,/\#\# \[/p' CHANGELOG.md | sed '/\#\# \[/d' | sed '1{/^$/d}' | sed '1{/^$/d}' | sed '${/^$/d}' | sed '${/^$/d}' > version_changelog.md
      - name: Create GitHub release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{needs.check-version.outputs.version}}
          release_name: v${{needs.check-version.outputs.version}}
          body_path: version_changelog.md