name: release
on:
push:
branches: [main]
tags: ["v*"]
workflow_dispatch:
inputs:
bump:
description: "Version bump when current Cargo.toml version is already released"
required: false
default: "patch"
type: choice
options:
- patch
- minor
- major
- none
permissions:
contents: write
concurrency:
group: release
cancel-in-progress: false
jobs:
plan:
runs-on: ubuntu-latest
outputs:
sha: ${{ steps.plan.outputs.sha }}
version: ${{ steps.plan.outputs.version }}
tag: ${{ steps.plan.outputs.tag }}
release: ${{ steps.plan.outputs.release }}
crate: ${{ steps.plan.outputs.crate }}
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- id: plan
env:
GH_TOKEN: ${{ github.token }}
EVENT_NAME: ${{ github.event_name }}
DISPATCH_BUMP: ${{ inputs.bump || 'patch' }}
HEAD_COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
if [[ "$GITHUB_REF" != refs/tags/* ]]; then
git fetch origin main --tags
git checkout -B main origin/main
fi
name=$(sed -n 's/^name = "\(.*\)"/\1/p' Cargo.toml | head -1)
version=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)
tag="v$version"
if [[ "$GITHUB_REF" == refs/tags/* && "$GITHUB_REF_NAME" != "$tag" ]]; then
echo "::error::tag $GITHUB_REF_NAME does not match Cargo.toml's version $version"
exit 1
fi
check_published() {
local v="$1"
local t="v$1"
if gh release view "$t" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
release=false
else
release=true
fi
local code
code=$(curl -s -o /dev/null -w '%{http_code}' -A "$GITHUB_REPOSITORY release workflow" \
"https://crates.io/api/v1/crates/$name/$v")
if [ "$code" = 200 ]; then
crate=false
else
crate=true
fi
}
check_published "$version"
# Auto-bump only when the current version is already published everywhere.
if [[ "$release" == "false" && "$crate" == "false" && "$GITHUB_REF" != refs/tags/* ]]; then
bump_kind=""
if [[ "$EVENT_NAME" == "workflow_dispatch" ]]; then
if [[ "$DISPATCH_BUMP" != "none" ]]; then
bump_kind="$DISPATCH_BUMP"
fi
elif [[ "$GITHUB_REF" == "refs/heads/main" ]]; then
if [[ "$HEAD_COMMIT_MSG" == *"[skip release]"* ]]; then
echo "Skipping auto-bump due to [skip release] in commit message."
else
if git rev-parse -q --verify "refs/tags/$tag" >/dev/null; then
changed=$(git diff --name-only "$tag" HEAD)
else
changed=$(git diff --name-only HEAD~1 HEAD)
fi
echo "Changed files since $tag:"
echo "$changed"
if printf '%s\n' "$changed" | grep -Eq '^(src/|scripts/|Cargo\.toml$|Cargo\.lock$|LICENSE)'; then
bump_kind="patch"
else
echo "No release-relevant changes since $tag; skipping auto-bump."
fi
fi
fi
if [[ -n "$bump_kind" ]]; then
new_version=$(python3 - "$version" "$bump_kind" <<'PY'
import pathlib, re, sys
old_ver, kind = sys.argv[1], sys.argv[2]
major, minor, patch = map(int, old_ver.split("."))
if kind == "major":
new_ver = f"{major + 1}.0.0"
elif kind == "minor":
new_ver = f"{major}.{minor + 1}.0"
elif kind == "patch":
new_ver = f"{major}.{minor}.{patch + 1}"
else:
raise SystemExit(f"Unknown bump kind: {kind}")
cargo_toml = pathlib.Path("Cargo.toml").read_text()
updated_toml, n1 = re.subn(
r'(\[package\][^\[]*?^version\s*=\s*")' + re.escape(old_ver) + r'(")',
rf'\g<1>{new_ver}\2',
cargo_toml,
count=1,
flags=re.MULTILINE | re.DOTALL,
)
assert n1 == 1, f"Expected 1 version match in Cargo.toml, got {n1}"
pathlib.Path("Cargo.toml").write_text(updated_toml)
cargo_lock = pathlib.Path("Cargo.lock").read_text()
updated_lock, n2 = re.subn(
r'(\[\[package\]\]\s*\nname\s*=\s*"iphone-cli"\s*\nversion\s*=\s*")' + re.escape(old_ver) + r'(")',
rf'\g<1>{new_ver}\2',
cargo_lock,
count=1,
)
assert n2 == 1, f"Expected 1 version match in Cargo.lock, got {n2}"
pathlib.Path("Cargo.lock").write_text(updated_lock)
print(new_ver)
PY
)
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add Cargo.toml Cargo.lock
git commit -m "Release $new_version"
git push origin HEAD:main
version="$new_version"
tag="v$new_version"
check_published "$version"
fi
fi
sha=$(git rev-parse HEAD)
echo "$name $version ($sha): GitHub release needed: $release, crates.io needed: $crate"
{
echo "sha=$sha"
echo "version=$version"
echo "tag=$tag"
echo "release=$release"
echo "crate=$crate"
} >> "$GITHUB_OUTPUT"
macos:
needs: plan
if: needs.plan.outputs.release == 'true' || needs.plan.outputs.crate == 'true'
runs-on: macos-latest
steps:
- uses: actions/checkout@v5
with:
ref: ${{ needs.plan.outputs.sha }}
- run: rustup target add aarch64-apple-darwin x86_64-apple-darwin
- run: cargo test --locked
- run: cargo build --release --locked --target aarch64-apple-darwin
- run: cargo build --release --locked --target x86_64-apple-darwin
- name: Package
run: |
mkdir -p dist/iph
lipo -create -output dist/iph/iph \
target/aarch64-apple-darwin/release/iph \
target/x86_64-apple-darwin/release/iph
# The merged file comes out unsigned, and Apple silicon will not run
# unsigned code: sign every slice ad hoc.
codesign --force --sign - dist/iph/iph
codesign --verify --verbose dist/iph/iph
lipo -info dist/iph/iph
dist/iph/iph --version
cp LICENSE LICENSE.idevice LICENSE.jktcp dist/iph/
tar -C dist -czf dist/iph-macos-universal.tar.gz iph
(cd dist && shasum -a 256 iph-macos-universal.tar.gz > iph-macos-universal.tar.gz.sha256)
- uses: actions/upload-artifact@v4
with:
name: macos
path: dist/iph-macos-universal.tar.gz*
windows:
needs: plan
if: needs.plan.outputs.release == 'true'
runs-on: windows-latest
steps:
- uses: actions/checkout@v5
with:
ref: ${{ needs.plan.outputs.sha }}
- run: cargo build --release --locked
- name: Package
shell: pwsh
run: |
New-Item -ItemType Directory -Force dist/iph | Out-Null
Copy-Item target/release/iph.exe, LICENSE, LICENSE.idevice, LICENSE.jktcp dist/iph/
dist/iph/iph.exe --version
Compress-Archive -Path dist/iph/* -DestinationPath dist/iph-windows-x86_64.zip
$hash = (Get-FileHash dist/iph-windows-x86_64.zip -Algorithm SHA256).Hash.ToLower()
"$hash iph-windows-x86_64.zip" | Out-File -Encoding ascii -NoNewline dist/iph-windows-x86_64.zip.sha256
- uses: actions/upload-artifact@v4
with:
name: windows
path: dist/iph-windows-x86_64.zip*
release:
needs: [plan, macos, windows]
if: needs.plan.outputs.release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.plan.outputs.tag }}
SHA: ${{ needs.plan.outputs.sha }}
run: |
gh release create "$TAG" dist/* \
--repo "$GITHUB_REPOSITORY" --target "$SHA" --title "$TAG" --generate-notes
crate:
needs: [plan, macos]
if: needs.plan.outputs.crate == 'true'
runs-on: macos-latest
environment: crates-io
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v5
with:
ref: ${{ needs.plan.outputs.sha }}
- uses: rust-lang/crates-io-auth-action@v1
id: auth
- run: cargo publish --locked
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}