如何在GitHub操作中引用GitHub Pages的URL?

huangapple go评论60阅读模式
英文:

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 &quot;repos/$GITHUB_REPOSITORY/pages&quot; --jq &#39;.html_url&#39;)
    sed -i &#39;19i\
          &lt;meta http-equiv=&quot;refresh&quot; content=&quot;0;URL=&#39;&quot;$url&quot;manual/&#39;&quot;&gt;
    &#39; _site/index.html    

This takes into consideration everything like protocol and custom domain names.

huangapple
  • 本文由 发表于 2023年5月29日 07:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定