Skip to content

Commit 9128732

Browse files
committed
Add CI/CD
1 parent 58f20d9 commit 9128732

File tree

5 files changed

+351
-0
lines changed

5 files changed

+351
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/ci.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: rust
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- develop
7+
- release/*
8+
pull_request:
9+
branches:
10+
- main
11+
- develop
12+
- release/*
13+
14+
jobs:
15+
build-and-validate:
16+
runs-on: windows-latest
17+
strategy:
18+
matrix:
19+
include:
20+
- toolchain: stable
21+
RUSTDOCFLAGS: "-D warnings"
22+
DOCTYPE: "docrs"
23+
- toolchain: nightly
24+
RUSTDOCFLAGS: "-D warnings --cfg docsrs"
25+
DOCTYPE: "doc"
26+
27+
name: "Build and validate (${{ matrix.toolchain}})"
28+
env:
29+
CARGO_TERM_COLOR: always
30+
RUSTFLAGS: -D warnings
31+
RUSTDOCFLAGS: ${{ matrix.RUSTDOCFLAGS }}
32+
33+
steps:
34+
- name: Checkout Code
35+
uses: actions/checkout@v4
36+
37+
- name: Cache Cargo Registry
38+
uses: actions/cache@v3
39+
with:
40+
path: ~/.cargo/registry
41+
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
42+
restore-keys: ${{ runner.os }}-cargo-registry-
43+
44+
- name: Cache Cargo Git Index
45+
uses: actions/cache@v3
46+
with:
47+
path: ~/.cargo/git
48+
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
49+
restore-keys: ${{ runner.os }}-cargo-index-
50+
51+
- name: Install Rust (Matrix)
52+
shell: pwsh
53+
run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
54+
55+
- name: Lint with Clippy (Stable Only)
56+
if: matrix.toolchain == 'stable'
57+
run: cargo clippy --all-targets --all-features
58+
59+
- name: Check Code Formatting (Stable Only)
60+
if: matrix.toolchain == 'stable'
61+
run: cargo fmt -- --check
62+
63+
- name: Build the Project
64+
run: cargo build --verbose
65+
66+
- name: Run Tests
67+
run: cargo test --verbose
68+
69+
- name: Generate Documentation (${{ matrix.DOCTYPE }}, ${{ matrix.toolchain }})
70+
run: cargo doc --no-deps --all-features
71+
72+
- name: Install and Run Security Audit (Nightly Only)
73+
if: matrix.toolchain == 'nightly'
74+
run: |
75+
cargo install cargo-audit
76+
cargo audit

.github/workflows/deploy.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Create GitHub Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
validate-and-release:
10+
runs-on: windows-latest
11+
env:
12+
CARGO_TERM_COLOR: always
13+
RUSTFLAGS: -D warnings
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Cache Cargo Registry
20+
uses: actions/cache@v3
21+
with:
22+
path: ~/.cargo/registry
23+
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
24+
restore-keys: ${{ runner.os }}-cargo-registry-
25+
26+
- name: Cache Cargo Git Index
27+
uses: actions/cache@v3
28+
with:
29+
path: ~/.cargo/git
30+
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
31+
restore-keys: ${{ runner.os }}-cargo-index-
32+
33+
- name: Install Rust
34+
shell: pwsh
35+
run: rustup update stable && rustup default stable
36+
37+
- name: Install PSSemVer
38+
shell: pwsh
39+
run: |
40+
try {
41+
Import-Module -Name PSSemVer -ErrorAction Stop
42+
} catch {
43+
Write-Host "Installing PSSemVer module..."
44+
Install-Module -Name PSSemVer -Scope CurrentUser -Force -ErrorAction Stop
45+
Import-Module -Name PSSemVer -ErrorAction Stop
46+
}
47+
48+
- name: Validate Tag with PSSemVer
49+
id: validate
50+
shell: pwsh
51+
run: |
52+
$tagName = "${{ github.ref_name }}"
53+
try {
54+
$rawVersion = $tagName -replace '^v', ''
55+
$Version = [PSSemVer]::Parse($rawVersion)
56+
if ($Version.Prerelease) {
57+
echo "::set-output name=prerelease::true"
58+
} else {
59+
echo "::set-output name=prerelease::false"
60+
}
61+
echo "::set-output name=nuget_version::$version.metadata"
62+
} catch {
63+
Write-Error "Tag name does not contain a valid semantic version. Current tag: $tagName"
64+
exit 1
65+
}
66+
continue-on-error: false
67+
68+
- name: Cargo Publish Dry-Run
69+
run: cargo publish --dry-run
70+
71+
- name: Create GitHub Release
72+
id: release
73+
uses: actions/create-release@v1
74+
with:
75+
tag_name: ${{ github.ref_name }}
76+
release_name: |
77+
${{ steps.validate.outputs.prerelease == 'true' && 'Prerelease' || 'Release' }} ${{ github.ref_name }}
78+
body: |
79+
This is a ${{ steps.validate.outputs.prerelease == 'true' && 'prerelease' || 'release' }} of the crate.
80+
You can find the [crate here](https://crates.io/crates/${{ github.repository }}).
81+
This version use is a binding for for the WSLPluginAPI V${{steps.validate.outputs.nuget_version}} avalable on [Nuget](https://www.nuget.org/packages/Microsoft.WSL.PluginApi/${{steps.validate.outputs.nuget_version}}).:
82+
prerelease: ${{ steps.validate.outputs.prerelease }}
83+
env:
84+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
86+
- name: Cargo Publish
87+
run: cargo publish
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
on:
2+
push:
3+
branches:
4+
- release/*
5+
pull_request:
6+
branches:
7+
- release/*
8+
9+
jobs:
10+
version-check:
11+
runs-on: windows-latest
12+
name: "Check crate versions"
13+
env:
14+
CARGO_TERM_COLOR: always
15+
RUSTFLAGS: -D warnings
16+
17+
steps:
18+
- name: Checkout Code
19+
uses: actions/checkout@v4
20+
21+
- name: Cache Cargo Dependencies
22+
uses: actions/cache@v3
23+
with:
24+
path: |
25+
~/.cargo/registry
26+
~/.cargo/git
27+
key: ${{ runner.os }}-cargo-dependencies-${{ hashFiles('**/Cargo.lock') }}
28+
restore-keys: ${{ runner.os }}-cargo-dependencies-
29+
30+
- name: Cache PowerShell Modules
31+
uses: actions/cache@v3
32+
with:
33+
path: |
34+
$env:USERPROFILE\Documents\WindowsPowerShell\Modules
35+
key: ${{ runner.os }}-powershell-modules
36+
restore-keys: ${{ runner.os }}-powershell-modules-
37+
38+
- name: Install Rust
39+
shell: pwsh
40+
run: rustup update stable && rustup default stable
41+
42+
- name: Install PSSemVer
43+
shell: pwsh
44+
run: |
45+
try {
46+
Import-Module -Name PSSemVer -ErrorAction Stop
47+
} catch {
48+
Write-Host "Installing PSSemVer module..."
49+
Install-Module -Name PSSemVer -Scope CurrentUser -Force -ErrorAction Stop
50+
Import-Module -Name PSSemVer -ErrorAction Stop
51+
}
52+
53+
- name: Extract Branch Version
54+
id: extract_version
55+
shell: pwsh
56+
run: |
57+
$branchName = "${{ github.ref_name }}"
58+
try {
59+
$rawVersion = $branchName -replace '^release/', ''
60+
$expectedVersion = [PSSemVer]::Parse($rawVersion)
61+
62+
Write-Output "expected_version=$expectedVersion" | Out-File -FilePath $GITHUB_OUTPUT -Encoding utf8
63+
Write-Host "Expected version for crates: $expectedVersion"
64+
} catch {
65+
Write-Error "Branch name does not contain a valid semantic version. Current branch: $branchName"
66+
exit 1
67+
}
68+
69+
- name: Parse and Compare Crate Versions
70+
shell: pwsh
71+
env:
72+
EXPECTED_VERSION: ${{ steps.extract_version.outputs.expected_version }}
73+
run: |
74+
try {
75+
$expectedSemVer = [PSSemVer]::Parse($env:EXPECTED_VERSION)
76+
} catch {
77+
Write-Error "Expected version '$env:EXPECTED_VERSION' is not a valid semantic version."
78+
exit 1
79+
}
80+
81+
# Retrieve package version using cargo pkgid
82+
$pkgid = cargo pkgid --quiet
83+
if ($pkgid -match "#([\d\.]+)") {
84+
$currentVersion = $matches[1]
85+
} else {
86+
Write-Error "Failed to extract version from cargo pkgid output: $pkgid"
87+
exit 1
88+
}
89+
90+
Write-Host "Current crate version: $currentVersion"
91+
92+
try {
93+
$currentSemVer = [PSSemVer]::Parse($currentVersion)
94+
} catch {
95+
Write-Error "Current version '$currentVersion' is not a valid semantic version."
96+
exit 1
97+
}
98+
99+
if ($currentSemVer.CompareTo($expectedSemVer) -ne 0) {
100+
Write-Error "ERROR: Crate version '$currentSemVer' does not match expected '$expectedSemVer'."
101+
exit 1
102+
} else {
103+
Write-Host "Crate version '$currentSemVer' matches the branch version."
104+
}
105+
106+
publish-dry-run:
107+
needs: version-check
108+
runs-on: windows-latest
109+
name: "Publish Dry-Run"
110+
env:
111+
CARGO_TERM_COLOR: always
112+
RUSTFLAGS: -D warnings
113+
114+
steps:
115+
- name: Checkout Code
116+
uses: actions/checkout@v4
117+
118+
- name: Cache Cargo Dependencies
119+
uses: actions/cache@v3
120+
with:
121+
path: |
122+
~/.cargo/registry
123+
~/.cargo/git
124+
key: ${{ runner.os }}-cargo-dependencies-${{ hashFiles('**/Cargo.lock') }}
125+
restore-keys: ${{ runner.os }}-cargo-dependencies-
126+
127+
- name: Install Rust
128+
shell: pwsh
129+
run: rustup update stable && rustup default stable
130+
131+
- name: Run Tests
132+
shell: pwsh
133+
run: |
134+
echo "Running tests..."
135+
cargo test --all-targets
136+
137+
- name: Publish Dry-Run
138+
env:
139+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
140+
shell: pwsh
141+
run: |
142+
echo "Running cargo publish dry-run..."
143+
cargo publish --dry-run

