setup-ko/README.md

108 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

2021-04-27 21:41:07 +08:00
# GitHub Action to install and setup [`ko`](https://github.com/google/ko)
[![Build](https://github.com/imjasonh/setup-ko/actions/workflows/use-action.yaml/badge.svg)](https://github.com/imjasonh/setup-ko/actions/workflows/use-action.yaml)
## Example usage
```yaml
name: Publish
on:
push:
branches: ['main']
jobs:
publish:
name: Publish
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.15
- uses: actions/checkout@v2
2021-05-01 10:46:54 +08:00
- uses: imjasonh/setup-ko@v0.4
2021-04-27 21:41:07 +08:00
- run: ko publish ./
```
_That's it!_ This workflow will build and publish your code to [GitHub Container Regsitry](https://ghcr.io).
By default, the action sets `KO_DOCKER_REPO=ghcr.io/[owner]/[repo]`.
See [documentation for `ko`](https://github.com/google/ko#configuration) to learn more about configuring `ko`.
By default the action sets `KO_DOCKER_REPO` for all subsequent steps.
Instead, if the `env` is already set when the action runs, it will skip logging in to ghcr.io and propagate `KO_DOCKER_REPO` for subsequent steps.
2021-05-01 02:24:39 +08:00
2021-04-29 11:11:22 +08:00
The action works on Linux and macOS [runners](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners), and _should_ work for Windows runners when `ko` releases include Windows binaries (next `ko` release🤞! see [google/ko#339](https://github.com/google/ko/pull/339))
2021-04-27 21:41:07 +08:00
### Select `ko` version to install
By default, `imjasonh/setup-ko` installs the latest released version of `ko`.
You can select a version with the `version` parameter:
```yaml
2021-05-01 10:46:54 +08:00
- uses: imjasonh/setup-ko@v0.4
2021-04-27 21:41:07 +08:00
with:
version: v0.8.0
```
To build and install `ko` from source using `go get`, specify `version: tip`.
### Pushing to other registries
By default, `imjasonh/setup-ko` configures `ko` to push images to [GitHub Container Registry](https://ghcr.io), but you can configure it to push to other registries.
To do this, you need to provide credentials to authorize the push.
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:
```
2021-05-01 10:46:54 +08:00
- uses: imjasonh/setup-ko@v0.4
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
export KO_DOCKER_REPO=my.registry/my-repo
ko publish ./
```
### Release Integration
2021-04-27 21:41:07 +08:00
In addition to publishing images, `ko` can produce YAML files containing references to built images, using [`ko resolve`](https://github.com/google/ko#kubernetes-integration)
2021-04-27 21:41:07 +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).
For example:
2021-04-27 21:41:07 +08:00
```yaml
name: Publish Release YAML
on:
release:
types: ['created']
jobs:
publish-release-yaml:
name: Publish Release YAML
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.15
- uses: actions/checkout@v2
2021-05-01 10:46:54 +08:00
- uses: imjasonh/setup-ko@v0.4
- name: Generate and upload release.yaml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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
```
### A note on versioning
2021-05-01 10:46:54 +08:00
The `@v0.4` in the `uses` statement refers to the version _of the action definition in this repo._
2021-04-27 21:41:07 +08:00
Regardless of what version of the action definition you use, `imjasonh/setup-ko` will install the latest released version of `ko` unless otherwise specified with `version:`.