Skip to content
Merged
19 changes: 19 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Rust v1.77 as a base image
FROM rust:1.77-slim

RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
# install python3, jinja2 pyyaml
python3 \
python3-pip \
&& pip3 install --break-system-packages jinja2 pyyaml \
# install nodejs and serve
&& curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g serve \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \

WORKDIR /workspace
ENV PATH="/root/.cargo/bin:${PATH}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO: PythonとNode.jsのバージョンが明示的に指定されていないため、Dockerfileをビルドする時期によって環境が変わってしまうことが懸念されます。
複数の言語ランタイムを併用する状況が今後も続くと思われるので、mise等の開発環境を一括で管理できるツールを導入すると良いかもしれません。

また、Pythonの依存関係のJinja2とPyYAMLについてもバージョンが固定されていないため、同様の懸念が生じます。そこで、requirements.txtを追加して依存関係を明示的に管理するか、Ryeのようなパッケージ管理ツールを導入すると良いかもしれません。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大変勉強になります。
懸念点は理解できます。一方でバージョン固定対応をすることで、記述が複雑になったり、管理すべき別ファイルが増えたり、docker build の時間が増えたり、などのデメリットも大きく感じます。
取り急ぎ以下のような編集を行いました。


Python に関しては .github/workflows/deploy.yml での記述に合わせました。こちらでもバージョン固定はされておりませんでしたので、バージョンアップによる不具合が発生した場合にはこちらでも検知でき、.devcontainer/Dockerfile の記述を deploy.yml と合わせておくという認識が取れていれば(覚えていれば)スムーズに対応できるかと思います。

https://github.com/typst-jp/typst-jp.github.io/blob/3805444542ae077ed849bb47bcccfda39b5bb1a2/.github/workflows/deploy.yml#L22

https://github.com/typst-jp/typst-jp.github.io/blob/3805444542ae077ed849bb47bcccfda39b5bb1a2/.devcontainer/Dockerfile#L10


Node.js については、私自身が不慣れでよくわかっていない部分もありますが

https://github.com/typst-jp/typst-jp.github.io/blob/3805444542ae077ed849bb47bcccfda39b5bb1a2/.devcontainer/Dockerfile#L12

こちらでメジャーバージョンは18を指定できていると思います。
serve パッケージのバージョンについては現在の最新版である v14.2.3 に指定いたしました。

https://github.com/typst-jp/typst-jp.github.io/blob/3805444542ae077ed849bb47bcccfda39b5bb1a2/.devcontainer/Dockerfile#L14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python, Node.jsの環境については、それぞれpyproject.toml, package.jsonのように標準的な環境管理のためのファイル形式が存在していて、言語ランタイムそのものの管理については、pyenvの.python-versionやnodenvの.node-versionがmiseを含む後発の環境管理ツールで広くサポートされています。CI/CDはアプリケーション本体の開発のために補助的に存在しているものなので、言語ランタイムと依存関係はこういった標準的なファイル形式で集約して管理すべきだと考えています。

しかし、現状では依存しているライブラリが少なく、これらで破壊的な変更が行われる可能性も非常に低いので、確かに管理すべきファイルが増えることのデメリットを上回るメリットがないように感じます。そこで、これらの環境については、サイト構築用のスクリプトの大規模な改変や、Node.js製のLinterの導入をする際に、個別に環境を整備すると良いかと思いました。

Copy link
Member Author

@kimushun1101 kimushun1101 Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。方針は同意です。

Node.js のバージョンの方はよくわかっていなかったのですが、deploy.yml をみると JamesIves/github-pages-deploy-action@v4 が使用されているらしく。

https://github.com/typst-jp/typst-jp.github.io/blob/04dc4a65dc013e16cee68ad4722554ff8852bba9/.github/workflows/deploy.yml#L25-L30

確認したところ、こちらの最新版では node20 になっているようでした。

https://github.com/JamesIves/github-pages-deploy-action/blob/dev/action.yml

これも時々刻々と変わってしまうものだと思いますので、とりあえずこのDokcerfile では 18 固定のままでもよろしいでしょうか?

大した手間ではなかったので 20 にしておきました。

Copy link
Member

@3w36zj6 3w36zj6 Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub Actionsの各Action内で使用されるNode.jsのバージョンは独立しており、各Actionは内部で指定されているNode.jsのバージョンを使用して実行されます。Marketplaceで公開されているActionの利用者は、内部で指定されているNode.jsのバージョンを気にする必要はありません。
注意が必要なのは、ワークフロー内にてNode.jsに依存するコマンドをrun:で実行したい場合です。Node.jsに依存した処理を実行するには、前のステップでNode.jsのセットアップを完了させておく必要があり、ここでバージョンを明示的に指定してセットアップを行うと良いです。

このPRにおいてDockerfileで指定するNode.jsのバージョンについては、このままでも大きな問題はないと思いますが、どちらかと言えば既にMaintenanceになっている18.xよりも現在のActive LTSである20.xで固定する方が良いかもしれません1

Footnotes

  1. https://github.com/nodejs/release#release-schedule

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考ページを提示していただきありがとうございます!
完全に理解しました()

typst-jp/typst-jp.github.io@9fde1d9
こちらで 20.x にして、Dockerコンテナ内で node -v して確認できました。

24 changes: 24 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Dev Containerについて

Visual Studio Codeで編集する場合には、[Dev Container](https://code.visualstudio.com/docs/devcontainers/containers)を使用してローカル環境でWebページの仕上がりを確認することができます。
Visual Studio Codeでtypst-jp.github.ioディレクトリを開き以下の操作を実施してください。
1. Ctrl+Shift+Pから`>Dev Containers: Reopen in Container`を実行
2. ビルドが完了したらブラウザで http://localhost:3000 に接続
3. ページを更新した際には、Ctrl+shift+Pから`Tasks: Run task`を実行し`preview: typst-jp documentation`を選択。ビルドが完了したらブラウザを更新。


## 別のエディターを使用している場合

別のエディターで編集している場合にもDockerfileの使用のみであれば可能です。
ターミナルから typst-jp.github.io ディレクトリ上で以下のコマンドを実行してください。
1. Docker imageをビルドして実行
```
docker build . -f .devcontainer/Dockerfile -t typst-jp-doc
docker run --name typst-jp-doc -p 3000:3000 -it -v "$(pwd):/workspace" -w /workspace --rm typst-jp-doc /bin/bash
```
2. Dockerコンテナ内でビルド
```
cargo test --package typst-docs --lib -- tests::test_docs --exact --nocapture && python3 ./gen.py && npx serve -n ./dist
```
3. ビルドが完了したらブラウザで http://localhost:3000 に接続
4. ファイルを更新した際には、2 のコマンドを再度実行して、ブラウザを更新。
11 changes: 11 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "typst-jp-domumentation",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"forwardPorts": [3000],
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached",
"workspaceFolder": "/workspace",
"postStartCommand": "cargo test --package typst-docs --lib -- tests::test_docs --exact --nocapture && python3 ./gen.py && npx serve -n ./dist"
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# General
.vscode
.vscode/*
!.vscode/tasks.json
.idea
_things
desktop.ini
Expand Down
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "preview: typst-jp documentation",
"type": "shell",
"command": "cargo test --package typst-docs --lib -- tests::test_docs --exact --nocapture && python3 ./gen.py && echo reload or open http://localhost:3000",
"problemMatcher": []
}
]
}