GitHub Actions执行本地脚本提示文件不存在问题求助
问题分析与解决方案
你遇到的问题是:GitHub Action中ls -la能列出deploy_container_app.sh脚本,但执行时提示文件未找到。这类问题通常由以下几个原因导致,对应解决方案如下:
1. 脚本缺少可执行权限
GitHub仓库中的文件默认可能没有可执行权限,即使本地配置了权限,推送到仓库后也可能丢失。解决方法是在执行前给脚本添加可执行权限:
修改Action步骤:
- name: Deploy Container App with Custom Script working-directory: scripts run: | ls -la # list files to debug chmod +x deploy_container_app.sh ./deploy_container_app.sh # execute the script shell: bash env: REPOSITORY_NAME: ${{ secrets.REPOSITORY_NAME }} REGISTRY_LOGIN_SERVER: ${{ secrets.REGISTRY_LOGIN_SERVER }} ROBOFLOW_API_KEY: ${{ secrets.ROBOFLOW_API_KEY }} CALLER_API_KEY: ${{ secrets.CALLER_API_KEY }} CALLER_SECRET_KEY: ${{ secrets.CALLER_SECRET_KEY }}
2. 脚本换行符格式错误(Windows CRLF 转 Linux LF)
如果脚本在Windows环境下编辑,换行符会是CRLF格式,Linux系统无法正确识别,会抛出类似“文件未找到”的错误。可以通过dos2unix转换格式:
- name: Install dos2unix run: sudo apt-get update && sudo apt-get install -y dos2unix - name: Deploy Container App with Custom Script working-directory: scripts run: | ls -la dos2unix deploy_container_app.sh chmod +x deploy_container_app.sh ./deploy_container_app.sh shell: bash env: # 保持原有环境变量配置
3. 直接用bash解释器执行脚本(绕过shebang和权限问题)
如果不想处理权限或换行符,可直接调用bash解释器运行脚本,无需依赖脚本自身的可执行权限和shebang配置:
- name: Deploy Container App with Custom Script working-directory: scripts run: | ls -la bash deploy_container_app.sh shell: bash env: # 保持原有环境变量配置
4. 检查脚本的shebang行
确保脚本第一行是正确的shebang声明,比如#!/bin/bash(对应Action中指定的shell: bash)。如果shebang指定的解释器不存在(比如写了#!/usr/bin/zsh但环境中没有zsh),也会触发“文件未找到”的错误。
内容的提问来源于stack exchange,提问作者Tomer Shukhman
相关产品推荐
相关产品推荐

