Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option to define user to run commands in the target environment #66

Merged
merged 2 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/test-user_option.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test user option
on:
push:
branches:
- 'main'
- 'releases/**'
pull_request:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- run: |
id | grep -c runner
- uses: actions/checkout@v3
- uses: ./ # pguyot/arm-runner-action@HEAD
with:
commands: |
id | grep -c root
- uses: ./ # pguyot/arm-runner-action@HEAD
with:
user: nobody
commands: |
id | grep -c nobody
- uses: ./ # pguyot/arm-runner-action@HEAD
with:
user: nobody:lp
commands: |
id | grep -c lp
- uses: ./ # pguyot/arm-runner-action@HEAD
with:
user: nobody
use_systemd_nspawn: true
commands: |
id | grep -c nobody
- id: unknown_user
continue-on-error: true
uses: ./ # pguyot/arm-runner-action@HEAD
with:
user: unknown_user
commands: |
id
- name: Report unexpected success
if: ${{ steps.unknown_user.outcome == 'success' }}
run: |
echo "Action is expected to fail"
exit 1
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ with some shells that come packaged under a different package name.
For example, to use `ksh93` as shell, set `shell` to `ksh93` and
`shell_package` to `ksh`.

#### `user`

User to run commands within the image. It must exists.
By default, commands are run with user 0 (root).
Unless you are using `systemd-nspawn`, you can also specify the group with
the `user:group` syntax.

#### `exit_on_fail`

Exit immediately if a command exits with a non-zero status. Default is to exit.
Expand Down
19 changes: 16 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ inputs:
description: 'The shell package to install, if different from shell'
required: false
default: ''
user:
description: 'User to run commands on the image. Must exist in the image'
required: false
default: ''
exit_on_fail:
description: 'Exit immediately if a command exits with a non-zero status'
required: false
Expand Down Expand Up @@ -178,6 +182,15 @@ runs:
else
shell_opts=""
fi
if [ "${{ inputs.user }}" != "" ]; then
if [ "${{ inputs.use_systemd_nspawn }}x" != "x" -a "${{ inputs.use_systemd_nspawn }}x" != "nox" ]; then
user_opt="-u ${{ inputs.user }}"
else
user_opt="--userspec=${{ inputs.user }}"
fi
else
user_opt=""
fi
shell_package=${{ inputs.shell_package }}
[ -x ${{ steps.mount_image.outputs.mount }}/${shell} ] || \
shell_path=$(sudo chroot ${{ steps.mount_image.outputs.mount }} /bin/sh -c "command -v ${shell}") || \
Expand Down Expand Up @@ -233,9 +246,9 @@ runs:
ARM_RUNNER_INPUT_COMMANDS_EOF
if [ "${{ inputs.use_systemd_nspawn }}x" != "x" -a "${{ inputs.use_systemd_nspawn }}x" != "nox" ]; then
sudo apt-get install -y systemd-container
sudo -E systemd-nspawn -q -a --bind=${script_dir}:${chroot_script_dir} -D ${{ steps.mount_image.outputs.mount }} ${{ inputs.systemd_nspawn_options }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
sudo -E systemd-nspawn ${user_opt} -q -a --bind=${script_dir}:${chroot_script_dir} -D ${{ steps.mount_image.outputs.mount }} ${{ inputs.systemd_nspawn_options }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
else
sudo -E chroot ${{ steps.mount_image.outputs.mount }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
sudo -E chroot ${user_opt} ${{ steps.mount_image.outputs.mount }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
fi
rc=$?
[ -f ${script_dir}/github_env.sh ] && \
Expand All @@ -244,7 +257,7 @@ runs:
shell: bash
- name: Copy artifacts within image
if: ${{ always() && !cancelled() && (inputs.copy_artifacts_on_fail == 'yes' || steps.runcmd.conclusion == 'success') }}
run: |
run: |
case "${{ inputs.debug }}" in
yes|true)
set -x
Expand Down