英文:
How to reference the GitHub Pages URL in a GitHub action?
问题
我正在编写一个 GitHub Action,用于在侧边生成一些内容。它包含以下步骤:
- name: 生成首页的客户端重定向
run: sed -i '19i\ <meta http-equiv="refresh" content="0;URL='https://casey-hofland.github.io/UnityClock/manual/'">' _site/index.html
这个 GitHub Action 需要是通用的,所以我需要修改这一行:
URL='https://casey-hofland.github.io/UnityClock/manual/'
变成类似这样:
URL='{{ $GITHUB_PAGES_URL }}manual/'
我查看了 默认环境变量,但没有找到类似的内容,有没有办法让我获取这个链接?
注:仍然需要在使用自定义 GitHub Pages 链接时正常工作,因此类似 {{ $GITHUB_ACCOUNT_NAME }}/{{ $GITHUB_REPO_NAME }}
的巫术不可行。
额外:
我有点像 GitHub Actions / bash 的新手,所以对如何在我的 sed -i
命令中实现变量的任何提示都将不胜感激。
英文:
I'm writing a github action that generates some content on the side. It contains the following step:
- name: Generate a client-side redirect on the index page
run: sed -i '19i\ <meta http-equiv="refresh" content="0;URL='https://casey-hofland.github.io/UnityClock/manual/'">' _site/index.html
This github action needs to be generic, so I need to change this line:
URL='https://casey-hofland.github.io/UnityClock/manual/'
To something like:
URL='{{ $GITHUB_PAGES_URL }}manual/'
I've looked through the default environment variables and haven't found anything like it, is there any way for me to retrieve the link?
Note: it still has to work when people are using a custom github pages link, so doing some wizardry like {{ $GITHUB_ACCOUNT_NAME }}/{{ $GITHUB_REPO_NAME }}
is not an option.
Bonus:
I'm kind of a github actions / bash noob so any tips on how to implement a variable inside my sed -i
command would be appreciated as well.
答案1
得分: 1
GitHub Pages 网站可以通过 API 访问,例如 REST(请参阅文档)。您可以在 GitHub CLI 的 run
步骤中获取它:
- name: 生成重定向
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
url=$(gh api "repos/$GITHUB_REPOSITORY/pages" --jq '.html_url')
sed -i '19i\
<meta http-equiv="refresh" content="0;URL='$url'manual/">' _site/index.html
'
这会考虑到协议和自定义域名等所有内容。
英文:
A GitHub Pages site is available via the APIs, for example REST (see the docs). You can get it in a run
step using the GitHub CLI:
- name: Generate redirect
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
url=$(gh api "repos/$GITHUB_REPOSITORY/pages" --jq '.html_url')
sed -i '19i\
<meta http-equiv="refresh" content="0;URL='"$url"manual/'">
' _site/index.html
This takes into consideration everything like protocol and custom domain names.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论