# Ship updates

Check for a new version at launch, download it with progress, offer a restart, and test the whole flow locally before you publish.

The app checks GitHub Releases at launch, downloads a newer version in the background and asks the
user to restart. The CI publishes each release with the metadata files the updater reads.

## Add the modules

```kotlin title="build.gradle.kts"
dependencies {
    implementation("dev.nucleusframework:nucleus.updater-runtime:3.0.0")
    implementation("dev.nucleusframework:nucleus.notification-common:3.0.0")
}
```

## Check, download, offer a restart

```kotlin
val updater = NucleusUpdater {
    provider = GitHubProvider(owner = "myorg", repo = "myapp")
}

fun main(args: Array<String>) = nucleusApplication(args) {
    LaunchedEffect(Unit) {
        val result = updater.checkForUpdates()
        if (result !is UpdateResult.Available) return@LaunchedEffect

        var installer: File? = null
        updater.downloadUpdate(result.info).collect { progress ->
            progress.file?.let { installer = it }
        }
        val file = installer ?: return@LaunchedEffect

        notification(title = "Update ready", message = "Restart to finish the update") {
            button("Restart now") { updater.installAndRestart(file) }
        }.send()
    }

    Window(onCloseRequest = ::exitApplication, title = "My App") { App() }
}
```

`checkForUpdates()` returns `NotAvailable` for store-managed formats (App Store PKG, AppX/MSIX,
Snap, Flatpak): the store updates those. On a per-user NSIS install, `installAndRestart` installs
while the app stays on screen.

The updater replaces the `.app` from the release's ZIP. Add `TargetFormat.Zip` next to
`TargetFormat.Dmg` or `TargetFormat.Pkg`.

## Test before you publish

Simulate an update without a release:

```shell
./gradlew run -Pnucleus.updater.simulate=update
./gradlew run -Pnucleus.updater.simulate=download-error
```

Then point the app at a local build to run the real download and install:

```shell
./gradlew packageNsis
./gradlew run -Pnucleus.updater.feedUrl=build/compose/binaries/main/nsis
```

## Publish from CI

`generate-update-yml` writes the `latest*.yml` files the updater reads, and `publish-release`
uploads them with the installers:

```yaml
- uses: NucleusFramework/Nucleus/.github/actions/generate-update-yml@main
  with:
    artifacts-path: artifacts
    version: ${{ env.VERSION }}
    channel: ${{ env.CHANNEL }} # latest, beta, or alpha
- uses: NucleusFramework/Nucleus/.github/actions/publish-release@main
  with:
    artifacts-path: artifacts
    tag: ${{ env.TAG }}
    release-type: ${{ env.RELEASE_TYPE }} # release, draft, or prerelease
```

## What's next

- [Auto-update](/docs/packaging/auto-update): channels, differential downloads, failed installs.
- [Test updates](/docs/packaging/test-updates): an installed app, and automated tests with `UpdateFeedServer`.
- [CI/CD](/docs/packaging/ci-cd): the full build, sign and release workflow.
- [Code signing](/docs/packaging/code-signing): sign the installers you ship.
