2023-02-15 22:43:34 +08:00
# GitHub Action to install and setup [`ko`](https://github.com/ko-build/ko)
2021-04-27 21:41:07 +08:00
2023-01-13 05:43:19 +08:00
[![Build ](https://github.com/ko-build/setup-ko/actions/workflows/use-action.yaml/badge.svg )](https://github.com/ko-build/setup-ko/actions/workflows/use-action.yaml)
2021-04-27 21:41:07 +08:00
2022-09-07 02:01:41 +08:00
> :warning: Note: `ko` recently [moved to its own GitHub org](https://github.com/ko-build/ko/issues/791), which broke `setup-ko@v0.5` if the `ko` version wasn't specified.
>
2023-01-13 05:43:19 +08:00
> To fix this, either upgrade to [`setup-ko@v0.6`](https://github.com/ko-build/setup-ko/releases/tag/v0.6) or specify `version`
2022-09-07 02:01:41 +08:00
2021-04-27 21:41:07 +08:00
## Example usage
```yaml
name: Publish
on:
push:
branches: ['main']
jobs:
publish:
name: Publish
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
2023-05-17 01:44:36 +08:00
go-version: '1.20.x'
2021-04-27 21:41:07 +08:00
- uses: actions/checkout@v2
2023-01-13 05:43:19 +08:00
- uses: ko-build/setup-ko@v0.6
2022-09-07 02:20:30 +08:00
- run: ko build
2021-04-27 21:41:07 +08:00
```
_That's it!_ This workflow will build and publish your code to [GitHub Container Regsitry ](https://ghcr.io ).
2021-10-28 23:14:58 +08:00
By default, the action sets `KO_DOCKER_REPO=ghcr.io/[owner]/[repo]` for all subsequent steps, and uses the `${{ github.token }}` to authorize pushes to GHCR.
2021-04-27 21:41:07 +08:00
2023-02-15 22:43:34 +08:00
See [documentation for `ko` ](https://ko.build/configuration/ ) to learn more about configuring `ko` .
2021-05-01 02:24:39 +08:00
2023-01-13 05:43:19 +08:00
The action works on Linux and macOS [runners ](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners ).
If you'd like support for Windows runners, [let us know ](https://github.com/ko-build/setup-ko/issues/new )!
2021-04-29 11:11:22 +08:00
2021-04-27 21:41:07 +08:00
### Select `ko` version to install
2023-02-15 22:43:34 +08:00
By default, `ko-build/setup-ko` installs the [latest released version of `ko` ](https://github.com/ko-build/ko/releases ).
2021-04-27 21:41:07 +08:00
You can select a version with the `version` parameter:
```yaml
2023-01-13 05:43:19 +08:00
- uses: ko-build/setup-ko@v0.6
2021-04-27 21:41:07 +08:00
with:
2022-09-07 02:20:30 +08:00
version: v0.11.2
2021-04-27 21:41:07 +08:00
```
2022-01-07 22:24:36 +08:00
To build and install `ko` from source using `go install` , specify `version: tip` .
2021-04-27 21:41:07 +08:00
### Pushing to other registries
2023-01-13 05:43:19 +08:00
By default, `ko-build/setup-ko` configures `ko` to push images to [GitHub Container Registry ](https://ghcr.io ), but you can configure it to push to other registries as well.
2021-04-27 21:41:07 +08:00
2021-10-28 23:14:58 +08:00
If `KO_DOCKER_REPO` is already set when `setup-ko` runs, it will skip logging in to ghcr.io and will propagate `KO_DOCKER_REPO` for subsequent steps.
To do this, you should provide credentials to authorize the push.
2021-04-27 21:41:07 +08:00
You can use [encrypted secrets ](https://docs.github.com/en/actions/reference/encrypted-secrets ) to store the authorization token, and pass it to `ko login` before pushing:
2022-11-24 22:32:14 +08:00
```yaml
2021-10-28 23:14:58 +08:00
steps:
...
2023-01-13 05:43:19 +08:00
- uses: ko-build/setup-ko@v0.6
2021-10-28 23:14:58 +08:00
env:
KO_DOCKER_REPO: my.registry/my-repo
2021-04-27 21:41:07 +08:00
- env:
auth_token: ${{ secrets.auth_token }}
run: |
echo "${auth_token}" | ko login https://my.registry --username my-username --password-stdin
2022-09-07 02:20:30 +08:00
ko build
2021-04-27 21:41:07 +08:00
```
2021-04-29 10:04:23 +08:00
### Release Integration
2021-04-27 21:41:07 +08:00
2023-02-15 22:43:34 +08:00
In addition to publishing images, `ko` can produce YAML files containing references to built images, using [`ko resolve` ](https://ko.build/features/k8s )
2021-04-27 21:41:07 +08:00
2021-04-30 22:56:10 +08:00
With this action, you can use `ko resolve` to produce output YAML that you then attach to a GitHub Release using the [GitHub CLI ](https://cli.github.com ).
2021-04-29 10:04:23 +08:00
For example:
2021-04-27 21:41:07 +08:00
2021-04-29 10:04:23 +08:00
```yaml
2021-04-30 22:56:10 +08:00
name: Publish Release YAML
2021-04-29 10:04:23 +08:00
on:
2021-04-30 22:56:10 +08:00
release:
types: ['created']
2021-04-29 10:04:23 +08:00
jobs:
2021-04-30 22:56:10 +08:00
publish-release-yaml:
name: Publish Release YAML
2021-04-29 10:04:23 +08:00
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
2023-05-17 01:44:36 +08:00
go-version: '1.20.x'
2021-04-29 10:04:23 +08:00
- uses: actions/checkout@v2
2023-01-13 05:43:19 +08:00
- uses: ko-build/setup-ko@v0.6
2021-04-29 10:04:23 +08:00
2021-04-30 22:56:10 +08:00
- name: Generate and upload release.yaml
2021-04-29 10:04:23 +08:00
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2021-04-30 22:56:10 +08:00
run: |
tag=$(echo ${{ github.ref }} | cut -c11-) # get tag name without tags/refs/ prefix.
ko resolve -t ${tag} -f config/ > release.yaml
gh release upload ${tag} release.yaml
2021-04-27 21:41:07 +08:00
```
2021-04-29 10:04:23 +08:00
### A note on versioning
2023-01-13 05:43:19 +08:00
The `@v0.X` in the `uses` statement refers to the version _of the action definition in this repo._
2021-04-27 21:41:07 +08:00
2023-01-13 05:43:19 +08:00
Regardless of what version of the action definition you use, `ko-build/setup-ko` will install the latest released version of `ko` unless otherwise specified with `version:` .