From 47862890e71327ae44a175bcbb3dbfe6ceeb2510 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Fri, 30 Apr 2021 22:33:35 -0400 Subject: [PATCH] if KO_DOCKER_REPO is set, skip login and propagate it --- .github/workflows/use-action.yaml | 20 ++++++++++++++++---- README.md | 4 ++-- action.yml | 16 +++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.github/workflows/use-action.yaml b/.github/workflows/use-action.yaml index e15d610..1a683b5 100644 --- a/.github/workflows/use-action.yaml +++ b/.github/workflows/use-action.yaml @@ -22,18 +22,30 @@ jobs: - uses: imjasonh/setup-ko@main - run: ko publish --bare ./ - # Install an old release. - - uses: imjasonh/setup-ko@main + - name: Install old release + uses: imjasonh/setup-ko@main with: version: v0.8.0 - name: Check installed version run: ko version | grep 0.8.0 - # Install from tip - - uses: imjasonh/setup-ko@main + - name: Install from tip + uses: imjasonh/setup-ko@main with: version: tip - name: Check installed version (tip) run: | sudo rm /usr/local/bin/ko # Uninstall previous versions. ko version + + # If KO_DOCKER_REPO is set during setup, it's set for future steps. + - name: Pre-set KO_DOCKER_REPO + uses: imjasonh/setup-ko@main + env: + KO_DOCKER_REPO: already-set + - name: Check pre-set KO_DOCKER_REPO + run: | + if [[ ${KO_DOCKER_REPO} -ne "already-set"]]; then + echo "${KO_DOCKER_REPO} != already-set" + exit 1 + fi diff --git a/README.md b/README.md index 44c0af6..cd87ba6 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ _That's it!_ This workflow will build and publish your code to [GitHub Container 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`. -:warning: **Note:** The action _sets_ `KO_DOCKER_REPO` for all subsequent steps. -If you define `KO_DOCKER_REPO` as a workflow-wide environment variable, its value will be modified after the action runs. +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. 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)) diff --git a/action.yml b/action.yml index 9621b6e..115620a 100644 --- a/action.yml +++ b/action.yml @@ -41,9 +41,15 @@ runs: curl -fsL https://github.com/google/ko/releases/download/${tag}/ko_${tag:1}_${os}_x86_64.tar.gz | sudo tar xzf - -C /usr/local/bin ko fi - # NB: username doesn't seem to matter. - echo "${{ github.token }}" | ko login ghcr.io --username "dummy" --password-stdin + if [[ ! -z ${KO_DOCKER_REPO} ]]; then + echo "KO_DOCKER_REPO is already set" + echo "Skipping login to ghcr.io and passing KO_DOCKER_REPO=${KO_DOCKER_REPO} on to future steps" + echo "KO_DOCKER_REPO=${KO_DOCKER_REPO}" >> $GITHUB_ENV + else + # NB: username doesn't seem to matter. + echo "${{ github.token }}" | ko login ghcr.io --username "dummy" --password-stdin - # Set KO_DOCKER_REPO for future steps. - echo "KO_DOCKER_REPO=ghcr.io/${{ github.repository }}" - echo "KO_DOCKER_REPO=ghcr.io/${{ github.repository }}" >> $GITHUB_ENV + # Set KO_DOCKER_REPO for future steps. + echo "KO_DOCKER_REPO=ghcr.io/${{ github.repository }}" + echo "KO_DOCKER_REPO=ghcr.io/${{ github.repository }}" >> $GITHUB_ENV + fi