GitHub Actions中Makefile执行touch命令失败求助
问题描述
在GitHub Actions中执行Makefile里的touch命令失败,提示“无法找到指定文件或目录”,但相同操作在Jenkins和本地环境均可正常运行。尝试给Make命令添加$(shell)后问题依旧。
相关代码与信息
Makefile内容
test: $(Shell prometheus/generated_config.sh --first-cluster-url "test.yl4.us-east-1.eks.amazonaws.com" --first-cluster-name test --first-cluster-token "test" --test-context)
Make版本信息
Run make --version make --version shell: /usr/bin/bash -e {0} GNU Make 4.3 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
GitHub Actions Workflow片段
- name: prometheus-deploy-${{ inputs.environment }} env: FIRST_KUBE_TOKEN: ${{ secrets.FIRST_KUBE_TOKEN }} DEPLOYER_KEY: ${{ secrets.IHR_DEPLOYER_KEY }} FIRST_CLUSTER_URL: ${{ secrets.FIRST_CLUSTER_URL }} CLUSTER_NAME: ${{ secrets.CLUSTER_NAME }} SSH_HOST: ${{ secrets.SSH_HOST }} run: | touch DEPLOYER_KEY.pem echo "${DEPLOYER_KEY}" >> DEPLOYER_KEY.pem chmod 0600 ./DEPLOYER_KEY.pem echo $SHELL ls prometheus/ sudo chsh -s /bin/bash if [[ "true" == "true" ]]; then export CONFIG_ONLY_FLAG='--config-only' make test ./prometheus/generate_config.sh --first-cluster-url "${FIRST_CLUSTER_URL}" --first-cluster-name "${CLUSTER_NAME}" --first-cluster-token "${FIRST_KUBE_TOKEN}"
错误输出
shell: /usr/bin/bash -e {0} env: FIRST_KUBE_TOKEN: *** IHR_DEPLOYER_KEY: *** FIRST_CLUSTER_URL: *** CLUSTER_NAME: *** SSH_HOST: *** /bin/bash config generate_config.sh rules tests web-config.yml prometheus/generate_config.sh --first-cluster-url "test.yl4.us-east-1.eks.amazonaws.com" --first-cluster-name test --first-cluster-token "test" --test-context touch: cannot touch '/home/runner/_work/prometheus/project-prometheus/prometheus/generated_config/first_token': No such file or directory touch: cannot touch '/home/runner/_work/prometheus/project-prometheus/prometheus/generated_config/second_token': No such file or directory
解决方法
核心问题是prometheus/generated_config.sh脚本尝试创建文件时,目标文件的父目录prometheus/generated_config在GitHub Actions环境中不存在——本地和Jenkins环境可能已提前创建该目录,或脚本在这些环境中隐含处理了目录创建逻辑。
以下是几种可行的修复方案:
修改shell脚本,确保父目录存在
打开prometheus/generated_config.sh,找到执行touch命令的位置,在touch前添加mkdir -p命令创建父目录。比如:# 针对first_token的处理 TOKEN_PATH="./prometheus/generated_config/first_token" mkdir -p "$(dirname "$TOKEN_PATH")" touch "$TOKEN_PATH" # 针对second_token的处理同理 SECOND_TOKEN_PATH="./prometheus/generated_config/second_token" mkdir -p "$(dirname "$SECOND_TOKEN_PATH")" touch "$SECOND_TOKEN_PATH"用
dirname动态获取父目录,避免硬编码路径,提升脚本兼容性。在Makefile中提前创建目录
修改Makefile的test目标,在执行shell脚本前先创建所需目录:test: mkdir -p prometheus/generated_config $(Shell prometheus/generated_config.sh --first-cluster-url "test.yl4.us-east-1.eks.amazonaws.com" --first-cluster-name test --first-cluster-token "test" --test-context)在GitHub Actions Workflow中提前创建目录
在执行make test的步骤前,添加目录创建命令:run: | # ... 其他已有的命令 ... mkdir -p prometheus/generated_config make test # ... 其他已有的命令 ...检查脚本中的路径是否为相对路径
确认generated_config.sh中使用的是相对路径而非硬编码的绝对路径。GitHub Actions的工作目录结构与本地/Jenkins可能不同,硬编码绝对路径会导致路径不匹配。
内容的提问来源于stack exchange,提问作者user_01_02
相关产品推荐
相关产品推荐

