为什么这个 GitHub Actions 脚本不能构建我的 Hugo 页面?

Why does this GitHub Actions script not build my Hugo page?

我有一个基于 GitHub project with a Hugo 的网站。每当有人向 prod 分支推送内容时,我都想构建 Hugo 页面(将 Markdown 文件转换为 HTML)并将其上传到我的托管服务提供商。我在构建页面时遇到问题。

我在 GitHub 操作中有 this 脚本:

name: Publish prod branch
on:
        push:
                branches:
                        - prod

jobs:
        build:
                name: Greeting
                runs-on: ubuntu-latest
                steps:
                        - name: Hello world
                          uses: actions/hello-world-javascript-action@v1
                          with:
                                  who-to-greet: Dmitrii
                          id: hello
                        - name: Echo the greeting's time
                          run: echo 'The time was ${{ steps.hello.outputs.time }}.'
                        - name: Build Hugo
                          uses: srt32/hugo-action@master

fails because it does not find the configuration file config.toml, even though it is there:

/usr/bin/docker run --name e87b520e21a5125f094485b4e030650bd57153_f8bc76 --label e87b52 --workdir /github/workspace --rm -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/hugo-wp-site/hugo-wp-site":"/github/workspace" e87b52:0e21a5125f094485b4e030650bd57153
Error: Unable to locate config file or config directory. Perhaps you need to create a new site.
#################################################
       Run `hugo help new` for details.
Starting the Hugo Action

Total in 0 ms

我该如何解决,我。 e.让雨果看到我的 config.toml 文件?

更新1:我试图通过如下修改脚本来找出正在使用的Hugo版本:

name: Publish prod branch
on:
        push:
                branches:
                        - prod

jobs:
        build:
                name: Build and publish web site to hosting provider
                runs-on: ubuntu-latest
                steps:
                        - name: Hello world
                          uses: actions/hello-world-javascript-action@v1
                          with:
                                  who-to-greet: Dmitrii
                          id: hello
                        - name: Echo the greeting's time
                          run: echo 'The time was ${{ steps.hello.outputs.time }}.'
                        - name: Output the version of Hugo
                          run: hugo version
                        - name: Build Hugo
                          uses: srt32/hugo-action@master

但是当我 运行 它时,我得到以下错误:

hugo version
  shell: /bin/bash -e {0}
/home/runner/work/_temp/9e57960c-2f2c-4f2a-870c-c1cbc41d820f.sh: line 1: hugo: command not found
##[error]Process completed with exit code 127.

更新2:在输出中找到Hugo的版本:

 (7/7) Installing hugo (0.61.0-r0)

更新3:最早可能有问题的Hugo版本6794 fixed is v0.64.0 because that issue was merged on January 31st and v0.64.0是那天之后出现的第一个版本

更新4:看来为了修复这个错误,我需要确保Hugo动作使用更新版本的Hugo。为此,我更改了 Dockerfile so that version 0.65.3-r0 is installed (according to this answer):

RUN apk add --no-cache hugo=0.65.3-r0 bash

但是当我 运行 脚本时,Alpine Linux 无法安装 Hugo:

fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  hugo-0.61.0-r0:
    breaks: world[hugo=0.65.3-r0]
The command '/bin/sh -c apk add --no-cache hugo=0.65.3-r0 bash' returned a non-zero code: 1
##[warning]Docker build failed with exit code 1, back off 9.558 seconds before retry.
/usr/bin/docker build -t e87b52:dfe904e1240c4dbea120e452e5568b51 "/home/runner/work/_actions/dpisarenko/hugo-action/master"
Sending build context to Docker daemon  7.168kB

非常感谢任何有关如何解决此问题的帮助。

更新 5: 将 Hugo 安装部分更改为

RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk update 
RUN apk add --no-cache hugo=0.65.3-r0 bash

该操作安装更新版本的 Hugo:

 Step 10/13 : RUN apk add --no-cache hugo=0.65.3-r0 bash
 ---> Running in 633b06ba9a65
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
(1/7) Installing ncurses-terminfo-base (6.1_p20200118-r2)
(2/7) Installing ncurses-libs (6.1_p20200118-r2)
(3/7) Installing readline (8.0.1-r0)
(4/7) Installing bash (5.0.11-r1)
Executing bash-5.0.11-r1.post-install
(5/7) Installing libgcc (9.2.0-r3)
(6/7) Installing libstdc++ (9.2.0-r3)
(7/7) Installing hugo (0.65.3-r0)

但我仍然得到同样的错误:

Run dpisarenko/hugo-action@master
/usr/bin/docker run --name e87b52fba2a6bbd65d4e86b03264ae4ae92e94_cbeaf6 --label e87b52 --workdir /github/workspace --rm -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/hugo-wp-site/hugo-wp-site":"/github/workspace" e87b52:fba2a6bbd65d4e86b03264ae4ae92e94
#################################################
Starting the Hugo Action
Error: Unable to locate config file or config directory. Perhaps you need to create a new site.
       Run `hugo help new` for details.

更新 6: 我在调用 hugo 的文件 entrypoint.sh 中添加了命令 pwdls -al

echo "pwd:"

pwd

echo "ls -al:"

ls -al

hugo "$@"

这是它的输出:

Starting the Hugo Action
pwd:
/github/workspace
ls -al:
total 8
drwxr-xr-x    2 1001     115           4096 Mar 15 17:39 .
drwxr-xr-x    5 root     root          4096 Mar 15 17:39 ..
Error: Unable to locate config file or config directory. Perhaps you need to create a new site.
       Run `hugo help new` for details.

似乎该操作试图 运行 hugo 进入空目录 /github/workspace

我的下一步是找出我的 git 分支的内容所在的目录。

更新7:我尝试输出目录的内容

entrypoint.sh 中,但其中 none 包含我的 Hugo 代码。

更新 8: 我将以下行添加到 entrypoint.sh 以查找包含 Hugo 源的目录:

find / -name "*archetypes*"

所有 Hugo 项目都包含该目录。

但是find什么也没找到。看起来 GitHub 操作的 Docker 是 运行ning 在错误的目录中。

这基于 srt32/hugo-action,它可能使用了旧版本的 Hugo。

首先检查 hugo 版本,看看是否 issue 6794 applies (it was fixed in january 2020 with PR 6834.

Hugo代码好像根本就没签出来。因此,解决方案是修改 GitHub 操作,以便

  1. git 安装在 Dockerized Linux 和
  2. Hugo 源代码已签出。

要执行前者,Dockerfile 需要修改如下所示(参见 RUN apk add --no-cache git):

FROM alpine:latest

LABEL "com.github.actions.name"="Hugo Actions"
LABEL "com.github.actions.description"="Commands to help with building Hugo based static sites"
LABEL "com.github.actions.icon"="mic"
LABEL "com.github.actions.color"="yellow"

LABEL "repository"="http://github.com/dpisarenko/hugo-action"
LABEL "homepage"="http://github.com/dpisarenko/hugo-action"

RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk update 
RUN apk add --no-cache hugo=0.65.3-r0 bash
RUN apk add --no-cache bash
RUN apk add --no-cache git 

ADD entrypoint.sh /
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

然后我们需要在entrypoint.sh中调用git clone:

#!/bin/bash
set -e

echo "#################################################"
echo "Starting the Hugo Action"

git clone --branch prod https://github.com/dpisarenko/hugo-wp-site.git /hugo
cd /hugo
hugo "$@"

echo "#################################################"
echo "Completed the Hugo Action"