From d6f156da38cae51fb83c4b0b609fab279d29287f Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Thu, 1 Aug 2024 16:08:18 -0700 Subject: [PATCH 1/4] feat: emulate docker CLI with podman --- .github/workflows/test.yaml | 39 ++++++++++++++++++++++++++++++ README.md | 2 -- action.yml | 6 ++--- docker | 3 +++ setup.sh | 47 ++++++++++++++++++++++++++++++++++++- 5 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/test.yaml create mode 100755 docker diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..2c1bcc9 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,39 @@ +name: Test + +on: + push: + branches: + - main + - releases/* + pull_request: + +jobs: + base-test: + strategy: + matrix: + runner: [ubuntu-latest, ubuntu-24.04, ubuntu-22.04, ubuntu-20.04] + runs-on: ${{ matrix.runner }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Emulate Docker CLI with podman + uses: ./ + - name: Test + run: | + test "$(podman version)" == "$(docker version)" + + test-with-podman-api: + strategy: + matrix: + runner: [ubuntu-latest, ubuntu-24.04, ubuntu-22.04, ubuntu-20.04] + runs-on: ${{ matrix.runner }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Emulate Docker CLI with podman + uses: ./ + with: + podman_api: true + - name: Test + run: | + test "$(podman version)" == "$(docker version)" diff --git a/README.md b/README.md index 1be3d3f..3008c35 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,6 @@ Create a workflow YAML file in your `.github/workflows` directory. An [example w | Input | Description | Default | | --------------------|---------------------|--------------------| | `podman_api` | Enable Podman API and configure `DOCKER_HOST` environment variable | `false` | -| `docker_api` | Disable Docker API if any | `true` | - ### Examples diff --git a/action.yml b/action.yml index 262f13c..263ef1c 100644 --- a/action.yml +++ b/action.yml @@ -11,9 +11,6 @@ inputs: podman_api: description: "Enable Podman API and configure DOCKER_HOST environment variable" default: "false" - docker_api: - description: "Disable Docker API if any" - default: "true" runs: using: composite @@ -21,3 +18,6 @@ runs: - name: Emulate Docker CLI with podman shell: bash run: setup.sh + env: + ENABLE_PODMAN_API: ${{ inputs.podman_api }} + DISABLE_DOCKER_API: ${{ inputs.docker_api }} diff --git a/docker b/docker new file mode 100755 index 0000000..f7e5e8a --- /dev/null +++ b/docker @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +exec podman "$@" diff --git a/setup.sh b/setup.sh index 0c9bb31..830ae23 100755 --- a/setup.sh +++ b/setup.sh @@ -1,3 +1,48 @@ -#!/usr/local/bin env bash +#!/usr/bin/env bash +# Copyright The k8s-crafts Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +set -o errexit +set -o nounset +set -o pipefail + +BIN="$HOME/.bin" +DIR="$(dirname "$(readlink -f "$0")")" + +main() { + if [[ "$(uname)" != "Linux" ]]; then + echo "Only Linux platform is supported." + exit 2 + fi + + # If apt is available, try installing podman-docker package. + # If failed, fall back to method of an executable script. + if command -v apt; then + sudo apt -y install podman-docker || use_fallback_script "$@" + else + use_fallback_script + fi +} + +# This function does the following: +# - Create an executable script named docker. +# - Delegate the execution to podman and pass all its arguments to it. +# - Ensure the script is available on PATH with higher precedence than existing docker command. +use_fallback_script() { + mkdir -p "$BIN"; cat "$DIR/docker" > "$BIN/docker"; chmod +x "$BIN/docker" + echo "PATH=$BIN:$PATH" >> "$GITHUB_ENV" +} + +main "$@" From 5fc0f32f25245082e0041f779d6a261fd8c06fe5 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Thu, 1 Aug 2024 16:32:32 -0700 Subject: [PATCH 2/4] feat: support for podman API --- .github/workflows/test.yaml | 4 +++- action.yml | 1 - setup.sh | 12 +++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 2c1bcc9..fe84141 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -21,7 +21,7 @@ jobs: - name: Test run: | test "$(podman version)" == "$(docker version)" - + test-with-podman-api: strategy: matrix: @@ -37,3 +37,5 @@ jobs: - name: Test run: | test "$(podman version)" == "$(docker version)" + test "$DOCKER_HOST" == "unix:///run/user/$(id -u)/podman/podman.sock" + systemctl --user is-active podman.socket diff --git a/action.yml b/action.yml index 263ef1c..3198395 100644 --- a/action.yml +++ b/action.yml @@ -20,4 +20,3 @@ runs: run: setup.sh env: ENABLE_PODMAN_API: ${{ inputs.podman_api }} - DISABLE_DOCKER_API: ${{ inputs.docker_api }} diff --git a/setup.sh b/setup.sh index 830ae23..f086a67 100755 --- a/setup.sh +++ b/setup.sh @@ -30,10 +30,15 @@ main() { # If apt is available, try installing podman-docker package. # If failed, fall back to method of an executable script. if command -v apt; then - sudo apt -y install podman-docker || use_fallback_script "$@" + sudo apt -y install podman-docker || \ + echo "Failed to install podman-docker. Falling back to an executable script \"docker\"" && use_fallback_script else use_fallback_script fi + + if [[ -n "$ENABLE_PODMAN_API" && "$ENABLE_PODMAN_API" == "true" ]]; then + enable_podman_api + fi } # This function does the following: @@ -45,4 +50,9 @@ use_fallback_script() { echo "PATH=$BIN:$PATH" >> "$GITHUB_ENV" } +enable_podman_api() { + sudo systemctl --user enable --now podman.socket + echo "DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock" >> "$GITHUB_ENV" +} + main "$@" From 6887084979401f2caa7c959254fb05155a17b347 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Thu, 1 Aug 2024 16:37:04 -0700 Subject: [PATCH 3/4] correct executable path --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 3198395..da22a07 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,6 @@ runs: steps: - name: Emulate Docker CLI with podman shell: bash - run: setup.sh + run: ./setup.sh env: ENABLE_PODMAN_API: ${{ inputs.podman_api }} From 0778a4ea7ac3f7407fdddae48d3283331b4c9817 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Thu, 1 Aug 2024 16:41:05 -0700 Subject: [PATCH 4/4] do not use sudo for user services --- setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.sh b/setup.sh index f086a67..b8968c0 100755 --- a/setup.sh +++ b/setup.sh @@ -51,7 +51,7 @@ use_fallback_script() { } enable_podman_api() { - sudo systemctl --user enable --now podman.socket + systemctl --user enable --now podman.socket echo "DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock" >> "$GITHUB_ENV" }