# Generic packages

> Publish any file to a repository as a versioned package, and download it by version or by an alias such as latest, with curl, the CLI or a pipeline step.
A generic package is a set of files with a name and versions: a compiled
library, a data bundle, an installer. Use one for anything that is built once
and fetched by something else, and that is not an image or an npm package.

```
https://registry.branchd.run/generic/<repository>/<package>/<version>/<file>
```

- **`<repository>`** is the repository's full path, such as `acme/tools/core`.
  The package belongs to it.
- **`<package>`, `<version>` and `<file>`** are names you choose: letters,
  digits and `.` `_` `+` `-`, starting with a letter or digit, at most 200
  characters, never a slash. Names are not case-sensitive. A version can be a
  release number or a commit id.
- **Every file records its size and SHA-256** when it is published.
- **A published file is never overwritten.** Publishing the same name again
  in the same version is refused. Publish a new version.

The examples below read a token from `BRANCHD_TOKEN`. On your own machine,
set it to a [personal API key](https://docs.branchd.run/branchd/access/personal-keys) or to
`$(branchd auth token)`. In a pipeline job it is already set. See
[Access](https://docs.branchd.run/branchd/packages/access) for which token may do what.

## Publish a file

```sh
curl -f --user "token:$BRANCHD_TOKEN" --upload-file core.so \
  https://registry.branchd.run/generic/acme/tools/core/core/1.4.2/core.so
```

The username is ignored. The answer is the file as recorded:

```json
{"package":"core","version":"1.4.2","file":"core.so","size":18734112,"sha256":"95346e21…"}
```

- **The package and the version are created by the first file** published to
  them. Publish a version's other files the same way.
- **To have branchd check the bytes**, send their SHA-256 in an
  `X-Checksum-Sha256` header. A mismatch is refused and nothing is stored.
- **A second publish of the same file answers `409`**, whatever its bytes.
- **`latest` is never a version.** It is an [alias](https://docs.branchd.run/branchd/packages/generic#aliases), and a publish
  to it is refused.
- **One file is at most 5 GiB.** A `PUT` passes through branchd. For large
  files prefer the CLI or the pipeline step, which send the bytes straight to
  storage.

With the CLI (`branchd` 0.63 or later), which checks each file's SHA-256 on
arrival and can point an alias at the version once every file is up:

```sh
branchd package upload core 1.4.2 core.so core.info --alias latest -R acme/tools/core
```

Inside a clone of the repository, `-R` can be left out.

## Download a file

```sh
curl -fL --user "token:$BRANCHD_TOKEN" -O \
  https://registry.branchd.run/generic/acme/tools/core/core/1.4.2/core.so
```

- **`-L` is needed**: the answer is a redirect to a short-lived signed URL.
- **`<version>` may be an alias**: `…/core/latest/core.so`.
- **The checksum comes with the answer**, in `X-Checksum-Sha256`, with the
  version an alias resolved to in `X-Package-Version`. A `HEAD` request
  answers both, and the size, without downloading:

```sh
curl -sI --user "token:$BRANCHD_TOKEN" \
  https://registry.branchd.run/generic/acme/tools/core/core/latest/core.so
```

- **Where the repository's packages are public**, leave the credential out.
  See [Public packages](https://docs.branchd.run/branchd/packages/access#public-packages).

With the CLI, which downloads every file of a version and verifies each
against its recorded SHA-256:

```sh
branchd package download core latest -R acme/tools/core            # every file, into the current directory
branchd package download core 1.4.2 core.so --dir vendor -R acme/tools/core
```

`branchd package list` shows a repository's packages, and
`branchd package view core` one package's aliases and versions.

## Aliases

An alias is a movable name for one version of a package. `latest` is the
usual one; `stable`, `beta` or anything else works the same. A download URL,
the CLI and the pipeline step take an alias wherever they take a version.

- **Publishing a file never moves an alias.** You move it once the version
  is complete, so nobody downloads half of a version through `latest`.
- **An alias cannot have the name of one of the package's versions**, and a
  version cannot be named `latest`.
- **Removing an alias keeps the version.** Deleting a version removes the
  aliases that named it.

```sh
branchd package alias set core latest 1.4.2 -R acme/tools/core
branchd package alias remove core stable -R acme/tools/core
```

In the console, open the package and use **Set an alias**, or the edit and
delete buttons on an alias's row.

## In a pipeline

Two steps publish and download generic packages with the job's own token.
This pipeline publishes a build's file under the commit's short id, points
`latest` at it, and downloads it again by alias in a second job:

```yaml title=".branchd/pipelines/packages.yaml"
# .branchd/pipelines/packages.yaml
# Publishes a build's files as a generic package and points latest at it,
# then downloads them again by alias in a second job.
name: Packages
on:
  push:
    branches: [main]
permissions:
  packages: write
jobs:
  publish:
    image: alpine:3.22
    outputs:
      version: ${{ steps.publish.outputs.version }}
    steps:
      - run: |
          mkdir -p dist
          echo "built from $CI_COMMIT_SHA" > dist/hello.txt
      - id: publish
        uses: branchd/steps/forge/package-publish@v1
        isolated: true
        workspace: [dist/**]
        with:
          package: hello
          version: $CI_COMMIT_SHORT_SHA
          files: [dist/hello.txt]
          aliases: [latest]
  fetch:
    needs: [publish]
    image: alpine:3.22
    permissions:
      packages: read
    steps:
      - run: mkdir -p vendor && touch vendor/.keep
      - uses: branchd/steps/forge/package-download@v1
        isolated: true
        workspace: [vendor/**]
        with:
          package: hello
          version: latest
          path: vendor
      - run: cat vendor/hello.txt
```

- **`permissions: packages: write`** lets the job's token publish.
  `packages: read` is enough to download. A write needs a run of a protected
  branch: see [The job token](https://docs.branchd.run/branchd/pipelines/job-token).
- **`branchd/steps/forge/package-publish@v1`** takes `package`, `files` (paths or
  globs, each published under its base name, or `<name>=<path>`), and
  `version` or `version_file` (a file holding the version, for one an earlier
  step computes). `aliases` are pointed at the version once every file is
  published. `if_exists: keep` leaves an already published file alone instead
  of failing, for a re-run of the same commit. Its outputs are `version` and
  `files`.
- **`branchd/steps/forge/package-download@v1`** takes `package`, and optionally
  `version` (a version or an alias; `latest` when left out), `files` (every
  file of the version when left out) and `path` (the directory to write
  into). Each file is checked against its recorded SHA-256.
- **Both are typed steps**, so among `run:` steps they are marked
  `isolated: true`, and `workspace:` names the directory they share with the
  other steps. See
  [Isolate a step from the job](https://docs.branchd.run/branchd/pipelines/isolated-steps).
- **`repository:`** on either step names another repository's package. That
  needs a [CI grant](https://docs.branchd.run/branchd/packages/access#another-repositorys-jobs) there.

A `run:` step can also use `curl` exactly as above: `$BRANCHD_TOKEN` is the
job's token.

## Delete

Deleting is permanent: the stored bytes go, and download links stop working
at once. It needs maintain access to the repository.

```sh
branchd package delete core 1.4.0 core.info -R acme/tools/core   # one file
branchd package delete core 1.4.0 -R acme/tools/core             # one version, its files and aliases
branchd package delete core -R acme/tools/core                   # the whole package
```

- **A version left with no file is removed**, with the aliases that named it.
- **A deleted version's name can be published again.**
- **A pipeline job never deletes.**

In the console, each needs its name typed to confirm.

## Retention

By default nothing is ever deleted. A repository can set a retention policy
for its generic packages, on its **Settings › Releases and registry** tab:

- **Keep the newest _N_ versions** of each package;
- **Keep everything published within _D_ days**;
- a version **an alias names is always kept**.

A version is deleted only when none of the three keeps it. **Dry run** on the
same panel lists what the policy would delete now, without deleting anything.
branchd applies the policy on a schedule; there is no button to apply it now. Setting or
removing a policy needs maintain access.

## The API

`xeonr.forge.v1.GenericPackageService` does everything above, and is what the
console and the CLI use:

| Method                                                                                                                                       | Does                                                                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `ListGenericPackages`, `GetGenericPackage`                                                                                                   | A repository's packages: aliases, newest version, version count and download prefix                                                        |
| `ListGenericPackageVersions`, `GetGenericPackageVersion`                                                                                     | Versions with their files: name, size, SHA-256 and download URL                                                                            |
| `GetGenericPackageFileDownload`                                                                                                              | A short-lived signed URL for one file, which needs no credential                                                                           |
| `CreateGenericPackageFile`, `CompleteGenericPackageFile`                                                                                     | Declare a file with its size and SHA-256, `PUT` the bytes to the upload URL it answers, then complete it. How the CLI and the step upload. |
| `SetGenericPackageAlias`, `DeleteGenericPackageAlias`                                                                                        | Move or remove an alias                                                                                                                    |
| `DeleteGenericPackageFile`, `DeleteGenericPackageVersion`, `DeleteGenericPackage`                                                            | Delete; each takes the name again as `confirm_file`, `confirm_version` or `confirm_package`                                                |
| `GetGenericPackageRetentionPolicy`, `SetGenericPackageRetentionPolicy`, `DeleteGenericPackageRetentionPolicy`, `PlanGenericPackageRetention` | The retention policy and its dry run                                                                                                       |

Calls are made as on [The job token](https://docs.branchd.run/branchd/pipelines/job-token#calling-the-api)
page: an HTTP `POST` of JSON with a bearer token.

Source: https://docs.branchd.run/branchd/packages/generic
