Skip to content

Commit

Permalink
feat(github-actions): update rust templates
Browse files Browse the repository at this point in the history
Fixes #179
  • Loading branch information
believer committed Jun 29, 2023
1 parent 8f5e72c commit 815fc39
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/commands/github_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn run(no_npm: bool, project_type: Option<ProjectType>) -> Result<()> {
let data = json!({ "name": env!("CARGO_PKG_NAME"), "noNpm": !no_npm });

template::render_dir(project.directory(), ".github/workflows", &data)?;
template::render_file(project.release_config(), ".releaserc", Some(&data))?;
template::render_file(project.release_config(), ".releaserc.json", Some(&data))?;

Ok(())
}
32 changes: 25 additions & 7 deletions src/templates/github_actions/release_config/.releaserc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
["@semantic-release/exec", {
"verifyConditionsCmd": "semantic-release-rust verify-conditions",
"prepareCmd": "semantic-release-rust prepare ${nextRelease.version}",
}],
["@semantic-release/git", {
"assets": ["Cargo.toml", "Cargo.lock", "CHANGELOG.md"]
}]
[
"@semantic-release/exec",
{
"verifyConditionsCmd": "semantic-release-rust verify-conditions",
"prepareCmd": "semantic-release-rust prepare ${nextRelease.version}"
}
],
[
"@semantic-release/git",
{
"assets": ["Cargo.toml", "Cargo.lock", "CHANGELOG.md"]
}
],
[
"@semantic-release/github",
{
"assets": [
{
"path": "artifacts/aarch64-apple-darwin/{{name}}-aarch64-apple-darwin.tar.gz",
"label": "{{name}}-${nextRelease.version}-aarch64-apple-darwin.tar.gz",
"name": "{{name}}-${nextRelease.version}-aarch64-apple-darwin.tar.gz"
}
]
}
]
]
}

37 changes: 22 additions & 15 deletions src/templates/github_actions/rust/pr_check.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
name: Release

on:
pull_request:
branches:
- main

env:
CARGO_TERM_COLOR: always
# Cancel previously running workflows
concurrency:
group: {{{{raw}}}}ci-${{ github.ref }}{{{{/raw}}}}
cancel-in-progress: true

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1

- name: Checkout code
uses: actions/checkout@v3

- name: Setup Rust (nightly)
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@nightly
id: rust-toolchain
with:
profile: minimal
toolchain: nightly
components: rustfmt, clippy
override: true

- name: Cache cargo
uses: actions/cache@v3
id: cache-cargo
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
{{{{raw}}}}key: ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }}{{{{/raw}}}}

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

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

- name: Run formatting
- name: Check formatting
run: cargo fmt --all -- --check

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

195 changes: 141 additions & 54 deletions src/templates/github_actions/rust/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,185 @@ on:
branches:
- main

env:
CARGO_TERM_COLOR: always
# Cancel previously running workflows
concurrency:
group: {{{{raw}}}}ci-${{ github.ref }}{{{{/raw}}}}
cancel-in-progress: true

jobs:
test:
name: Test and create release
name: Test
if: "!contains(github.event.head_commit.message, '[skip ci]')"
runs-on: ubuntu-latest
outputs:
release: {{{{raw}}}}${{ steps.semantic.outputs.new_release_version }}{{{{/raw}}}}
new_release_published: {{{{raw}}}}${{ steps.semantic.outputs.new_release_published }}{{{{/raw}}}}
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Rust (nightly)
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@nightly
id: rust-toolchain
with:
profile: minimal
toolchain: nightly
components: rustfmt, clippy
override: true

- name: Install semantic-release-rust
run: cargo install semantic-release-rust --version 1.0.0-alpha.8
- name: Cache cargo
uses: actions/cache@v3
id: cache-cargo
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: {{{{raw}}}}${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }}{{{{/raw}}}}

- name: Run tests
run: |
cargo clippy --all-targets --all-features -- -D warnings
cargo test --locked
cargo fmt --all -- --check
run: cargo test --locked

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

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

# Analyze commits and determine if a new release should be published
# If a new release should be published, the next version is outputted
next-version:
name: Get next version
runs-on: ubuntu-latest
outputs:
new_release_version: {{{{raw}}}}${{ steps.semantic.outputs.new_release_version }}{{{{/raw}}}}
new_release_published: {{{{raw}}}}${{ steps.semantic.outputs.new_release_published }}{{{{/raw}}}}
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install semantic-release-cargo
uses: taiki-e/install-action@v2
with:
tool: semantic-release-cargo@2

- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
uses: cycjimmy/semantic-release-action@v3
id: semantic
with:
dry_run: true
semantic_version: 17.1.1
extra_plugins: |
@semantic-release/changelog@5.0.1
@semantic-release/exec@5.0
@semantic-release/git@9.0
env:
# GITHUB_TOKEN is added automatically by GitHub
GITHUB_TOKEN: {{{{raw}}}}${{ secrets.GITHUB_TOKEN }}{{{{/raw}}}}
CARGO_REGISTRY_TOKEN: {{{{raw}}}}${{ secrets.CARGO_REGISTRY_TOKEN }}{{{{/raw}}}}

