From 023864fc8842ad0c65a0a5120c3b4468ace52fc4 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 25 Jul 2026 19:16:06 +0200 Subject: [PATCH] Speed up action setup with tarball install and parallel tool installs (#842) Co-authored-by: UltralyticsAssistant --- action.yml | 22 +++++++++++----------- actions/__init__.py | 2 +- actions/format_code.py | 9 +++++---- actions/update_markdown_code_blocks.py | 17 ++++++++--------- dependabot/action.yml | 4 ++-- github-report/action.yml | 4 ++-- tests/test_update_markdown_codeblocks.py | 6 +++--- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/action.yml b/action.yml index 009a760..4a149a7 100644 --- a/action.yml +++ b/action.yml @@ -113,17 +113,17 @@ runs: env: INPUTS_SPELLING: ${{ inputs.spelling }} GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_REF: ${{ github.ref }} + GITHUB_SHA: ${{ github.sha }} run: | echo "::group::Install Dependencies" trap 'echo "::endgroup::"' EXIT - # Install from current git branch if testing in ultralytics/actions repo, otherwise from GitHub main branch + # Source tarballs install ~2x faster than git+ URLs, which clone the repository first. Test the current commit + # in the ultralytics/actions repo, otherwise install main. if [ "$GITHUB_REPOSITORY" = "ultralytics/actions" ]; then - echo "Installing from git branch: $GITHUB_REF" - packages="git+https://github.com/ultralytics/actions@${GITHUB_REF#refs/heads/}" + echo "Installing from commit: $GITHUB_SHA" + packages="https://github.com/ultralytics/actions/archive/${GITHUB_SHA}.tar.gz" else - # packages="ultralytics-actions" - packages="git+https://github.com/ultralytics/actions@main" + packages="https://github.com/ultralytics/actions/archive/refs/heads/main.tar.gz" fi if [ "$INPUTS_SPELLING" = "true" ]; then @@ -216,18 +216,18 @@ runs: run: | echo "::group::Run Prettier" trap 'echo "::endgroup::"' EXIT - npm install -g prettier@3.6.2 prettier-plugin-sh + npm install -g prettier@3.6.2 + curl -fsSL "https://github.com/mvdan/sh/releases/download/v3.13.1/shfmt_v3.13.1_$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')" -o "$(npm prefix -g)/bin/shfmt" + chmod +x "$(npm prefix -g)/bin/shfmt" ultralytics-actions-update-markdown-code-blocks npx prettier --write --list-different --print-width 120 "**/*.{js,jsx,ts,tsx,css,less,scss,json,yml,yaml,html,vue,svelte}" '!**/*lock.{json,yaml,yml}' '!**/*.lock' '!**/model.json' '!**/*.min.js' '!**/*.min.css' - # Bash file format - if find . -name "*.sh" -type f | grep -q .; then - npx prettier --write --list-different --print-width 120 --plugin=$(npm root -g)/prettier-plugin-sh/lib/index.cjs "**/*.sh" - fi # Handle Markdown separately find . -name "*.md" -type f ! -path "*/docs/*" -exec npx prettier --write --list-different --print-width 120 {} + if [ -d "./docs" ]; then find ./docs -name "*.md" -type f ! -path "*/reference/*" -exec npx prettier --tab-width 4 --print-width 120 --write --list-different {} + fi + # Bash last: shfmt exits non-zero when a script fails to parse, which would otherwise skip the formatting above + find . -name "*.sh" -type f -not -path "*/node_modules/*" -not -path "*/.git/*" -exec shfmt -i 2 -sr -bn -ci -l -w {} + shell: bash continue-on-error: true diff --git a/actions/__init__.py b/actions/__init__.py index c3fee67..e301976 100644 --- a/actions/__init__.py +++ b/actions/__init__.py @@ -28,4 +28,4 @@ # ├── test_summarize_pr.py # └── ... -__version__ = "0.2.39" +__version__ = "0.2.40" diff --git a/actions/format_code.py b/actions/format_code.py index ca42770..810780e 100644 --- a/actions/format_code.py +++ b/actions/format_code.py @@ -19,17 +19,18 @@ RUFF_CHECK = [ RUFF_FORMAT = ["ruff", "format", "--line-length=120", "."] DOCSTRINGS = ["ultralytics-actions-format-python-docstrings", "."] PRETTIER = """ -npm install -g prettier@3.6.2 prettier-plugin-sh +npm install -g prettier@3.6.2 +curl -fsSL "https://github.com/mvdan/sh/releases/download/v3.13.1/shfmt_v3.13.1_$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')" -o "$(npm prefix -g)/bin/shfmt" +chmod +x "$(npm prefix -g)/bin/shfmt" ultralytics-actions-update-markdown-code-blocks npx prettier --write --list-different --print-width 120 "**/*.{js,jsx,ts,tsx,css,less,scss,json,yml,yaml,html,vue,svelte}" '!**/*lock.{json,yaml,yml}' '!**/*.lock' '!**/model.json' '!**/*.min.js' '!**/*.min.css' -if find . -name "*.sh" -type f | grep -q .; then - npx prettier --write --list-different --print-width 120 --plugin=$(npm root -g)/prettier-plugin-sh/lib/index.cjs "**/*.sh" -fi # Handle Markdown separately find . -name "*.md" -type f ! -path "*/docs/*" -exec npx prettier --write --list-different --print-width 120 {} + if [ -d "./docs" ]; then find ./docs -name "*.md" -type f ! -path "*/reference/*" -exec npx prettier --tab-width 4 --print-width 120 --write --list-different {} + fi +# Bash last: shfmt exits non-zero when a script fails to parse, which would otherwise skip the formatting above +find . -name "*.sh" -type f -not -path "*/node_modules/*" -not -path "*/.git/*" -exec shfmt -i 2 -sr -bn -ci -l -w {} + """ CODESPELL = [ "codespell", diff --git a/actions/update_markdown_code_blocks.py b/actions/update_markdown_code_blocks.py index 622abad..a1396c3 100644 --- a/actions/update_markdown_code_blocks.py +++ b/actions/update_markdown_code_blocks.py @@ -96,26 +96,25 @@ def format_code_with_ruff(temp_dir): print(f"ERROR running Python docstring formatter ❌ {e}") -def format_bash_with_prettier(temp_dir): - """Formats bash script files in the specified directory using prettier.""" +def format_bash_with_shfmt(temp_dir): + """Formats bash script files in the specified directory using shfmt.""" if not next(Path(temp_dir).rglob("*.sh"), None): return try: - # Run prettier with explicit config path + # Flags reproduce prettier-plugin-sh output: 2-space indent, spaced redirects, binary ops and cases indented result = subprocess.run( - "npx prettier --write --print-width=120 --plugin=$(npm root -g)/prettier-plugin-sh/lib/index.cjs ./**/*.sh", - shell=True, # must use shell=True to expand internal $(cmd) + ["shfmt", "-i", "2", "-sr", "-bn", "-ci", "-w", str(temp_dir)], capture_output=True, text=True, check=False, ) if result.returncode != 0: - print(f"ERROR running prettier-plugin-sh ❌ {result.stderr}") + print(f"ERROR running shfmt ❌ {result.stderr}") else: print("Completed bash formatting ✅") except Exception as e: - print(f"ERROR running prettier-plugin-sh ❌ {e}") + print(f"ERROR running shfmt ❌ {e}") def process_markdown_string( @@ -141,7 +140,7 @@ def process_markdown_string( if python_files_exist: format_code_with_ruff(temp_dir) if bash_files_exist: - format_bash_with_prettier(temp_dir) + format_bash_with_shfmt(temp_dir) update_markdown_file(temp_md, markdown_snapshot, temp_files) formatted_markdown = temp_md.read_text(encoding="utf-8") @@ -244,7 +243,7 @@ def main(root_dir=None, process_python=True, process_bash=True, verbose=False): if process_python: format_code_with_ruff(temp_dir) # Format Python files if process_bash: - format_bash_with_prettier(temp_dir) # Format Bash files + format_bash_with_shfmt(temp_dir) # Format Bash files # Update Markdown files with formatted code blocks for markdown_file, markdown_content, temp_files in all_temp_files: diff --git a/dependabot/action.yml b/dependabot/action.yml index 65c1eea..2c7eeed 100644 --- a/dependabot/action.yml +++ b/dependabot/action.yml @@ -39,9 +39,9 @@ runs: echo "::group::Install ultralytics-actions" if [ "$GITHUB_REPOSITORY" = "ultralytics/actions" ]; then echo "Installing from commit: $GITHUB_SHA" - packages="git+https://github.com/ultralytics/actions@${GITHUB_SHA}" + packages="https://github.com/ultralytics/actions/archive/${GITHUB_SHA}.tar.gz" else - packages="git+https://github.com/ultralytics/actions@main" + packages="https://github.com/ultralytics/actions/archive/refs/heads/main.tar.gz" fi if [ "$(uname)" = "Darwin" ]; then uv pip install --system --break-system-packages $packages diff --git a/github-report/action.yml b/github-report/action.yml index 23fcd65..fed3a59 100644 --- a/github-report/action.yml +++ b/github-report/action.yml @@ -55,9 +55,9 @@ runs: echo "::group::Install ultralytics-actions" if [ "$GITHUB_REPOSITORY" = "ultralytics/actions" ]; then echo "Installing from commit: $GITHUB_SHA" - packages="git+https://github.com/ultralytics/actions@${GITHUB_SHA}" + packages="https://github.com/ultralytics/actions/archive/${GITHUB_SHA}.tar.gz" else - packages="git+https://github.com/ultralytics/actions@main" + packages="https://github.com/ultralytics/actions/archive/refs/heads/main.tar.gz" fi if [ "$(uname)" = "Darwin" ]; then uv pip install --system --break-system-packages $packages diff --git a/tests/test_update_markdown_codeblocks.py b/tests/test_update_markdown_codeblocks.py index 6319462..1358cd0 100644 --- a/tests/test_update_markdown_codeblocks.py +++ b/tests/test_update_markdown_codeblocks.py @@ -6,7 +6,7 @@ from unittest.mock import mock_open, patch from actions.update_markdown_code_blocks import ( add_indentation, extract_code_blocks, - format_bash_with_prettier, + format_bash_with_shfmt, generate_temp_filename, main, process_markdown_file, @@ -110,11 +110,11 @@ def test(): def test_format_bash_skips_when_no_shell_files(tmp_path): - """Test bash formatter skips Prettier when no shell snippets were extracted.""" + """Test bash formatter skips shfmt when no shell snippets were extracted.""" (tmp_path / "snippet.py").write_text("print('ok')", encoding="utf-8") with patch("subprocess.run") as mock_run: - format_bash_with_prettier(tmp_path) + format_bash_with_shfmt(tmp_path) mock_run.assert_not_called()