Skip to content

Commit 3a59fb6

Browse files
committed
ci: add build workflows
1 parent 234fcfe commit 3a59fb6

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
lint:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout sources
18+
uses: actions/checkout@v4
19+
- name: Install Rust toolchain
20+
uses: dtolnay/rust-toolchain@stable
21+
with:
22+
components: rustfmt, clippy
23+
- name: Cache cargo artifacts
24+
uses: Swatinem/rust-cache@v2
25+
- name: Cargo fmt
26+
run: cargo fmt --all --check
27+
- name: Cargo clippy
28+
run: cargo clippy --all-targets --all-features -- -D warnings
29+
- name: Cargo test
30+
run: cargo test --all-features -- --nocapture
31+
32+
build:
33+
needs: lint
34+
strategy:
35+
matrix:
36+
include:
37+
- os: ubuntu-latest
38+
target: x86_64-unknown-linux-gnu
39+
- os: macos-latest
40+
target: aarch64-apple-darwin
41+
runs-on: ${{ matrix.os }}
42+
steps:
43+
- name: Checkout sources
44+
uses: actions/checkout@v4
45+
- name: Install Rust toolchain
46+
uses: dtolnay/rust-toolchain@stable
47+
with:
48+
targets: ${{ matrix.target }}
49+
- name: Cache cargo artifacts
50+
uses: Swatinem/rust-cache@v2
51+
with:
52+
shared-key: build-${{ matrix.target }}
53+
- name: Cargo build
54+
run: cargo build --target ${{ matrix.target }}

.github/workflows/release.yml

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
concurrency:
8+
group: release
9+
cancel-in-progress: true
10+
11+
jobs:
12+
prepare:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
publish: ${{ steps.decision.outputs.publish }}
16+
version: ${{ steps.current_version.outputs.result }}
17+
reason: ${{ steps.decision.outputs.reason }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 2
23+
- name: Fetch tags
24+
run: git fetch --tags --force
25+
- name: Detect Cargo.toml change
26+
id: changes
27+
uses: tj-actions/changed-files@v45
28+
with:
29+
files: |
30+
Cargo.toml
31+
- name: Read current version
32+
id: current_version
33+
uses: mikefarah/[email protected]
34+
with:
35+
cmd: yq -p toml '.package.version' Cargo.toml | tr -d '\r\n'
36+
- name: Read previous version
37+
id: previous_version
38+
uses: mikefarah/[email protected]
39+
with:
40+
cmd: |
41+
if git rev-parse HEAD^ >/dev/null 2>&1; then
42+
git show HEAD^:Cargo.toml | yq -p toml '.package.version' | tr -d '\r\n'
43+
else
44+
echo ""
45+
fi
46+
- name: Decide release
47+
id: decision
48+
run: |
49+
set -euo pipefail
50+
publish=false
51+
reason="Cargo.toml unchanged"
52+
current="$(echo "${{ steps.current_version.outputs.result }}" | tr -d '\r\n')"
53+
previous="$(echo "${{ steps.previous_version.outputs.result }}" | tr -d '\r\n')"
54+
if [ "${{ steps.changes.outputs.any_changed }}" = "true" ]; then
55+
if [ -z "$previous" ] || [ "$current" != "$previous" ]; then
56+
if git rev-parse "v${current}" >/dev/null 2>&1; then
57+
reason="Tag v${current} already exists"
58+
else
59+
publish=true
60+
if [ -z "$previous" ]; then
61+
reason="First release detected at version ${current}"
62+
else
63+
reason="Version bumped from ${previous} to ${current}"
64+
fi
65+
fi
66+
else
67+
reason="Cargo.toml changed but [package] version stayed at ${current}"
68+
fi
69+
fi
70+
echo "publish=$publish" >> "$GITHUB_OUTPUT"
71+
echo "reason=$reason" >> "$GITHUB_OUTPUT"
72+
echo "Release decision: $reason"
73+
shell: bash
74+
75+
create_release:
76+
needs: prepare
77+
if: needs.prepare.outputs.publish == 'true'
78+
runs-on: ubuntu-latest
79+
steps:
80+
- name: Checkout
81+
uses: actions/checkout@v4
82+
- name: Create GitHub release
83+
env:
84+
GH_TOKEN: ${{ github.token }}
85+
run: |
86+
VERSION=${{ needs.prepare.outputs.version }}
87+
gh release create "v${VERSION}" \
88+
--title "v${VERSION}" \
89+
--notes "Automated release for version ${VERSION}" \
90+
--target "${GITHUB_SHA}"
91+
92+
build_and_upload:
93+
needs: [prepare, create_release]
94+
if: needs.prepare.outputs.publish == 'true'
95+
strategy:
96+
matrix:
97+
include:
98+
- os: ubuntu-latest
99+
target: x86_64-unknown-linux-gnu
100+
use_cross: false
101+
- os: ubuntu-latest
102+
target: x86_64-unknown-linux-musl
103+
use_cross: true
104+
- os: ubuntu-latest
105+
target: aarch64-unknown-linux-gnu
106+
use_cross: true
107+
- os: ubuntu-latest
108+
target: aarch64-unknown-linux-musl
109+
use_cross: true
110+
- os: macos-latest
111+
target: aarch64-apple-darwin
112+
use_cross: false
113+
runs-on: ${{ matrix.os }}
114+
steps:
115+
- name: Checkout
116+
uses: actions/checkout@v4
117+
- name: Install Rust
118+
uses: dtolnay/rust-toolchain@stable
119+
with:
120+
targets: ${{ matrix.target }}
121+
- name: Cache cargo artifacts
122+
uses: Swatinem/rust-cache@v2
123+
with:
124+
shared-key: release-${{ matrix.target }}
125+
- name: Setup Cross
126+
if: matrix.use_cross == true
127+
run: |
128+
set -euo pipefail
129+
curl -sSfLo /tmp/cross.tar.gz https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-x86_64-unknown-linux-gnu.tar.gz
130+
tar -xzf /tmp/cross.tar.gz -C /tmp
131+
sudo mv /tmp/cross /usr/local/bin/cross
132+
- name: Build release binary
133+
run: |
134+
set -euo pipefail
135+
if [ "${{ matrix.use_cross }}" = "true" ]; then
136+
cross build --release --target ${{ matrix.target }}
137+
else
138+
cargo build --release --target ${{ matrix.target }}
139+
fi
140+
- name: Package binary
141+
run: |
142+
set -euo pipefail
143+
VERSION=${{ needs.prepare.outputs.version }}
144+
TARGET=${{ matrix.target }}
145+
BIN=databend-loki-adapter
146+
ARCHIVE="databend-loki-adapter-${VERSION}-${TARGET}.tar.gz"
147+
mkdir -p dist
148+
tar -czf "dist/${ARCHIVE}" -C "target/${TARGET}/release" "${BIN}"
149+
echo "archive=dist/${ARCHIVE}" >> "$GITHUB_OUTPUT"
150+
id: package
151+
shell: bash
152+
- name: Upload release asset
153+
env:
154+
GH_TOKEN: ${{ github.token }}
155+
run: |
156+
VERSION=${{ needs.prepare.outputs.version }}
157+
gh release upload "v${VERSION}" "${{ steps.package.outputs.archive }}" --clobber

0 commit comments

Comments
 (0)