feat: restore .npmrc state at the end of setup-node-environment action

This change modifies the `setup-node-environment` composite action to ensure that any changes made to the `.npmrc` file during the action's execution are reverted at the end.

- **Backup:** At the beginning of the `prepare` step, the existing `.npmrc` is backed up to `.npmrc.action-backup`. If `.npmrc` does not exist, a marker file `.npmrc.was-missing` is created.
- **Restore:** A new step `🧹 恢复 .npmrc` is added at the end of the action. It runs with `if: always()` to guarantee execution even if intermediate steps fail. It either restores `.npmrc` from the backup or deletes it if it was originally missing.

This ensures that the action leaves the environment (specifically the `.npmrc` file) in the same state as it found it, preventing side effects on subsequent workflow steps or jobs.
This commit is contained in:
google-labs-jules[bot]
2025-12-08 14:31:50 +00:00
committed by 严浩
parent 94776edf02
commit faab20ac2f

View File

@@ -43,6 +43,15 @@ runs:
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
# 🤖---- 备份 .npmrc 状态 ----🤖 #
if [ -f .npmrc ]; then
echo "备份现有的 .npmrc 到 .npmrc.action-backup"
cp .npmrc .npmrc.action-backup
else
echo "标记 .npmrc 原本不存在"
touch .npmrc.was-missing
fi
# 🤖---- 准备环境变量 ----🤖 #
# --- 1. 包管理器 ---
@@ -247,6 +256,21 @@ runs:
with:
path: ${{ steps.pnpm-store-dir.outputs.pnpmStoreDir }}
key: ${{ steps.cache-pnpm-restore.outputs.cache-primary-key }}
- name: 🧹 恢复 .npmrc
if: always()
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "🤖---- 恢复 .npmrc ----🤖"
if [ -f .npmrc.action-backup ]; then
echo "恢复备份的 .npmrc"
mv .npmrc.action-backup .npmrc
elif [ -f .npmrc.was-missing ]; then
echo "删除生成的 .npmrc (原本不存在)"
rm -f .npmrc
rm .npmrc.was-missing
fi
# rm -r node_modules
# pnpm fetch
# pnpm install --offline