Skip to content

Commit

Permalink
Merge pull request #1 from k8s-crafts/initial-action
Browse files Browse the repository at this point in the history
feat: emulate docker CLI with podman
  • Loading branch information
tthvo committed Aug 1, 2024
2 parents cd14584 + 0778a4e commit f8f64d0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
3 changes: 3 additions & 0 deletions docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exec podman "$@"
57 changes: 56 additions & 1 deletion setup.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"

0 comments on commit f8f64d0

Please sign in to comment.