= Using the Koji build system

[#using_koji_in_fedora]
== Using Koji in Fedora

The https://fedoraproject.org/wiki/Koji[Koji Build System] is Fedora's RPM buildsystem.
Packagers use the `koji` client to request package builds
and get information about the buildsystem.
Koji runs on top of Mock to build RPM packages for specific architectures
and ensure that they build correctly.

You can use the `koji` command directly, or use `fedpkg`,
a script that interacts with the RPM Packaging system and other subsystems,
like `git` and `koji` itself.

[#installing_koji]
=== Installing Koji

Follow xref:Installing_Packager_Tools.adoc[Installing Packager Tools].

[#koji_config]
=== Koji Config

The global-local client configuration file for koji is `/etc/koji.conf`.
You should not need to change this from the defaults
for building Fedora packages.
These allow you to use the primary build system
as well as secondary arch build systems.

[#building_with_fedpkg_targets]
=== Building with fedpkg targets

When building with `fedpkg` within a git repository,
every push is automatically tagged via git.
All you have to do to build the package is to run `fedpkg build`.
This triggers a build request for the branch.
Easy!

It is also possible to target a specific koji tag:

....
fedpkg build --target TARGET
....

For example,
if building on rawhide
against a side-tag for updating API for many packages,
such as `dist-f14-python`,
use the following:

....
fedpkg build --target 'dist-f14-python'
....

[#chained_builds]
==== Chained builds
WARNING: chain-builds only work when building on rawhide.
To chain-build packages to update a released OS version,
https://fedoraproject.org/wiki/Bodhi/BuildRootOverrides[set up an override using bodhi]
requesting packages to be included in the proper buildroot.

Sometimes you want to make sure
that one build has succeeded before launching the next one,
for example when you want to rebuild a package
against a dependency that has just been rebuilt.
In that case, use a chain build with:

....
fedpkg chain-build libwidget libgizmo
....

The current package is added to the end of the CHAIN list.
Colons (`:`) can be used in the CHAIN parameter to define groups of packages.
Packages in any single group will be built in parallel
and all packages in a group must build successfully
and populate the repository
before the next group will begin building.
For example:

....
fedpkg chain-build libwidget libaselib : libgizmo :
....

causes `libwidget` and `libaselib` to be built in parallel,
followed by `libgizmo`,
and then the package in your current directory.
If no groups are defined,
packages will be built sequentially.

If a build fails,
following builds are canceled,
but the builds that already succeeded are pushed to the repository.

[#scratch_builds]
=== Scratch Builds

Sometimes it is useful to be able to build a package against the buildroot
without actually including it in the release.
This is called a scratch build.

The following section covers using koji directly,
as well as the fedpkg tool,
to do scratch builds.

To create a scratch build from changes you haven't committed, do the following:

....
rpmbuild -bs foo.spec
koji build --scratch rawhide foo.srpm
....

From the latest git commit:

....
koji build --scratch rawhide 'git url'
....

If you have committed the changes to git
and you are in the current branch,
you can do a scratch build with `fedpkg`,
which wraps the koji command line tool with the appropriate options:

....
fedpkg scratch-build
....

To run a scratch build for a specific architecture:

....
fedpkg scratch-build --arches <archs>
....

`<archs>`can be a comma separated list of several architectures.

Finally, it is possible to combine the scratch-build command
with a specific koji tag in the form:

....
fedpkg scratch-build --target TARGET
....

Run `fedpkg scratch-build --help` or `koji build --help` for more information.

[#build_failures]
=== Build Failures

If your package fails to build, you get an error, for example:

....
420066 buildArch kernel-2.6.18-1.2739.10.9.el5.jjf.215394.2.src.rpm,
ia64): open (build-1.example.com) -> FAILED: BuildrootError:
error building package (arch ia64), mock exited with status 10
....

Investigate why the build failed by looking at the log files.
If there is a `build.log`, start there.
Otherwise, look at `init.log`.

Each job you successfully start gets a unique task ID,
which is listed in its output.

Logs can be found in the web interface,
in the _Task_ pages for the failed task.
Alternatively, use `koji watch-log`, along with the task ID,
to view the logs.
See the help output for more details.

[#advanced_use_of_koji]
=== Advanced use of Koji

We've tried to make Koji self-documenting wherever possible.
The command line tool prints a list of valid commands,
and each command supports `--help`. For example:

....
$ koji help

Koji commands are:
build                Build a package from source
cancel-task          Cancel a task
help                 List available commands
latest-build         Print the latest rpms for a tag
latest-pkg           Print the latest builds for a tag
[...] 
....

....
$ koji build --help

usage: koji build [options]  tag URL
(Specify the --help global option for a list of other help options)

options:
-h, --help            show this help message and exit
--skip-tag            Do not attempt to tag package
--scratch             Perform a scratch build
--nowait              Don't wait on the build
[...] 
....

[#using_koji_to_generate_a_mock_config_to_replicate_a_buildroot]
==== Using koji to generate a mock config to replicate a buildroot

koji can be used to replicate a build root for local debugging.

....
koji mock-config --help
Usage: koji mock-config [options] name
(Specify the --help global option for a list of other help options)

Options:
  -h, --help            show this help message and exit
  --arch=ARCH           Specify the arch
  --tag=TAG             Create a mock config for a tag
  --task=TASK           Duplicate the mock config of a previous task
  --buildroot=BUILDROOT
                        Duplicate the mock config for the specified buildroot
                        id
  --mockdir=DIR         Specify mockdir
  --topdir=DIR          Specify topdir
  --topurl=URL          url under which Koji files are accessible
  --distribution=DISTRIBUTION
                        Change the distribution macro
  -o FILE               Output to a file
....

For example to get the latest buildroot for `dist-f12-build` run:

....
koji mock-config --tag dist-f12-build --arch=x86_64 --topurl=http://kojipkgs.fedoraproject.org/ dist-f12
....

You must pass `--topurl=http://kojipkgs.fedoraproject.org/` to any mock-config command
to get a working mock-config from Fedora's koji.

[#using_koji_to_control_tasks]
==== Using Koji to control tasks

List tasks:

....
koji list-tasks
....

List only tasks requested by you:

....
koji list-tasks --mine
....

requeue an already-processed task (general syntax is: `koji resubmit [options] taskID`):

....
koji resubmit 3
....

[#building_a_package_with_the_command_line_tool]
==== Building a Package with the command-line tool

Instead of using the `fedpkg` target,
you can also directly use the command line tool, `koji`.

To build a package, the syntax is:

....
koji build <build target> <git URL>
....

For example:

....
koji build dist-f14 'git url'
....

The koji build command creates a build task in Koji.
By default, the tool will wait and print status updates until the build completes.
You can override this with the `--nowait` option.

NOTE: For fedora koji,
the git url MUST be based on pkgs.fedoraproject.org.
Other arbitrary git repos cannot be used for builds.

[#koji_tags_and_packages_organization]
=== Koji tags and packages organization

==== Terminology

In Koji, it is sometimes necessary to distinguish
between a package in general,
a specific build of a package,
and the various rpm files created by a build.
When precision is needed, these terms should be interpreted as follows:

* *Package*: The name of a source rpm.
This refers to the package in general
and not any particular build or subpackage.
For example: kernel, glibc, etc.

* Build: A particular build of a package.
This refers to the entire build:
all arches and subpackages.
For example: kernel-2.6.9-34.EL, glibc-2.3.4-2.19.

* RPM: A particular rpm.
A specific arch and subpackage of a build.
For example: kernel-2.6.9-34.EL.x86_64, kernel-devel-2.6.9-34.EL.s390, glibc-2.3.4-2.19.i686, glibc-common-2.3.4-2.19.ia64

[#tags_and_targets]
==== Tags and targets

Koji organizes packages using tags.
In Koji a tag is roughly a collection of packages:

* Tags support inheritance
* Each tag has its own list of valid packages (inheritable)
* Package ownership can be set per-tag (inheritable)
* When you build you specify a target rather than a tag

A build target specifies where a package should be built
and how it should be tagged afterward.
This allows target names to remain fixed
as tags change through releases.

[#koji_commands_for_tags]
==== Koji commands for tags

===== Targets

You can get a full list of build targets with the following command:

....
$ koji list-targets
....

You can see just a single target with the `--name` option:

....
$ koji list-targets --name f30
Name                           Buildroot                      Destination                   
---------------------------------------------------------------------------------------------
f30                            f30-build                      f30-pending                   
....

This tells you a build for target `f30`
will use a buildroot with packages from the `f30-build` tag
and tag the resulting packages as f30-pending.

You probably do not want to build against rawhide.
If Fedora N is the latest one out,
to build to the next one, choose `f\{N+1}`.

===== Tags

You can get a list of tags with the following command:

....
$ koji list-tags
....

===== Packages

As mentioned above,
each tag has its own list of packages that may be placed in the tag.
To see that list for a tag, use the `list-pkgs` command:

....
$ koji list-pkgs --tag dist-f14
....

The first column is the name of the package,
the second tells you which tag the package entry has been inherited from,
and the third tells you the owner of the package.

[#latest_builds]
===== Latest Builds

To see the latest builds for a tag, use the `latest-pkg` command:

....
$ koji latest-pkg --all dist-f14
....

The output gives you not only the latest builds,
but which tag they have been inherited from
and who built them.
