Repository navigation
Make every deployment create and update flag take effect - #25
Merged
Merged
Conversation
This was referenced Nov 22, 2026
joshk
added a commit
to nerves-hub/nerves_hub_web
that referenced
this pull request
Nov 23, 2026
Adds `/deployments/:name/releases` to the API: list a deployment group's releases, read one, create one, and change whether one is required or what its connecting code is. Until now the only way to make a release over the API was `PUT /deployments/:name` with `firmware` or `archive`, and that request ignores every other group setting in its body. The `nh` CLI has to split one update into as many as three requests, and it can't list releases or edit `required` or the connecting code on a release that already exists (found in nerves-hub/nh#25). A later `nh` PR adds `nh deployment release list|show|create|update` against these endpoints. `PUT /deployments/:name` keeps working as it does, so older CLIs are unaffected. Its OpenAPI description now points at the new endpoints. ## Endpoints All four sit under `/api/orgs/:org_name/products/:product_name/deployments/:name/releases` and accept a user token or a product API key. A release is identified by its `number` within the group. | Method and path | Needs | Returns | | --- | --- | --- | | `GET /releases` | membership | 200, newest first | | `GET /releases/:number` | membership | 200, or 404 | | `POST /releases` | `deployment_group:release:manage` | 201 with a `location` header, or 422 | | `PATCH /releases/:number` | `deployment_group:release:manage` | 200, or 404, or 422 | Every endpoint returns 404 for a group the product doesn't have. They also check `require_every_device`, as the group endpoints do, so a role limited to tagged devices can't use them. `POST` body, flat (not wrapped in `deployment` the way the `PUT` is): ```json { "firmware": "d9f8c63a-1234-5678-abcd-ef0123456789", "archive": "6a1f0c2e-...", "required": false, "description": "Fixes the Wi-Fi reconnect loop", "notes": "Roll out after the field test passes", "connecting_code": "Logger.info(\"connected\")", "connecting_code_mode": "last" } ``` Only `firmware` is required; an `archive` left out or sent as `""` means none. A `null` for `required` or `connecting_code_mode` is a 422; leave them out for the defaults (`false`, `last`). Here the connecting code is the release's own; the `PUT` drops it because there it means the group's. `PATCH` body, every field optional, anything left out keeps its value: ```json { "required": true, "connecting_code": null, "connecting_code_mode": "first" } ``` `null` or `""` removes the connecting code. The firmware, archive, description and notes of a release can't change once it's made, so the endpoint ignores them if sent. `DeploymentRelease` holds which fields each changeset casts; the controller passes the body through. A release, under `data` (a list for `GET /releases`): ```json { "number": 3, "firmware": { "uuid": "d9f8...", "version": "1.0.1", "platform": "rpi0", "architecture": "arm" }, "archive_uuid": null, "required": false, "description": "Fixes the Wi-Fi reconnect loop", "notes": null, "connecting_code": null, "connecting_code_mode": "last", "delta_status": "ready", "is_current": true, "created_by": { "type": "user", "name": "Jane Doe", "email": "jane@example.com" }, "inserted_at": "2026-11-22T10:00:00", "updated_at": "2026-11-22T10:00:00" } ``` The timestamps are UTC written without an offset, as the deployment group's are. `created_by` is `{"type": "api_key", "name": ...}` for a release a product API key made, and `null` for releases made before authors were recorded. `connecting_code_mode` is `first`, `last` or `override`; `delta_status` is `ready`, `preparing` or `failed`. 422s use the usual changeset shape, for example `{"errors": {"firmware": ["The current release already has this firmware and archive"]}}`. ## A permission for releases: `deployment_group:release:manage` Making a release used to need `deployment_group:update`, the same permission as changing the group's settings. Everything done to a release now needs `deployment_group:release:manage` instead, in the web UI, Slack and the API: creating and editing releases, approving, skipping and retrying workflow steps, and retrying or deleting deltas. The built-in `manage` and `admin` roles have it. `deployment_group:update` keeps the group's Settings tab. `PUT /deployments/:name` with `firmware` or `archive` makes a release, so it needs the new permission. A `PUT` without either still needs `deployment_group:update`. Creating a deployment group needs `deployment_group:release:manage` as well as `deployment_group:create`, in the web UI and in `POST /deployments`. A new group starts with a release of the firmware it's given, so without this a role with `deployment_group:create` and `deployment_group:toggle` could ship firmware. A migration gives `deployment_group:release:manage` to every custom role that has `deployment_group:update`, so those roles keep editing and approving releases. A role with `deployment_group:create` but not `deployment_group:update` isn't given it, since that would let it approve steps and send releases on every existing group. Such a role can't create groups until an admin adds the permission. An API key with a custom role gets the permission through that role. ## Deltas are queued after a release is saved Queueing deltas for a new release, for one marked required, or for a group whose delta updates were turned on, ran inside that save's own transaction. Queueing a delta runs a transaction of its own, and when that one failed, Ecto rolled back the outer one too. The release or setting was never saved, and the API answered 500 (or the group's Settings tab crashed, on a `MatchError`). It now happens after the commit, as the web UI's comment always said it did. A delta that can't be queued is logged and sent to Sentry, and devices that need it get the whole firmware. An error raised while queueing is caught the same way, so the save still answers with what it saved and the orchestrator is still told about it. A release that reads `ready` before its deltas are queued would be sent as whole firmware, so a release that will queue deltas now starts `preparing`: a new release, or one marked required, in a group that's on with delta updates on. The recalculation after the queueing settles it, to `ready` if it has nothing to wait for. If the node goes down between the commit and that recalculation, the group's orchestrator settles the release on its two-minute tick, so the worst case is a rollout held back for a few minutes rather than for good. The tick leaves a release changed in the last minute alone, since its deltas may still be on their way into the queue. When a release's delta status changes, the orchestrator reads the group again, so it picks up a release whose own announcement never went out. ## Deploying Saving a custom role on a node still running the old code drops `deployment_group:release:manage`, because that node's role editor has no checkbox for it. If roles might be edited during the rollout, run the migration's statement again once every node is on the new code. It only adds the permission where it's missing: ```sql UPDATE org_roles SET permissions = ARRAY( SELECT DISTINCT p FROM unnest(permissions || ARRAY['deployment_group:release:manage']) AS p ORDER BY p ) WHERE 'deployment_group:update' = ANY(permissions) AND NOT 'deployment_group:release:manage' = ANY(permissions); ``` ## Choices I made without Josh - A `firmware` or `archive` UUID the product doesn't have gets a 422 `does not exist`. The `PUT` instead drops an unknown archive silently and creates a release without it, which a new endpoint has no reason to copy. - Connecting code runs on devices, and editing it takes the same permission as the rest of a release. Anyone holding `deployment_group:release:manage` (a member, or a `manage` API key) can set it over the API as well as in the web UI. - `created_by` includes the user's email. Any member can already list the organization's users with their emails. A product API key can't reach that list, so a key now sees the email of whoever made a release in its product. Say if that should be the name only. - `update_deployment_release/3` takes an API key as well as a user, and its audit entries name the key ("API key CI marked release 2 as required ..."). - `ManagedDeployments.get_deployment_release_by_number/2` is new, and loads a release the same way `list_deployment_releases/1` loads each release. ## Open question: should a new release keep the previous release's archive? The web UI's New release form starts with the current release's archive selected, so a release made there keeps it unless someone clears it. The API doesn't: a release made without an `archive` has none, even when the current release has one. I left the API that way, and its `POST` docs say so, so the CLI can pass the archive along itself. Should the API match the form? 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: U7D Bot <lars+underbot@underjord.io>
The server ignored or rejected several deployment writes: - create without --version or --tag got a 422, because create_changeset requires conditions. The body now always carries a conditions object. - create --state on was ignored, because create_changeset doesn't cast is_active. nh now turns the group on with a PUT after creating it. - update --firmware dropped every other flag, because the controller's release clause only creates a release. The settings (including --state off) now go first, then the release, then --state on, so a release never reaches devices outside the new conditions or a group that is being turned off. Also adds --tag-operator and --notes to create and update, --archive and --required to update, lets --version "" and --tag "" clear a condition, and shows notes, tag operator, archive and whether the current release is required. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshk
force-pushed
the
fix/deployment-create-update
branch
from
November 23, 2026 23:49
435ba0b to
655f146
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the deployment create/update findings from the API review. Every flag on
nh deployment createandnh deployment updatenow takes effect, andshowdisplays the fields it used to leave out.All references are to
nerves_hub_weborigin/main.Bugs fixed
1.
createwithout--versionor--taggot a 422.DeploymentGroup.create_changeset(lib/nerves_hub/managed_deployments/deployment_group.ex) doescast_embed(:conditions, required: true, ...), and the CLI leftconditionsout when it was empty. The create body now always sendsconditions, defaulting to{"tags": [], "version": ""}.2.
create --state onwas ignored.create_changesetcasts onlyname, delta_updatable, platform, architecture, notes, sois_active(andstate) are dropped and every new group starts off.CreateDeploymentnow stops sendingstatein the POST. When--state onis given, it sends a follow-upPUT {"deployment": {"state": "on"}}, which goes throughmaybe_active_from_state→update_changeset. If that PUT fails, the error says the group was created but is still off, and wraps the API error.--state offneeds nothing extra, so it stays a single request.3.
update --firmware Xdropped every other flag.DeploymentGroupController.updatehas two clauses. If the body hasfirmware,firmware_id,archiveorarchive_id, it callscreate_deployment_release, which casts onlydescription, notes, required, connecting_code(_mode)(DeploymentRelease.new_changeset) and ignores state, conditions, delta_updatable and group notes.UpdateDeploymentnow sends two requests when both kinds of change are given:{"deployment": {"firmware", "archive"?, "required"?}}{"deployment": {"state"?, "conditions"?, "delta_updatable"?, "notes"?}}. This request never carriesfirmware/archive, so it reaches theupdate_changesetclause.Order, which the server gives no single request for:
--state off, but not--state on{"state": "on"}, if asked forThe new release then lands only after the conditions and delta setting are in place, and never on a group that's being turned off. A group being turned on starts with the new firmware. Only the requests that are needed get sent:
--firmwarealone is still one PUT. If a later request fails, the error says which steps already took effect and wraps the*APIError.4. Fields the CLI ignored.
Deploymentnow decodesnotes,archive_uuid,conditions.tag_operatorandcurrent_release.required(all indeployment_group_json.ex).showprints Tag operator, Archive, Release required and Notes.--notes(create + update): cast by bothcreate_changesetandupdate_changeset.--tag-operator and|or(create + update):conditions_changesetcaststag_operator(Ecto.Enum [:and, :or]).--archive <uuid>(update only): the release clause resolvesarchivewithArchives.get/2.--required(update only):DeploymentRelease.new_changesetcastsrequired.Decisions (made without Josh; open to change)
--archiveand--requiredare update-only. The create endpoint doesn't accept them; it builds the first release from the firmware alone.CreateDeploymentreturns an error if they're set, rather than silently dropping them.--archiveon its own keeps the current firmware.new_changesetdoesvalidate_required([:firmware]), so an archive-only PUT would get a 422.UpdateDeploymentreads the group first and sends its currentfirmware_uuidalong with the archive.--requiredwithout--firmwareor--archiveis refused locally. A release with the same firmware and archive failsvalidate_differs_from_current_release, so there's nothing forrequiredto apply to.--firmwarewithout--archive, the new release has no archive. That's the server's behaviour (the dashboard's new-release form works the same way). nh doesn't carry the old archive over; the help text says so.--version ""sends"version": ""and--tag ""sends"tags": [].conditions_changesettreats both as clearing.conditionsuseson_replace: :update, so a partial conditions object leaves the other condition fields alone.--firmware ""/--archive ""are refused instead of being treated as unchanged.notesmeans the release's notes, so--notesis only ever sent with the group-settings request. A--release-notesflag could come later.firmware upload --deployNot changed. It sends only
{"deployment": {"firmware": uuid}}, a single request to the release clause, which is exactly what it needs. Likeupdate --firmware, the new release has no archive even if the group's previous release had one. That's worth knowing, but changing it is a product decision, so I left it.OpenAPI spec vs. server code
DeploymentGroupCreationRequest(controllers/api/schemas/deployment_group_schemas.ex) listsstateas a create field, butcreate_changesetnever casts it, so the spec is wrong there. The update schema describesnotesas group notes, but in a request that also hasfirmware,notesgoes to the new release instead.archiveisn't in the update schema, though the controller accepts it. I followed the code in each case.Not touched
internal/api/errors.go, which belongs tofix/nested-changeset-errors(now merged, #20). A 422 such as one onconditions.versionuses that formatting. The branch is based onmainafter #20, and the tests still pass.Review
Ran
code-reviewat levelhighon the diff before committing (signing was blocked at first, so there was no PR yet). It found three things, all fixed:update --firmware X --tag betaon an active group sent the release before the tag change, so X could reach devices that only matched the old tags. Fixed with the settings → release → turn-on order above.createhelp suggestedupdate --requiredworks on its own. It now says required belongs with a new release (--archiveor--firmware).showtable. Each line now gets its own row.Tests
internal/api/deployment_test.go: httptest server that records every request (method, path, body). It covers always-sent conditions, create + turn-on, a failed turn-on, refused release fields on create, settings-only/release-only updates, the split and its order (including--state off), archive-only with a GET for the current firmware, partial-failure errors at each step, a failed first step passed on unchanged, and decoding the new fields.cmd/deployment_test.go: create with every flag (POST then PUT), create with no conditions, no release flags on create, bad--tag-operator, update with firmware + settings (two PUTs, cleared conditions, JSON output), archive update,--requiredvalidation, and the newshowfields.showputs each line of multi-line notes on its own row.go test ./...,go vet ./...andgofmt -l .are clean.Not verified against a live server.
🤖 Generated with Claude Code