1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

fix!: fallback to sh if bash does not exist (#177)

Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/177
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
This commit is contained in:
earl-warren 2025-07-13 15:59:34 +00:00
commit d2f668c880
13 changed files with 101 additions and 70 deletions

View file

@ -5,7 +5,6 @@ package cacheproxy
import ( import (
"crypto/hmac" "crypto/hmac"
"crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -170,12 +169,10 @@ func (h *Handler) ExternalURL() string {
// The function returns the 32-bit random key which the run will use to identify itself. // The function returns the 32-bit random key which the run will use to identify itself.
func (h *Handler) AddRun(data RunData) (string, error) { func (h *Handler) AddRun(data RunData) (string, error) {
for retries := 0; retries < 3; retries++ { for retries := 0; retries < 3; retries++ {
keyBytes := make([]byte, 4) key, err := common.RandName(4)
_, err := rand.Read(keyBytes)
if err != nil { if err != nil {
return "", errors.New("Could not generate the run id") return "", errors.New("Could not generate the run id")
} }
key := hex.EncodeToString(keyBytes)
_, loaded := h.runs.LoadOrStore(key, data) _, loaded := h.runs.LoadOrStore(key, data)
if !loaded { if !loaded {

16
act/common/randname.go Normal file
View file

@ -0,0 +1,16 @@
// Copyright 2025 The Forgejo Authors
// SPDX-License-Identifier: MIT
package common
import (
"crypto/rand"
"encoding/hex"
)
func RandName(size int) (string, error) {
randBytes := make([]byte, size)
if _, err := rand.Read(randBytes); err != nil {
return "", err
}
return hex.EncodeToString(randBytes), nil
}

View file

@ -3,8 +3,6 @@ package runner
import ( import (
"archive/tar" "archive/tar"
"context" "context"
"crypto/rand"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -19,6 +17,8 @@ import (
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/nektos/act/pkg/common"
) )
type ActionCache interface { type ActionCache interface {
@ -39,11 +39,10 @@ func (c GoGitActionCache) Fetch(ctx context.Context, cacheDir, url, ref, token s
if err != nil { if err != nil {
return "", err return "", err
} }
tmpBranch := make([]byte, 12) branchName, err := common.RandName(12)
if _, err := rand.Read(tmpBranch); err != nil { if err != nil {
return "", err return "", err
} }
branchName := hex.EncodeToString(tmpBranch)
var auth transport.AuthMethod var auth transport.AuthMethod
if token != "" { if token != "" {

View file

@ -5,20 +5,20 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"crypto/rand"
"crypto/sha256" "crypto/sha256"
_ "embed" _ "embed"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"text/template" "text/template"
"time"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
@ -283,9 +283,10 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
return true return true
}) })
cacheDir := rc.ActionCacheDir() cacheDir := rc.ActionCacheDir()
randBytes := make([]byte, 8) randName, err := common.RandName(8)
_, _ = rand.Read(randBytes) if err != nil {
randName := hex.EncodeToString(randBytes) return err
}
miscpath := filepath.Join(cacheDir, randName) miscpath := filepath.Join(cacheDir, randName)
actPath := filepath.Join(miscpath, "act") actPath := filepath.Join(miscpath, "act")
if err := os.MkdirAll(actPath, 0o777); err != nil { if err := os.MkdirAll(actPath, 0o777); err != nil {
@ -586,6 +587,42 @@ func (rc *RunContext) startJobContainer() common.Executor {
} }
} }
func (rc *RunContext) sh(ctx context.Context, script string) (stdout, stderr string, err error) {
timeed, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
hout := &bytes.Buffer{}
herr := &bytes.Buffer{}
env := map[string]string{}
for k, v := range rc.Env {
env[k] = v
}
base, err := common.RandName(8)
if err != nil {
return "", "", err
}
name := base + ".sh"
oldStdout, oldStderr := rc.JobContainer.ReplaceLogWriter(hout, herr)
err = rc.JobContainer.Copy(rc.JobContainer.GetActPath(), &container.FileEntry{
Name: name,
Mode: 0o644,
Body: script,
}).
Then(rc.execJobContainer([]string{"sh", path.Join(rc.JobContainer.GetActPath(), name)},
env, "", "")).
Finally(func(context.Context) error {
rc.JobContainer.ReplaceLogWriter(oldStdout, oldStderr)
return nil
})(timeed)
if err != nil {
return "", "", err
}
stdout = hout.String()
stderr = herr.String()
return stdout, stderr, nil
}
func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user, workdir string) common.Executor { func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user, workdir string) common.Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
return rc.JobContainer.Exec(cmd, env, user, workdir)(ctx) return rc.JobContainer.Exec(cmd, env, user, workdir)(ctx)

View file

@ -231,12 +231,12 @@ func TestRunEvent(t *testing.T) {
tables := []TestJobFileInfo{ tables := []TestJobFileInfo{
// Shells // Shells
{workdir, "shells/defaults", "push", "", platforms, secrets}, {workdir, "shells/defaults", "push", "", platforms, secrets},
// TODO: figure out why it fails {workdir, "shells/custom", "push", "", platforms, secrets},
// {workdir, "shells/custom", "push", "", map[string]string{"ubuntu-latest": "catthehacker/ubuntu:pwsh-latest"}, }, // custom image with pwsh
{workdir, "shells/pwsh", "push", "", map[string]string{"ubuntu-latest": "catthehacker/ubuntu:pwsh-latest"}, secrets}, // custom image with pwsh
{workdir, "shells/bash", "push", "", platforms, secrets}, {workdir, "shells/bash", "push", "", platforms, secrets},
{workdir, "shells/python", "push", "", map[string]string{"ubuntu-latest": "node:16-buster"}, secrets}, // slim doesn't have python {workdir, "shells/node", "push", "", platforms, secrets},
{workdir, "shells/python", "push", "", platforms, secrets},
{workdir, "shells/sh", "push", "", platforms, secrets}, {workdir, "shells/sh", "push", "", platforms, secrets},
{workdir, "shells/pwsh", "push", "", platforms, secrets},
// Local action // Local action
{workdir, "local-action-docker-url", "push", "", platforms, secrets}, {workdir, "local-action-docker-url", "push", "", platforms, secrets},

View file

@ -194,9 +194,20 @@ func (sr *stepRun) setupShell(ctx context.Context) {
if err != nil { if err != nil {
step.Shell = shellWithFallback[1] step.Shell = shellWithFallback[1]
} }
} else if containerImage := rc.containerImage(ctx); containerImage != "" { } else {
// Currently only linux containers are supported, use sh by default like actions/runner shell_fallback := `
step.Shell = "sh" if command -v bash >/dev/null; then
echo -n bash
else
echo -n sh
fi
`
stdout, _, err := rc.sh(ctx, shell_fallback)
if err != nil {
common.Logger(ctx).Error("fail to run %q: %v", shell_fallback, err)
return
}
step.Shell = stdout
} }
} }
} }

View file

@ -14,7 +14,8 @@ jobs:
fi fi
check-container: check-container:
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: node:16-buster-slim container:
image: code.forgejo.org/oci/node:22-bookworm
steps: steps:
- shell: ${{ env.MY_SHELL }} - shell: ${{ env.MY_SHELL }}
run: | run: |

View file

@ -3,12 +3,6 @@ jobs:
check: check:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# prints version and exits, it's not valid (for github) if {0} is not included - shell: cat {0}
- shell: pwsh -v '. {0}' run: |
run: '' exit 1
check-container:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:pwsh-latest
steps:
- shell: pwsh -v '. {0}'
run: ''

View file

@ -1,6 +1,6 @@
on: push on: push
jobs: jobs:
check: # GHA uses `bash` as default for runners check: # use `bash` as default for runners
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- run: | - run: |
@ -9,12 +9,13 @@ jobs:
else else
exit 1 exit 1
fi fi
check-container: # GHA uses `sh` as default for containers fallback: # uses `sh` as fallback default if `bash` is not available
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: alpine:latest container:
image: code.forgejo.org/oci/alpine:latest
steps: steps:
- run: | - run: |
if [ -z ${BASH+x} ]; then if [ -z "${BASH}" ]; then
echo "I'm sh!" echo "I'm sh!"
else else
exit 1 exit 1

View file

@ -8,13 +8,6 @@ jobs:
- shell: ${{ env.MY_SHELL }} - shell: ${{ env.MY_SHELL }}
run: | run: |
console.log(process.version) console.log(process.version)
check-container:
runs-on: ubuntu-latest
container: node:16-buster-slim
steps:
- shell: ${{ env.MY_SHELL }}
run: |
console.log(process.version)
check-job-default: check-job-default:
runs-on: ubuntu-latest runs-on: ubuntu-latest
defaults: defaults:

View file

@ -4,19 +4,16 @@ env:
jobs: jobs:
check: check:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: container:
- shell: ${{ env.MY_SHELL }} image: code.forgejo.org/oci/ci:1
run: |
$PSVersionTable
check-container:
runs-on: ubuntu-latest
container: catthehacker/ubuntu:pwsh-latest
steps: steps:
- shell: ${{ env.MY_SHELL }} - shell: ${{ env.MY_SHELL }}
run: | run: |
$PSVersionTable $PSVersionTable
check-job-default: check-job-default:
runs-on: ubuntu-latest runs-on: ubuntu-latest
container:
image: code.forgejo.org/oci/ci:1
defaults: defaults:
run: run:
shell: ${{ env.MY_SHELL }} shell: ${{ env.MY_SHELL }}

View file

@ -4,14 +4,8 @@ env:
jobs: jobs:
check: check:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: container:
- shell: ${{ env.MY_SHELL }} image: code.forgejo.org/oci/python:slim
run: |
import platform
print(platform.python_version())
check-container:
runs-on: ubuntu-latest
container: node:16-buster
steps: steps:
- shell: ${{ env.MY_SHELL }} - shell: ${{ env.MY_SHELL }}
run: | run: |
@ -19,6 +13,8 @@ jobs:
print(platform.python_version()) print(platform.python_version())
check-job-default: check-job-default:
runs-on: ubuntu-latest runs-on: ubuntu-latest
container:
image: code.forgejo.org/oci/python:slim
defaults: defaults:
run: run:
shell: ${{ env.MY_SHELL }} shell: ${{ env.MY_SHELL }}

View file

@ -7,18 +7,7 @@ jobs:
steps: steps:
- shell: ${{ env.MY_SHELL }} - shell: ${{ env.MY_SHELL }}
run: | run: |
if [ -z ${BASH+x} ]; then if [ -z "${BASH}" ]; then
echo "I'm sh!"
else
exit 1
fi
check-container:
runs-on: ubuntu-latest
container: alpine:latest
steps:
- shell: ${{ env.MY_SHELL }}
run: |
if [ -z ${BASH+x} ]; then
echo "I'm sh!" echo "I'm sh!"
else else
exit 1 exit 1
@ -30,7 +19,7 @@ jobs:
shell: ${{ env.MY_SHELL }} shell: ${{ env.MY_SHELL }}
steps: steps:
- run: | - run: |
if [ -z ${BASH+x} ]; then if [ -z "${BASH}" ]; then
echo "I'm sh!" echo "I'm sh!"
else else
exit 1 exit 1