- name: Generate Cargo.lock after release
run: cargo generate-lockfile

- name: Push updated Cargo.lock
uses: EndBug/add-and-commit@v5
with:
message: 'chore(release): update lockfile'
env:
GITHUB_TOKEN: {{{{raw}}}}${{ secrets.GITHUB_TOKEN }}{{{{/raw}}}}
- name: New release should be published
if: steps.semantic.outputs.new_release_published == 'true'
run: |
echo {{{{raw}}}}${{ steps.semantic.outputs.new_release_version }}{{{{/raw}}}}
publish:
name: Publish GitHub release
runs-on: macos-latest
if: {{{{raw}}}}${{ success() && needs.test.outputs.new_release_published == 'true' }}{{{{/raw}}}}
needs: [test]
build:
name: Build (Apple Silicon)
needs: [test, next-version]
if: needs.next-version.outputs.new_release_published == 'true'
runs-on: macOS-latest
outputs:
sha: {{{{raw}}}}${{ steps.release.outputs.SHA }}{{{{/raw}}}}
env:
NAME: {{name}}-{{{{raw}}}}${{ needs.test.outputs.release }}-x86_64-apple-darwin{{{{/raw}}}}
TARGET: x86_64-apple-darwin
NAME: {{name}}-aarch64-apple-darwin
TARGET: aarch64-apple-darwin
CARGO: cargo

steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
- name: Checkout code
uses: actions/checkout@v3
with:
ref: 'main'

- name: Setup Rust (nightly)
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@nightly
id: rust-toolchain
with:
target: {{{{raw}}}}${{ env.TARGET }}{{{{/raw}}}}

- name: Cache cargo
uses: actions/cache@v3
id: cache-cargo
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: {{{{raw}}}}${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.cachekey }}-${{ hashFiles('**/Cargo.lock') }}{{{{/raw}}}}

- name: Install semantic-release-cargo
uses: taiki-e/install-action@v2
with:
profile: minimal
toolchain: nightly
override: true
target: x86_64-apple-darwin
tool: semantic-release-cargo@2

- name: Publish for MacOS
id: publish
- name: Prepare semantic-release for Rust
run: semantic-release-cargo prepare {{{{raw}}}}${{ needs.next-version.outputs.new_release_version }}{{{{/raw}}}}

- name: Install cross
uses: taiki-e/install-action@v2
with:
tool: cross

- name: Configure cross
run: echo "CARGO=cross" >> "$GITHUB_ENV"

- name: Compile release binary
run: {{{{raw}}}}${{ env.CARGO }} build --bin {{name}} --release --target ${{ env.TARGET }}{{{{/raw}}}}

- name: Create release archive
id: release
run: |
cargo build --release --locked --target $TARGET
mkdir "$NAME"
cp "target/$TARGET/release/{{name}}" "$NAME/"
cp target/{{{{raw}}}}${{ env.TARGET }}{{{{/raw}}}}/release/{{name}} "$NAME/"
cp README.md "$NAME/"
tar -czvf "$NAME.tar.gz" "$NAME"
SHA=$(shasum -a 256 "$NAME.tar.gz" | cut -d " " -f 1)
echo "SHA=$SHA" >> $GITHUB_OUTPUT
- name: Upload release artifacts
uses: actions/upload-artifact@v3
with:
name: {{{{raw}}}}${{ env.TARGET }}{{{{/raw}}}}
path: {{{{raw}}}}${{ env.NAME }}.tar.gz{{{{/raw}}}}
if-no-files-found: error
retention-days: 1

release:
name: Create release
needs: [build]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Download release artifacts
uses: actions/download-artifact@v3
with:
path: artifacts

- name: Upload binaries to GitHub releases
uses: svenstaro/upload-release-action@v2
- name: ls artifacts
run: ls -R ./artifacts

- name: Install semantic-release-cargo
uses: taiki-e/install-action@v2
with:
repo_token: {{{{raw}}}}${{ secrets.GITHUB_TOKEN }}{{{{/raw}}}}
file: {{{{raw}}}}${{ env.NAME }}.tar.gz{{{{/raw}}}}
tag: {{{{raw}}}}${{ needs.test.outputs.release }}{{{{/raw}}}}
tool: semantic-release-cargo@2

- name: Semantic Release
uses: cycjimmy/semantic-release-action@v3
id: semantic
with:
semantic_version: 17.1.1
extra_plugins: |
@semantic-release/changelog@5.0.1
@semantic-release/exec@5.0
@semantic-release/git@9.0
env:
GITHUB_TOKEN: {{{{raw}}}}${{ secrets.GITHUB_TOKEN }}{{{{/raw}}}}
CARGO_REGISTRY_TOKEN: {{{{raw}}}}${{ secrets.CARGO_REGISTRY_TOKEN }}{{{{/raw}}}}

0 comments on commit 815fc39

Please sign in to comment.