diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..fe84141 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,41 @@ +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)" + test "$DOCKER_HOST" == "unix:///run/user/$(id -u)/podman/podman.sock" + systemctl --user is-active podman.socket 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..da22a07 100644 --- a/action.yml +++ b/action.yml @@ -11,13 +11,12 @@ 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 steps: - name: Emulate Docker CLI with podman shell: bash - run: setup.sh + run: ./setup.sh + env: + ENABLE_PODMAN_API: ${{ inputs.podman_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..b8968c0 100755 --- a/setup.sh +++ b/setup.sh @@ -1,3 +1,58 @@ -#!/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 || \ + 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: +# - 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" +} + +enable_podman_api() { + systemctl --user enable --now podman.socket + echo "DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock" >> "$GITHUB_ENV" +} + +main "$@"