From 4bedfc30b73f8459b571f9329fb88a74eb5cee4e Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Thu, 16 Oct 2025 08:50:47 -0300 Subject: [PATCH] feat: input context --- .vscode/settings.json | 2 +- README.md | 23 ++++++++++++++++++++--- action.yml | 31 ++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c60674c..1e4dccc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["elif", "kaniko"] + "cSpell.words": ["elif", "esac", "kaniko"] } diff --git a/README.md b/README.md index c3a7b04..176dc2d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Build OCI images using Kaniko. ### Example: Basic Usage, all options. -```yaml +````yaml jobs: cowsay: runs-on: docker @@ -20,7 +20,24 @@ jobs: registry: docker.example.org username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_PASSWORD }} -``` + +### Example: Custom build context + +```yaml +jobs: + build: + runs-on: docker + steps: + - uses: actions/checkout@v4 + - uses: https://github.com/leandro-costa-oliveira/forgejo-kaniko-action@v3 + with: + context: ./subdir + image: my-user/test-image:latest + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} +```` + +```` ### Example: Multiple Tags, Default registry @@ -34,7 +51,7 @@ jobs: image: my-user/test-image:latest,my-user/test-image:${{ github.sha }} username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_PASSWORD }} -``` +```` ### Credits diff --git a/action.yml b/action.yml index 6856f9f..880b52e 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: dockerfile: description: The Dockerfile to pass to Kaniko required: false + context: + description: Build context directory (absolute or relative to GITHUB_WORKSPACE) + required: false image: description: Name and tag under which to upload the image required: true @@ -31,13 +34,31 @@ runs: mkdir -p /kaniko/.docker echo '{"auths":{"${{ inputs.registry }}":{"auth":"'$(printf "%s:%s" "${{ inputs.username }}" "${{ inputs.password }}" | base64 | tr -d '\n')'"}}}' > /kaniko/.docker/config.json + # Resolve build context + if [ -n "${{ inputs.context }}" ]; then + case "${{ inputs.context }}" in + /*) + CONTEXT="${{ inputs.context }}" + ;; + *) + CONTEXT="$GITHUB_WORKSPACE/${{ inputs.context }}" + ;; + esac + else + CONTEXT="$GITHUB_WORKSPACE" + fi + + echo "Using context: $CONTEXT" + + # Ensure Dockerfile exists within the resolved context if [ -n "${{ inputs.dockerfile }}" ]; then echo "Using Dockerfile from input" - echo "${{ inputs.dockerfile }}" > Dockerfile - elif [ -f "./Dockerfile" ]; then - echo "Using existing ./Dockerfile from workspace" + mkdir -p "$CONTEXT" + echo "${{ inputs.dockerfile }}" > "$CONTEXT/Dockerfile" + elif [ -f "$CONTEXT/Dockerfile" ]; then + echo "Using existing Dockerfile at $CONTEXT/Dockerfile" else - echo "ERROR: No Dockerfile provided and no ./Dockerfile found in workspace" + echo "ERROR: No Dockerfile provided and no Dockerfile found at $CONTEXT/Dockerfile" exit 1 fi @@ -48,4 +69,4 @@ runs: echo "Destinations: $dest_flags" - /kaniko/executor --dockerfile Dockerfile --context $GITHUB_WORKSPACE $dest_flags + /kaniko/executor --dockerfile Dockerfile --context "$CONTEXT" $dest_flags