Skip to content

Commit

Permalink
feat: support git installation (#14)
Browse files Browse the repository at this point in the history
Implement crate installation from git (`cargo install --git`).

- [x] Add new `git`, `branch`, `tag` and `version` input parameters.
- [x] Resolve branches and tag to commit hash with [`git
ls-remote`](https://git-scm.com/docs/git-ls-remote).
- [x] Refactor crate version resolution and installation to support both
crates.io and git sources.
- [x] Update CI tests.
- [x] Update changelog and readme.

Closes #13
  • Loading branch information
baptiste0928 authored Jun 9, 2023
1 parent 87bbb9d commit 05ef67c
Show file tree
Hide file tree
Showing 10 changed files with 649 additions and 338 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on: [push, pull_request]
on: [push]

jobs:
lint:
Expand Down Expand Up @@ -74,12 +74,22 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v3

- name: Install cargo-hack
- name: Install cargo-hack (from crates.io)
uses: ./
with:
crate: cargo-hack
version: ^0.4.4
cache-key: test

- name: Install cargo-sort (from git)
uses: ./
with:
crate: cargo-sort
git: https://github.com/devinr528/cargo-sort
tag: v1.0.9

- name: Print cargo hack version
run: cargo hack --version

- name: Print cargo sort version
run: cargo sort --version
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Git installation is now supported with the `git` input parameter. You can
specify a branch, tag or commit hash.

## [2.0.0] - 2023-03-23
### Added
Expand Down
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,64 @@
This action enables you to run `cargo install` in your GitHub workflows,
and automatically caches the resulting binaries to speed up subsequent builds.

> **Update:** The recent v2 update introduces some breaking changes. Read
> the [changelog] before updating.
| ✨ Recent updates: |
| :--- |
| **v2.1:** Installing crates from git is now supported. |
| **v2.0:** This major update introduces some breaking changes. Read the [changelog] before updating. |

## Features
- Install any Rust binary crate published on [crates.io].
- Install any Rust binary crate from [crates.io] or a git repository.
- Automatically cache installed binaries to avoid compiling them each run.
- Keep crates updated, with an optional version range to avoid breakages.
- Works on Linux, Windows and MacOS runners.

## Usage
The following example steps install the [`cargo-hack`] crate. Read
[Quickstart for GitHub Actions] to learn more about Actions usage.
The following example steps install the [`cargo-hack`] and [`cargo-sort`] crates.
Read [Quickstart for GitHub Actions] to learn more about Actions usage.

```yaml
- name: Install cargo-hack
- name: Install cargo-hack from crates.io
uses: baptiste0928/cargo-install@v2
with:
crate: cargo-hack
version: "^0.5" # You can specify any semver range

- name: Install cargo-sort from git
uses: baptiste0928/cargo-install@v2
with:
crate: cargo-sort
git: https://github.com/devinr528/cargo-sort
tag: v1.0.9 # `branch` and `commit` are also supported

- name: Run cargo hack
run: cargo hack --version
```
If no version is specified, the latest version will be installed. The
`--locked` flag is added by default to avoid breakages due to unexpected
dependencies updates.
If no version or branch/tag/commit is specified, the latest version will be
installed. The `--locked` flag is added by default to avoid breakages due to
unexpected dependencies updates.

### Inputs
### Input parameters
- `crate` *(required)*: Name of the crate to install.
- `version`: Version to install (defaults to the latest version). Supports any
semver range.
semver range. Only used when installing from crates.io, see below for git
installation.
- `features`: Space or comma-separated list of crate features to enable.
- `locked`: Use the crate `Cargo.lock` if available (enabled by default).
This adds `--locked` to the install command arguments.
- `args`: Additional arguments to pass to `cargo install`.
- `cache-key`: Additional string added to the cache key used to manually
invalidate the cache.

#### Git parameters
- `git`: URL of the git repository to install from.
- `branch`: Branch to install from.
- `tag`: Tag to install from.
- `commit`/`rev`: Commit hash to install from.

`branch`, `tag` and `commit`/`rev` are mutually exclusive. If none of them are
specified, the latest commit of the default branch will be used.

### Outputs
- `version`: The version of the crate that has been installed.
- `cache-hit`: A boolean indicating whether the crate was restored from cache.
Expand All @@ -66,7 +85,7 @@ to learn more about caching with GitHub Actions.
follows the following pattern:

```
cargo-install-<crate>-<version>-<hash>
cargo-install-<crate>-<version or commit>-<hash>
```
The hash is derived from the action job and runner os name and the
Expand All @@ -79,6 +98,9 @@ Crates are installed using `cargo install` and the latest version is retrieved
with the [crates.io] API. You can ask to install a specific version by not
using any semver range operator.
If using a git repository, the action will use [`git ls-remote`] to retrieve
the commit hash. The repository is cloned by `cargo install`.
## Contributing
There is no particular contribution guidelines, feel free to open a new PR to
improve the code. If you want to introduce a new feature, please create an
Expand All @@ -87,5 +109,7 @@ issue before.
[changelog]: https://github.com/baptiste0928/cargo-install/releases/tag/v2.0.0
[crates.io]: https://crates.io
[`cargo-hack`]: https://crates.io/crates/cargo-hack
[`cargo-sort`]: https://crates.io/crates/cargo-sort
[`git ls-remote`]: https://git-scm.com/docs/git-ls-remote
[Quickstart for GitHub Actions]: https://docs.github.com/en/actions/quickstart
[Caching dependencies to speed up workflows]: https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
29 changes: 25 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: "cargo-install"
description: "GitHub action for cache-efficient Rust crates installation"
inputs:
# Global input parameters
crate:
description: "Name of the crate to install"
required: true
version:
description: "Version range to install."
required: true
default: "latest"
features:
description: "Features of the crate to enable."
required: false
Expand All @@ -21,6 +18,30 @@ inputs:
cache-key:
description: "Additional key added to the automatic cache key."
required: false

# Crates.io installation
version:
description: "Version of the crate to install."
required: true
default: "latest"

# Git installation
git:
description: "Git repository to install the crate from."
required: false
branch:
description: "Branch to install the crate from."
required: false
tag:
description: "Tag to install the crate from."
required: false
commit:
description: "Commit to install the crate from."
required: false
rev: # alias for commit
description: "Commit to install the crate from."
required: false

outputs:
version:
description: "The version of the crate that has been installed."
Expand Down
Loading

0 comments on commit 05ef67c

Please sign in to comment.