使用 Jinja 和 GitHub 操作构建静态网站 - 即使看起来正在创建网页,也无法部署网站?

问题描述 投票:0回答:1

我正在尝试将静态网站模板从 GitLab Pages 迁移到 GitHub Pages。该模板使用 Python/Jinja 生成静态网页,然后使用 GitHub Actions 进行构建。迁移到 GitHub 时,脚本运行时没有错误,但生成的

index.html
文件实际上似乎并不存在于任何地方。发生什么事了?

这里是我正在使用的网站模板。有两个主要的文件需要关注 - 调用执行脚本的 GitLab CI 文件:

image: python:3.6-alpine

pages:
  stage: deploy
  before_script:
  - python -m pip install -r requirements.txt
  script:
  - cd public/
  - python render.py
  - cd ..
  artifacts:
    paths:
    - public/

这就是我迁移它的方式,以确保它可以与 GitHub 操作配合使用:

name: Build and Deploy

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest  # use the latest version of ubuntu

    steps:
    - name: Checkout code
      uses: actions/checkout@v3 #v3 to ensure that everything is run on node16

    - name: Set up Python
      uses: actions/setup-python@v3 #v3, node12 will be deprecated soon
      with:
        python-version: 3.9  #just upgrading from the alpine 3.6 version

    - name: Install dependencies
      run: |
        python -m pip install -r requirements.txt #install the requirements

    - name: Build and Deploy
      run: |
        cd public/
        python render.py
        cd ..

    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: public
        path: public/

以及

render.py
目录下的
public
文件:

import json
import yaml
from jinja2 import Environment, FileSystemLoader, DebugUndefined
from jinja2_markdown import MarkdownExtension
from pathlib import Path
import datetime
from glob import glob  # Add this line for importing glob
from tqdm import tqdm

# Create an empty dictionary to store template variables to be passed
data = dict()

# Add today's date, properly formatted, to the data dict to display at the bottom
# of the page
data['today'] = datetime.date.today().strftime('%Y-%m-%d')

# Load assets that can be used by templates. Assets are yaml files with data
for pathname in tqdm(glob('assets/*.y*ml'), desc='Loading assets'):
    path = Path(pathname)
    fname = path.stem
    with path.open('r') as f:
        assetdata = yaml.safe_load(f)
    if type(assetdata) is not dict:
        assetdata = {fname: assetdata}
    data.update(assetdata)

# Scan sections in the sections directory; there will be a section header for
# each one of these
data['sections'] = []
sectionfiles = glob('sections/*.html')
try:
    order = yaml.safe_load(Path('sections/order.yaml').open('r'))
    comparator = lambda key: (order + [Path(key).stem]).index(Path(key).stem)
except FileNotFoundError:
    comparator = lambda key: key
for sectionfile in tqdm(sorted(sectionfiles, key=comparator), 
                        desc='Processing sections'):
    sectionfile = Path(sectionfile)
    fname = sectionfile.stem
    sectionenv = Environment(loader=FileSystemLoader('sections'), 
                             extensions=['jinja2_markdown.MarkdownExtension'],
                             undefined=DebugUndefined)
    sectiontempl = sectionenv.get_template(sectionfile.name)
    data['sections'] += [dict(name=fname, content=sectiontempl.render(**data))]

# Create a Jinja2 environment instance
jinja_env = Environment(loader=FileSystemLoader('templates'), 
                        extensions=['jinja2_markdown.MarkdownExtension'],
                        undefined=DebugUndefined)
# Get template
template = jinja_env.get_template('landing.html')

# Render template and output it to index.html, the default page to show
output_path = Path('output')  #change path as needed
output_path.mkdir(exist_ok=True)

output_file_path = output_path.joinpath('index.html')
with output_file_path.open('w') as out:
    out.write(template.render(**data))

print(f"index.html saved at: {output_file_path}")

我添加了一行明确指出 where

index.html
已保存(
output/index.html
),但实际上没有在 GitHub 存储库中创建这样的存储库/文件组合。据我所知,GitHub Actions 启用了读/写权限(我确保为存储库启用了此设置),但仍然没有创建 index.html 文件。

如有任何帮助,我们将不胜感激!

github gitlab github-actions gitlab-ci github-pages
1个回答
0
投票

添加

- uses: stefanzweifel/git-auto-commit-action@v5

到 GitHub Actions

.yaml
文件的末尾。

类似的东西

- name: Push artifacts back to the original repository
      uses: stefanzweifel/git-auto-commit-action@v5

有效。该代码依赖于this存储库。

© www.soinside.com 2019 - 2024. All rights reserved.