How to reference the GitHub Pages URL in a GitHub action?

Casey Hofland

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.

Benjamin W.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related