.github/workflows/release-tag.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Tag on Merge to Master
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
branches:
8+
- main
9+
10+
jobs:
11+
create-tag:
12+
if: ${{ github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/') }}
13+
runs-on: windows-latest
14+
env:
15+
CARGO_TERM_COLOR: always
16+
17+
steps:
18+
- name: Checkout Repository
19+
uses: actions/checkout@v4
20+
21+
- name: Extract Version from Branch Name
22+
id: extract_version
23+
shell: pwsh
24+
run: |
25+
$branchName = "${{ github.event.pull_request.head.ref }}"
26+
$version = $branchName -replace '^release/', ''
27+
Write-Host "Extracted version: $version"
28+
echo "version=$version" >> $GITHUB_ENV
29+
30+
- name: Create Git Tag
31+
id: create_tag
32+
run: |
33+
git config user.name "GitHub Actions"
34+
git config user.email "actions@github.com"
35+
git tag "v${{ env.version }}" -a -m "Release version ${{ env.version }}"
36+
git push origin "v${{ env.version }}"
37+
38+
- name: Verify Tag
39+
run: |
40+
git fetch --tags
41+
if (! git tag | grep -q "v${{ env.version }}") {
42+
echo "Tag creation failed for version ${{ env.version }}."
43+
exit 1
44+
}

0 commit comments

Comments
 (0)