我想使用github操作更新github repo中README文件的一个部分,README文件的这个部分用注解清楚地标记出来:
<!-- start-quote -->
<!-- end-quote -->
我已经编写了这个gihub工作流来更新这个部分。
name: readme-quote
on:
push:
schedule:
- cron: '0 */12 * * *'
jobs:
add_quote:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Get an inspirational quote
run: |
QUOTE="This quote is fetched from a remote api using CURL"
echo "$QUOTE"
- name: Update quote in readme
run: |-
# Set a default quote if the
echo "${QUOTE:=>Silence is Golden}"
# Disable interpreting exclamation mark! as a command
histchars=
# Set delimiters to variables
start_delimiter="<!-- start-quote -->"
end_delimiter="<!-- end-quote -->"
# Remove previous quote with its delimter
sed -i "/$start_delimiter/,/$end_delimiter/d" README.md
# Add the new quote to the README file
printf "$start_delimiter\n$QUOTE\n$end_delimiter\n" >> README.md
- name: Commit and push changes if README changed
run: |-
git diff
git config --local user.email "github-actions@example.com"
git config --local user.name "GitHub Actions"
git add README.md
git diff --quiet || (git add README.md && git commit -m "Added new quote to README")
git push origin main
所有步骤在本地机器上都成功了,工作流在github操作中也成功了✔,但是README文件没有更新!令人惊讶的是,部分日志显示文件已经更新。
Run git diff
git diff
git config --local user.email "github-actions@example.com"
git config --local user.name "GitHub Actions"
git add README.md
git diff --quiet || (git add README.md && git commit -m "Added new quote to README")
git push origin main
shell: /usr/bin/bash -e {0}
diff --git a/README.md b/README.md
index 94b5482..ce225aa 100644
--- a/README.md
+++ b/README.md
@@ -27,4 +27,5 @@
<!-- start-quote -->
-<!-- end-quote -->
\ No newline at end of file
+> Silence is Golden
+<!-- end-quote -->
Everything up-to-date
1条答案
按热度按时间ybzsozfc1#
首先运行
git add README.md
,然后git diff --quiet
以代码0退出,这样下一个|| (git add README.md && git commit
就不会运行,没有提交就没有什么可推送的。删除第一个
git add README.md
。