aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/ci.yml
blob: 18d580efa4484e719e38ca16959cb4ae05434e1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: CI

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

jobs:
  check:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: rust-${{ hashFiles('Cargo.lock') }}

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

      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  test:
    needs: check
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: rust-${{ hashFiles('Cargo.lock') }}

      - name: Run tests
        run: cargo test --all

  create_release:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest

    outputs:
      upload_url: ${{ steps.create.outputs.upload_url }}

    steps:
      - name: Create GitHub Release
        id: create
        uses: actions/create-release@v1
        with:
          tag_name: ${{ github.ref_name }}   # 'v0.1.1'
          release_name: ${{ github.ref_name }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build_and_upload:
    needs: [test, create_release]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            asset_name: oreilly-epub-linux-x86_64
            bin_path: target/release/oreilly-epub
          - os: macos-latest
            asset_name: oreilly-epub-macos-x86_64
            bin_path: target/release/oreilly-epub
          - os: windows-latest
            asset_name: oreilly-epub-windows-x86_64.exe
            bin_path: target/release/oreilly-epub.exe

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: rust-${{ hashFiles('Cargo.lock') }}

      - name: Build release binary
        run: cargo build --release

      - name: Package binary (Unix)
        if: runner.os != 'Windows'
        run: |
          cp "${{ matrix.bin_path }}" "./${{ matrix.asset_name }}"
        shell: bash

      - name: Package binary (Windows)
        if: runner.os == 'Windows'
        run: |
          Copy-Item "${{ matrix.bin_path }}" ".\${{ matrix.asset_name }}"
        shell: pwsh

      - name: Upload to GitHub Release
        uses: actions/upload-release-asset@v1
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ./${{ matrix.asset_name }}
          asset_name: ${{ matrix.asset_name }}
          asset_content_type: application/octet-stream
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}