From 37c5d586fa316e458e12f6e6637940cbbb72b01c Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 13:00:47 +0900 Subject: [PATCH 01/49] =?UTF-8?q?=E3=83=97=E3=83=AB=E3=83=AA=E3=82=AF?= =?UTF-8?q?=E6=83=85=E5=A0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 41accff..6c5339d 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -37,10 +37,8 @@ jobs: run: | echo 'echo ${{ toJson(github.event_name) }}' echo 'echo ${{ toJson(github.event) }}' - echo 'echo ${{ toJson(gitea.event_name) }}' - echo 'echo ${{ toJson(gitea.event) }}' curl -v -X POST \ -H "Content-Type: application/json" \ -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ -d "{\"body\": \"## :mag: Ruff Lint Results\n\`\`\`\ntest\n\`\`\`\"}" \ - ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/${{ github.event.pull_request.number }}/comments + ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments From ff9c7b25f6a8d571706c00a5cdfc2fb11247d950 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:33:31 +0900 Subject: [PATCH 02/49] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=BF=E3=83=BC?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 15 +++++++-- .gitignore | 1 + scripts/generate_linter_from_json.py | 49 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 scripts/generate_linter_from_json.py diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 6c5339d..090d3fe 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -32,13 +32,22 @@ jobs: pip install -r requirements.txt pip install -r requirements-dev.txt + - name: Check Initer + id: checkIniter + run: | + ruff check . --output-format json --output-file ruff-report.json + + - name: Generate Linter Report + id: generateLinterReport + run: | + python scripts/generate_linter_from_json.py + + - name: pull_request message with Ruff Lint results id: prMessageRuffLint run: | - echo 'echo ${{ toJson(github.event_name) }}' - echo 'echo ${{ toJson(github.event) }}' curl -v -X POST \ -H "Content-Type: application/json" \ -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - -d "{\"body\": \"## :mag: Ruff Lint Results\n\`\`\`\ntest\n\`\`\`\"}" \ + -d @lint-result.json \ ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments diff --git a/.gitignore b/.gitignore index 1dddfae..139d1f8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__/ *.py[cod] *$py.class ruff-report.* +lint-result.json # C extensions *.so diff --git a/scripts/generate_linter_from_json.py b/scripts/generate_linter_from_json.py new file mode 100644 index 0000000..9d3c076 --- /dev/null +++ b/scripts/generate_linter_from_json.py @@ -0,0 +1,49 @@ +import json + +class GenerateLinter: + def __init__(self, + json_file="ruff-report.json", + output_file="lint-result.json"): + """ + 初期化 + """ + self.json_file = json_file + self.output_file = output_file + + def _genarate_lint_report(self, data: list) -> str: + _str = "" + if not data: + _str += "## Linter(リンタ)指摘事項なし\n\n" + _str += "素晴らしいコードです!🎉\n" + return _str + + _str += "## Linter(リンタ)レビュー\n\n" + _str += "以下の指摘事項があります。コードを見直してください。\n\n" + + _str += "### 指摘事項一覧\n" + _str += "|コード|重要性|ファイル名|行数|課題内容\n" + _str += "|---|---|---|---|---|\n" + for issue in data: + code = issue.get("code", "-") + severity = issue.get("severity", "-") + filename = issue.get("filename", "-") + line = issue.get("location", {}).get("line", "-") + message = issue.get("message", "-") + _str += f"|{code}|{severity}|{filename}|{line}|{message}|\n" + return _str + + def generate_lint_report_json(self): + + with open(self.json_file, "r") as f: + data = json.load(f) + + with open(self.output_file, "w") as f: + report = {"body": self._genarate_lint_report(data)} + json.dump(report, f, ensure_ascii=False, indent=4) + + print(f"Linter report generated: {self.output_file}") + + +if __name__ == "__main__": + generator = GenerateLinter() + generator.generate_lint_report_json() \ No newline at end of file From 89326a1c284caafbf83e0661a4508e9c992f4a25 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:36:39 +0900 Subject: [PATCH 03/49] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=BF=E3=83=BC?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 090d3fe..1ebe41e 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -36,6 +36,7 @@ jobs: id: checkIniter run: | ruff check . --output-format json --output-file ruff-report.json + echo "Ruff Lint Check completed. ruff-report.json" - name: Generate Linter Report id: generateLinterReport From dd472e0a90c709c9a509c2a6f01cc27180dddd3f Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:40:46 +0900 Subject: [PATCH 04/49] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 1ebe41e..499b43b 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -35,20 +35,21 @@ jobs: - name: Check Initer id: checkIniter run: | + echo "Running Ruff Lint Check..." ruff check . --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" - - name: Generate Linter Report - id: generateLinterReport - run: | - python scripts/generate_linter_from_json.py + # - name: Generate Linter Report + # id: generateLinterReport + # run: | + # python scripts/generate_linter_from_json.py - - name: pull_request message with Ruff Lint results - id: prMessageRuffLint - run: | - curl -v -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - -d @lint-result.json \ - ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments + # - name: pull_request message with Ruff Lint results + # id: prMessageRuffLint + # run: | + # curl -v -X POST \ + # -H "Content-Type: application/json" \ + # -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + # -d @lint-result.json \ + # ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments From f183cc611481591b10492df372e372caac9daa87 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:42:42 +0900 Subject: [PATCH 05/49] debug --- .github/workflows/pyruff.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 499b43b..ef57e0f 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -36,6 +36,10 @@ jobs: id: checkIniter run: | echo "Running Ruff Lint Check..." + python -m ruff --version + echo "Running Ruff Lint Check..." + python ruff --version + ruff check . ruff check . --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" From 78bade9d1112d6fc6018b2197fa5a5ce403b46a7 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:44:13 +0900 Subject: [PATCH 06/49] debug --- .github/workflows/pyruff.yml | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index ef57e0f..3bd342d 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -36,24 +36,20 @@ jobs: id: checkIniter run: | echo "Running Ruff Lint Check..." - python -m ruff --version - echo "Running Ruff Lint Check..." - python ruff --version - ruff check . - ruff check . --output-format json --output-file ruff-report.json + python -m ruff check . --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" - # - name: Generate Linter Report - # id: generateLinterReport - # run: | - # python scripts/generate_linter_from_json.py + - name: Generate Linter Report + id: generateLinterReport + run: | + python scripts/generate_linter_from_json.py - # - name: pull_request message with Ruff Lint results - # id: prMessageRuffLint - # run: | - # curl -v -X POST \ - # -H "Content-Type: application/json" \ - # -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - # -d @lint-result.json \ - # ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments + - name: pull_request message with Ruff Lint results + id: prMessageRuffLint + run: | + curl -v -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + -d @lint-result.json \ + ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments From b1abb13490d5478db6fd9912230bade6a56dd909 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:46:10 +0900 Subject: [PATCH 07/49] test --- .github/workflows/pyruff.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 3bd342d..42a2c91 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -36,6 +36,7 @@ jobs: id: checkIniter run: | echo "Running Ruff Lint Check..." + python -m ruff --version python -m ruff check . --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" From 1fde748cb1638e540c86f22ed4a2e9914cbfd8d4 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:48:20 +0900 Subject: [PATCH 08/49] test --- .github/workflows/pyruff.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 42a2c91..3275072 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -35,6 +35,7 @@ jobs: - name: Check Initer id: checkIniter run: | + ls -l echo "Running Ruff Lint Check..." python -m ruff --version python -m ruff check . --output-format json --output-file ruff-report.json From 53c1673aeeb9daf0443724363f37d736e7f54535 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:50:21 +0900 Subject: [PATCH 09/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 3275072..3f39132 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -37,8 +37,7 @@ jobs: run: | ls -l echo "Running Ruff Lint Check..." - python -m ruff --version - python -m ruff check . --output-format json --output-file ruff-report.json + python -m ruff check src tests examples --no-cache --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" - name: Generate Linter Report From e54a4539354cfbe98ea1722bea7eebb48579796b Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 14:52:15 +0900 Subject: [PATCH 10/49] test --- .github/workflows/pyruff.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 3f39132..5fd97ab 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -35,7 +35,7 @@ jobs: - name: Check Initer id: checkIniter run: | - ls -l + ls -la echo "Running Ruff Lint Check..." python -m ruff check src tests examples --no-cache --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" From 3151004281dbda11d6c3c4de41f428db78437c01 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 15:01:10 +0900 Subject: [PATCH 11/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 1 + requirements-dev.txt | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 5fd97ab..ac4f871 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -37,6 +37,7 @@ jobs: run: | ls -la echo "Running Ruff Lint Check..." + python -m ruff --version python -m ruff check src tests examples --no-cache --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" diff --git a/requirements-dev.txt b/requirements-dev.txt index 6fe2354..18ae90a 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,6 +2,5 @@ pytest pytest-cov coverage-badge - # Linting tool -ruff +ruff==0.14.7 From e429362634fe860c0280716cc9a90e81cf4d66cb Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 15:07:07 +0900 Subject: [PATCH 12/49] =?UTF-8?q?=E3=83=87=E3=83=90=E3=83=83=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index ac4f871..0e5408b 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -35,23 +35,27 @@ jobs: - name: Check Initer id: checkIniter run: | + set +e ls -la echo "Running Ruff Lint Check..." python -m ruff --version python -m ruff check src tests examples --no-cache --output-format json --output-file ruff-report.json + echo "ruff exit code: $?" echo "Ruff Lint Check completed. ruff-report.json" - - - name: Generate Linter Report - id: generateLinterReport - run: | - python scripts/generate_linter_from_json.py - - name: pull_request message with Ruff Lint results - id: prMessageRuffLint - run: | - curl -v -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - -d @lint-result.json \ - ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments + + # - name: Generate Linter Report + # id: generateLinterReport + # run: | + # python scripts/generate_linter_from_json.py + + + # - name: pull_request message with Ruff Lint results + # id: prMessageRuffLint + # run: | + # curl -v -X POST \ + # -H "Content-Type: application/json" \ + # -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + # -d @lint-result.json \ + # ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments From f2961daec166f5b853e53970808d8999c6a3e9d0 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 18:27:51 +0900 Subject: [PATCH 13/49] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=BF=E3=83=BC?= =?UTF-8?q?=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 0e5408b..a2bb9eb 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -35,27 +35,21 @@ jobs: - name: Check Initer id: checkIniter run: | - set +e - ls -la echo "Running Ruff Lint Check..." - python -m ruff --version - python -m ruff check src tests examples --no-cache --output-format json --output-file ruff-report.json - echo "ruff exit code: $?" + python -m ruff check . --exit-zero --no-cache --output-format json --output-file ruff-report.json echo "Ruff Lint Check completed. ruff-report.json" + - name: Generate Linter Report + id: generateLinterReport + run: | + python scripts/generate_linter_from_json.py - # - name: Generate Linter Report - # id: generateLinterReport - # run: | - # python scripts/generate_linter_from_json.py - - - # - name: pull_request message with Ruff Lint results - # id: prMessageRuffLint - # run: | - # curl -v -X POST \ - # -H "Content-Type: application/json" \ - # -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - # -d @lint-result.json \ - # ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments + - name: pull_request message with Ruff Lint results + id: prMessageRuffLint + run: | + curl -v -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + -d @lint-result.json \ + ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments From 3ed1efbbe058db64928cf330031bf15b7df42048 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 20:38:27 +0900 Subject: [PATCH 14/49] =?UTF-8?q?=E3=83=AC=E3=83=9D=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 2 +- lint-result.md | 15 +++++ scripts/generate_linter.py | 90 ++++++++++++++++++++++++++++ scripts/generate_linter.sh | 40 ------------- scripts/generate_linter_from_json.py | 49 --------------- 5 files changed, 106 insertions(+), 90 deletions(-) create mode 100644 lint-result.md create mode 100644 scripts/generate_linter.py delete mode 100644 scripts/generate_linter.sh delete mode 100644 scripts/generate_linter_from_json.py diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index a2bb9eb..6a11a11 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -42,7 +42,7 @@ jobs: - name: Generate Linter Report id: generateLinterReport run: | - python scripts/generate_linter_from_json.py + python scripts/generate_linter.py - name: pull_request message with Ruff Lint results diff --git a/lint-result.md b/lint-result.md new file mode 100644 index 0000000..c292e80 --- /dev/null +++ b/lint-result.md @@ -0,0 +1,15 @@ +## Linter(リンタ)レビュー + +以下の指摘事項があります。コードを見直してください。 + +総数:6個 + +### 指摘事項一覧 +|コード|重要性|項目|ファイル名|行数| +|---|---|---|---|---| +|W291|🟢低|行末に不要な空白があります。|examples/example.py|14行目27列 〜 14行目28列| +|W291|🟢低|行末に不要な空白があります。|scripts/generate_coverage.py|5行目23列 〜 5行目24列| +|E501|🟢低|行が長すぎます。79文字以内にしてください。|scripts/generate_coverage.py|38行目89列 〜 38行目107列| +|W291|🟢低|行末に不要な空白があります。|scripts/generate_linter_from_json.py|4行目23列 〜 4行目24列| +|W291|🟢低|行末に不要な空白があります。|scripts/generate_linter_from_json.py|25行目31列 〜 25行目39列| +|W292|🟢低|ファイルの最後に改行がありません。|scripts/generate_linter_from_json.py|53行目42列 〜 53行目42列| diff --git a/scripts/generate_linter.py b/scripts/generate_linter.py new file mode 100644 index 0000000..93ea827 --- /dev/null +++ b/scripts/generate_linter.py @@ -0,0 +1,90 @@ +import json +from pathlib import Path + +PROJECT_NAME = Path(".").resolve().name +print(f"Project Name: {PROJECT_NAME}") + + +CODE_MAP = { + "W291": {"message": "行末に不要な空白があります。", "severity": "🟢低"}, + "W292": {"message": "ファイルの最後に改行がありません。", "severity": "🟢低"} , + "E501": { + "message": "行が長すぎます。79文字以内にしてください。", + "severity": "🟢低", + }, +} + +def get_relative_path(absolute_path: str) -> str: + """ + 絶対パスからプロジェクトルートからの相対パスを取得 + """ + try: + index = absolute_path.index(PROJECT_NAME) + return absolute_path[index + len(PROJECT_NAME) + 1 :] + except ValueError: + return absolute_path + + +class GenerateLinter: + def __init__(self, json_file="ruff-report.json", output_file="lint-result.json"): + """ + 初期化 + """ + self.json_file = json_file + self.output_file = output_file + + def _genarate_lint_report(self, data: list) -> str: + _str = "" + if not data: + _str += "## Linter(リンタ)指摘事項なし\n\n" + _str += "素晴らしいコードです!🎉\n" + return _str + + _str += "## Linter(リンタ)レビュー\n\n" + _str += "以下の指摘事項があります。コードを見直してください。\n\n" + + _str += f"総数:{len(data)}個\n" + + _str += "### 指摘事項一覧\n" + _str += "|コード|重要性|項目|ファイル名|行数|\n" + _str += "|---|---|---|---|---|\n" + for issue in data: + code = issue.get("code", "-") + severity = ( + CODE_MAP.get(code, {}).get("severity", "❓不明") + if code != "-" + else "-" + ) + message = CODE_MAP.get(code, {}).get("message", issue.get("message", "-")) + filename = get_relative_path(issue.get("filename", "-")) + line = "" + if issue.get("location") and issue["location"].get("row"): + line = f"{issue['location']['row']}行目" + if issue["location"].get("column"): + line += f"{issue['location']['column']}列" + + if issue.get("end_location"): + if issue["end_location"].get("row"): + line += f" 〜 {issue['end_location']['row']}行目" + if issue["end_location"].get("column"): + line += f"{issue['end_location']['column']}列" + + _str += f"|{code}|{severity}|{message}|{filename}|{line}|\n" + return _str + + def generate_lint_report_json(self): + with open(self.json_file, "r") as f: + data = json.load(f) + + with open(self.output_file, "w") as f: + report_body = self._genarate_lint_report(data) + f.write(report_body) + # report = {"body": self._genarate_lint_report(data)} + # json.dump(report, f, ensure_ascii=False, indent=4) + + print(f"Linter report generated: {self.output_file}") + + +if __name__ == "__main__": + generator = GenerateLinter() + generator.generate_lint_report_json() diff --git a/scripts/generate_linter.sh b/scripts/generate_linter.sh deleted file mode 100644 index fbad27b..0000000 --- a/scripts/generate_linter.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -# scripts/genarater_linter.sh - -set -u # 未定義変数の利用で落とす(-e は付けない) - -RUFF_STATUS=0 -OUTPUT_MD=${1:-lint-result.md} - -{ - echo "## 🧹 Ruff Lint Result" - echo - echo "### Command" - echo '```bash' - echo '$ ruff check .' - echo '```' - echo - echo "### Output" - echo '```text' - # ruff がエラーになってもスクリプト自体は落とさず、ステータスだけ拾う - ruff check . || RUFF_STATUS=$? - echo '```' - echo - echo "### Summary" - echo - echo "| Tool | Status |" - echo "|------|--------|" - if [ "$RUFF_STATUS" -eq 0 ]; then - echo "| ruff | ✅ OK |" - else - echo "| ruff | ❌ Error (exit $RUFF_STATUS) |" - fi -} | tee "$OUTPUT_MD" - -# GitHub Actions から呼ばれた場合は、出力変数としてステータスを渡す -if [ -n "${GITHUB_OUTPUT:-}" ]; then - echo "ruff_status=$RUFF_STATUS" >> "$GITHUB_OUTPUT" -fi - -# ここでは常に 0 で終了(失敗にするかどうかはワークフロー側で制御) -exit 0 \ No newline at end of file diff --git a/scripts/generate_linter_from_json.py b/scripts/generate_linter_from_json.py deleted file mode 100644 index 9d3c076..0000000 --- a/scripts/generate_linter_from_json.py +++ /dev/null @@ -1,49 +0,0 @@ -import json - -class GenerateLinter: - def __init__(self, - json_file="ruff-report.json", - output_file="lint-result.json"): - """ - 初期化 - """ - self.json_file = json_file - self.output_file = output_file - - def _genarate_lint_report(self, data: list) -> str: - _str = "" - if not data: - _str += "## Linter(リンタ)指摘事項なし\n\n" - _str += "素晴らしいコードです!🎉\n" - return _str - - _str += "## Linter(リンタ)レビュー\n\n" - _str += "以下の指摘事項があります。コードを見直してください。\n\n" - - _str += "### 指摘事項一覧\n" - _str += "|コード|重要性|ファイル名|行数|課題内容\n" - _str += "|---|---|---|---|---|\n" - for issue in data: - code = issue.get("code", "-") - severity = issue.get("severity", "-") - filename = issue.get("filename", "-") - line = issue.get("location", {}).get("line", "-") - message = issue.get("message", "-") - _str += f"|{code}|{severity}|{filename}|{line}|{message}|\n" - return _str - - def generate_lint_report_json(self): - - with open(self.json_file, "r") as f: - data = json.load(f) - - with open(self.output_file, "w") as f: - report = {"body": self._genarate_lint_report(data)} - json.dump(report, f, ensure_ascii=False, indent=4) - - print(f"Linter report generated: {self.output_file}") - - -if __name__ == "__main__": - generator = GenerateLinter() - generator.generate_lint_report_json() \ No newline at end of file From 02d6d34b022d4e0607bc0f7c8dd988bafb924c71 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 4 Dec 2025 20:39:48 +0900 Subject: [PATCH 15/49] =?UTF-8?q?=E8=87=AA=E5=8B=95=E3=83=AC=E3=83=93?= =?UTF-8?q?=E3=83=A5=E3=83=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/generate_linter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/generate_linter.py b/scripts/generate_linter.py index 93ea827..2d706b2 100644 --- a/scripts/generate_linter.py +++ b/scripts/generate_linter.py @@ -77,10 +77,10 @@ class GenerateLinter: data = json.load(f) with open(self.output_file, "w") as f: - report_body = self._genarate_lint_report(data) - f.write(report_body) - # report = {"body": self._genarate_lint_report(data)} - # json.dump(report, f, ensure_ascii=False, indent=4) + # report_body = self._genarate_lint_report(data) + # f.write(report_body) + report = {"body": self._genarate_lint_report(data)} + json.dump(report, f, ensure_ascii=False, indent=4) print(f"Linter report generated: {self.output_file}") From f0a74c112201afeb46bed1beeedd115f04942c7a Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 00:44:16 +0900 Subject: [PATCH 16/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pyruff.yml | 21 +++++++++++++---- .gitignore | 2 ++ examples/example.py | 2 +- lint-result.md | 15 ------------ ruff.toml | 8 ++++++- scripts/generate_coverage.py | 42 ++++++++++++++++++--------------- scripts/generate_linter.py | 45 +++++++++++++++++++++++++----------- src/utils/singleton.py | 1 + 8 files changed, 83 insertions(+), 53 deletions(-) delete mode 100644 lint-result.md diff --git a/.github/workflows/pyruff.yml b/.github/workflows/pyruff.yml index 6a11a11..0945342 100644 --- a/.github/workflows/pyruff.yml +++ b/.github/workflows/pyruff.yml @@ -48,8 +48,19 @@ jobs: - name: pull_request message with Ruff Lint results id: prMessageRuffLint run: | - curl -v -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ - -d @lint-result.json \ - ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/8/comments + # イベントがプルリクエストの場合はPRにコメントを追加する + if [ "${{ github.event_name }}" = "pull_request" ]; then + echo "Posting Ruff Lint results to Pull Request..." + curl -v -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + -d @lint-result.json \ + ${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/issues/${{ github.event.pull_request.number }}/comments + else + echo "Not a pull request event." + echo "Ruff Lint results:" + echo "-------------------" + cat lint-result.md + echo "-------------------" + echo "No PR detected. Skipping API comment." + fi diff --git a/.gitignore b/.gitignore index 139d1f8..00b439c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,9 @@ __pycache__/ *.py[cod] *$py.class ruff-report.* +lint-result.md lint-result.json + # C extensions *.so diff --git a/examples/example.py b/examples/example.py index 029edea..0976ae4 100644 --- a/examples/example.py +++ b/examples/example.py @@ -11,7 +11,7 @@ logger = get_logger(__name__) def example(): logger.info("Application started") - print("Hello, World!") + print("Hello, World!") example() diff --git a/lint-result.md b/lint-result.md deleted file mode 100644 index c292e80..0000000 --- a/lint-result.md +++ /dev/null @@ -1,15 +0,0 @@ -## Linter(リンタ)レビュー - -以下の指摘事項があります。コードを見直してください。 - -総数:6個 - -### 指摘事項一覧 -|コード|重要性|項目|ファイル名|行数| -|---|---|---|---|---| -|W291|🟢低|行末に不要な空白があります。|examples/example.py|14行目27列 〜 14行目28列| -|W291|🟢低|行末に不要な空白があります。|scripts/generate_coverage.py|5行目23列 〜 5行目24列| -|E501|🟢低|行が長すぎます。79文字以内にしてください。|scripts/generate_coverage.py|38行目89列 〜 38行目107列| -|W291|🟢低|行末に不要な空白があります。|scripts/generate_linter_from_json.py|4行目23列 〜 4行目24列| -|W291|🟢低|行末に不要な空白があります。|scripts/generate_linter_from_json.py|25行目31列 〜 25行目39列| -|W292|🟢低|ファイルの最後に改行がありません。|scripts/generate_linter_from_json.py|53行目42列 〜 53行目42列| diff --git a/ruff.toml b/ruff.toml index d2b3aac..bafe49b 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,6 +1,12 @@ line-length = 88 # 末尾スペース・空行まわりをチェックするのは E と W系のルール +# E7xx/E9xx(構文/実行時エラーの可能性) +# W1xx/W5xx(スタイル・フォーマット関連) +# DXXX(ドキュメンテーション文字列関連) +# F (未使用インポートなどのエラー) +# BXX(バグの可能性) + [lint] -select = ["E", "W"] +select = ["F", "E", "W", "D101", "B"] ignore = [] \ No newline at end of file diff --git a/scripts/generate_coverage.py b/scripts/generate_coverage.py index 00a31df..a2e57d6 100644 --- a/scripts/generate_coverage.py +++ b/scripts/generate_coverage.py @@ -2,9 +2,11 @@ import re class GenerateCoverage: - def __init__(self, - coverage_file="pytest-coverage.txt", - output_file="coverage_table.md"): + """カバレッジ結果を解析して Markdown テーブルを生成""" + + def __init__( + self, coverage_file="pytest-coverage.txt", output_file="coverage_table.md" + ): """ 初期化 @@ -35,26 +37,28 @@ class GenerateCoverage: if in_coverage_section and line.strip().startswith("---"): continue # Coverage セクションの終わりを検出(TOTAL行の次の空行) - if in_coverage_section and (line.strip().startswith("TOTAL") or line.strip().startswith("=")): + if in_coverage_section and ( + line.strip().startswith("TOTAL") or line.strip().startswith("=") + ): break # Coverage データを抽出 if in_coverage_section: - match = re.match( - r"(.+?)\s+(\d+)\s+(\d+)\s+(\d+%)\s*(.*)", line) + match = re.match(r"(.+?)\s+(\d+)\s+(\d+)\s+(\d+%)\s*(.*)", line) if match: filename = match.group(1).strip() statements = match.group(2).strip() missed = match.group(3).strip() coverage = match.group(4).strip() - missing_lines = match.group( - 5).strip() if match.group(5) else "-" - coverage_info.append({ - "filename": filename, - "statements": statements, - "missed": missed, - "coverage": coverage, - "missing_lines": missing_lines - }) + missing_lines = match.group(5).strip() if match.group(5) else "-" + coverage_info.append( + { + "filename": filename, + "statements": statements, + "missed": missed, + "coverage": coverage, + "missing_lines": missing_lines, + } + ) self.coverage_data = coverage_info @@ -72,9 +76,11 @@ class GenerateCoverage: # テーブル行を生成 table_rows = [ - (f"| {data['filename']} | {data['statements']} | " - f"{data['missed']} | {data['coverage']} | " - f"{data['missing_lines']} |") + ( + f"| {data['filename']} | {data['statements']} | " + f"{data['missed']} | {data['coverage']} | " + f"{data['missing_lines']} |" + ) for data in self.coverage_data ] diff --git a/scripts/generate_linter.py b/scripts/generate_linter.py index 2d706b2..0e9917a 100644 --- a/scripts/generate_linter.py +++ b/scripts/generate_linter.py @@ -7,13 +7,18 @@ print(f"Project Name: {PROJECT_NAME}") CODE_MAP = { "W291": {"message": "行末に不要な空白があります。", "severity": "🟢低"}, - "W292": {"message": "ファイルの最後に改行がありません。", "severity": "🟢低"} , + "W292": {"message": "ファイルの最後に改行がありません。", "severity": "🟢低"}, "E501": { "message": "行が長すぎます。79文字以内にしてください。", "severity": "🟢低", }, + "D101": { + "message": "クラスにドキュメンテーション文字列がありません。", + "severity": "⚪️無害", + }, } + def get_relative_path(absolute_path: str) -> str: """ 絶対パスからプロジェクトルートからの相対パスを取得 @@ -26,7 +31,8 @@ def get_relative_path(absolute_path: str) -> str: class GenerateLinter: - def __init__(self, json_file="ruff-report.json", output_file="lint-result.json"): + """Linterレポートを生成するクラス""" + def __init__(self, json_file="ruff-report.json", output_file="lint-result"): """ 初期化 """ @@ -46,43 +52,56 @@ class GenerateLinter: _str += f"総数:{len(data)}個\n" _str += "### 指摘事項一覧\n" - _str += "|コード|重要性|項目|ファイル名|行数|\n" - _str += "|---|---|---|---|---|\n" + _str += "|コード|重要性|項目|ファイル名|行数|自動修正|\n" + _str += "|---|---|---|---|---|---|\n" for issue in data: code = issue.get("code", "-") severity = ( - CODE_MAP.get(code, {}).get("severity", "❓不明") - if code != "-" - else "-" + CODE_MAP.get(code, {}).get("severity", "❓不明") if code != "-" else "-" ) message = CODE_MAP.get(code, {}).get("message", issue.get("message", "-")) filename = get_relative_path(issue.get("filename", "-")) + file_link = f"./{filename}" + line = "" if issue.get("location") and issue["location"].get("row"): line = f"{issue['location']['row']}行目" if issue["location"].get("column"): line += f"{issue['location']['column']}列" - if issue.get("end_location"): if issue["end_location"].get("row"): line += f" 〜 {issue['end_location']['row']}行目" if issue["end_location"].get("column"): line += f"{issue['end_location']['column']}列" + auto_fix = "✅" if issue.get("fix") else "❌" + + _str += f"|{code}|{severity}|{message}|" + _str += f"[{filename}]({file_link})|{line}|{auto_fix}|\n" + + _str += "\n\n" + _str += "### 自動修正コマンド\n" + _str += ("自動修正が可能な指摘事項については、" + "以下のコマンドで自動修正を試みることができます。\n\n" + ) + _str += "```bash\n" + _str += "ruff check --fix .\n" + _str += "```\n\n" - _str += f"|{code}|{severity}|{message}|{filename}|{line}|\n" return _str def generate_lint_report_json(self): with open(self.json_file, "r") as f: data = json.load(f) - with open(self.output_file, "w") as f: - # report_body = self._genarate_lint_report(data) - # f.write(report_body) + with open(f"{self.output_file}.md", "w") as f: + report_body = self._genarate_lint_report(data) + f.write(report_body) + + with open(f"{self.output_file}.json", "w") as f: report = {"body": self._genarate_lint_report(data)} json.dump(report, f, ensure_ascii=False, indent=4) - print(f"Linter report generated: {self.output_file}") + print(f"Linter report generated: {self.output_file}.md, {self.output_file}.json") if __name__ == "__main__": diff --git a/src/utils/singleton.py b/src/utils/singleton.py index 8ac3278..54385f0 100644 --- a/src/utils/singleton.py +++ b/src/utils/singleton.py @@ -11,6 +11,7 @@ import threading class Singleton(object): + """シングルトンパターンの基底クラス""" _instances = {} _lock = threading.Lock() From 1d865583b25e5cb25cc2eeaf0a5c9ab1a0757b37 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 00:49:01 +0900 Subject: [PATCH 17/49] =?UTF-8?q?PEP8=E3=81=AB=E6=BA=96=E6=8B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruff.toml | 2 +- scripts/generate_linter.py | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ruff.toml b/ruff.toml index bafe49b..5e76b6c 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,4 +1,4 @@ -line-length = 88 +line-length = 79 # 末尾スペース・空行まわりをチェックするのは E と W系のルール # E7xx/E9xx(構文/実行時エラーの可能性) diff --git a/scripts/generate_linter.py b/scripts/generate_linter.py index 0e9917a..0461949 100644 --- a/scripts/generate_linter.py +++ b/scripts/generate_linter.py @@ -7,7 +7,10 @@ print(f"Project Name: {PROJECT_NAME}") CODE_MAP = { "W291": {"message": "行末に不要な空白があります。", "severity": "🟢低"}, - "W292": {"message": "ファイルの最後に改行がありません。", "severity": "🟢低"}, + "W292": { + "message": "ファイルの最後に改行がありません。", + "severity": "🟢低", + }, "E501": { "message": "行が長すぎます。79文字以内にしてください。", "severity": "🟢低", @@ -32,7 +35,10 @@ def get_relative_path(absolute_path: str) -> str: class GenerateLinter: """Linterレポートを生成するクラス""" - def __init__(self, json_file="ruff-report.json", output_file="lint-result"): + + def __init__( + self, json_file="ruff-report.json", output_file="lint-result" + ): """ 初期化 """ @@ -57,9 +63,13 @@ class GenerateLinter: for issue in data: code = issue.get("code", "-") severity = ( - CODE_MAP.get(code, {}).get("severity", "❓不明") if code != "-" else "-" + CODE_MAP.get(code, {}).get("severity", "❓不明") + if code != "-" + else "-" + ) + message = CODE_MAP.get(code, {}).get( + "message", issue.get("message", "-") ) - message = CODE_MAP.get(code, {}).get("message", issue.get("message", "-")) filename = get_relative_path(issue.get("filename", "-")) file_link = f"./{filename}" @@ -80,8 +90,9 @@ class GenerateLinter: _str += "\n\n" _str += "### 自動修正コマンド\n" - _str += ("自動修正が可能な指摘事項については、" - "以下のコマンドで自動修正を試みることができます。\n\n" + _str += ( + "自動修正が可能な指摘事項については、" + "以下のコマンドで自動修正を試みることができます。\n\n" ) _str += "```bash\n" _str += "ruff check --fix .\n" @@ -101,7 +112,10 @@ class GenerateLinter: report = {"body": self._genarate_lint_report(data)} json.dump(report, f, ensure_ascii=False, indent=4) - print(f"Linter report generated: {self.output_file}.md, {self.output_file}.json") + print( + f"Linter report generated: {self.output_file}.md" + f" and {self.output_file}.json" + ) if __name__ == "__main__": From 6828fe684d4f2bf01531b0cf174d432a19687fa2 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 00:51:08 +0900 Subject: [PATCH 18/49] =?UTF-8?q?=E8=A1=8C=E6=95=B0=E3=81=AE=E5=88=B6?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/example.py | 4 +++- scripts/generate_coverage.py | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/example.py b/examples/example.py index 0976ae4..db7d631 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,7 +1,9 @@ import sys import os -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src"))) +sys.path.append( + os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")) +) from utils.custom_logger import get_logger diff --git a/scripts/generate_coverage.py b/scripts/generate_coverage.py index a2e57d6..79d6c21 100644 --- a/scripts/generate_coverage.py +++ b/scripts/generate_coverage.py @@ -5,7 +5,9 @@ class GenerateCoverage: """カバレッジ結果を解析して Markdown テーブルを生成""" def __init__( - self, coverage_file="pytest-coverage.txt", output_file="coverage_table.md" + self, + coverage_file="pytest-coverage.txt", + output_file="coverage_table.md", ): """ 初期化 @@ -38,18 +40,23 @@ class GenerateCoverage: continue # Coverage セクションの終わりを検出(TOTAL行の次の空行) if in_coverage_section and ( - line.strip().startswith("TOTAL") or line.strip().startswith("=") + line.strip().startswith("TOTAL") + or line.strip().startswith("=") ): break # Coverage データを抽出 if in_coverage_section: - match = re.match(r"(.+?)\s+(\d+)\s+(\d+)\s+(\d+%)\s*(.*)", line) + match = re.match( + r"(.+?)\s+(\d+)\s+(\d+)\s+(\d+%)\s*(.*)", line + ) if match: filename = match.group(1).strip() statements = match.group(2).strip() missed = match.group(3).strip() coverage = match.group(4).strip() - missing_lines = match.group(5).strip() if match.group(5) else "-" + missing_lines = ( + match.group(5).strip() if match.group(5) else "-" + ) coverage_info.append( { "filename": filename, @@ -71,8 +78,12 @@ class GenerateCoverage: print("Parsed coverage data.") # Markdown テーブルヘッダー - table_header = "| File | Statements | Missed | Coverage | Missing Lines |\n" - table_header += "|------|------------|--------|----------|---------------|\n" + table_header = ( + "| File | Statements | Missed | Coverage | Missing Lines |\n" + ) + table_header += ( + "|------|------------|--------|----------|---------------|\n" + ) # テーブル行を生成 table_rows = [ From b6e6546bd7271f29d868ae5c5b10696d1948b98c Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 00:52:51 +0900 Subject: [PATCH 19/49] =?UTF-8?q?=E8=A1=8C=E6=95=B0=E3=81=AE=E5=88=B6?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/custom_logger.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/custom_logger.py b/src/utils/custom_logger.py index 9a25dca..65d5b39 100644 --- a/src/utils/custom_logger.py +++ b/src/utils/custom_logger.py @@ -6,9 +6,8 @@ from .singleton import Singleton class CustomLogger(Singleton): """ - Singleton logger class that initializes a logger with a specified name and log file. - It provides a method to log entry and exit of functions. - + Singleton logger class that initializes a logger with a specified name + and log file.It provides a method to log entry and exit of functions. """ def __init__(self, name="main", log_file=None, level=logging.INFO): @@ -22,7 +21,8 @@ class CustomLogger(Singleton): self.logger.propagate = False formatter = logging.Formatter( - "%(asctime)s %(levelname)s [%(filename)s:%(lineno)3d]: %(message)s" + "%(asctime)s %(levelname)s " + "[%(filename)s:%(lineno)3d]: %(message)s" ) # Console handler From c38992d942d1152b109875a2a7def44ed0f10f7a Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 01:25:24 +0900 Subject: [PATCH 20/49] =?UTF-8?q?DOCS=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/Makefile | 20 ++++++++++++++++++++ docs/conf.py | 35 +++++++++++++++++++++++++++++++++++ docs/index.rst | 20 ++++++++++++++++++++ docs/main.rst | 7 +++++++ docs/make.bat | 35 +++++++++++++++++++++++++++++++++++ docs/modules.rst | 9 +++++++++ docs/utils.rst | 29 +++++++++++++++++++++++++++++ readme/python_init.md | 27 ++++++++++++++++++++++++++- requirements-dev.txt | 5 +++++ src/utils/__init__.py | 0 10 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 docs/Makefile create mode 100644 docs/conf.py create mode 100644 docs/index.rst create mode 100644 docs/main.rst create mode 100644 docs/make.bat create mode 100644 docs/modules.rst create mode 100644 docs/utils.rst create mode 100644 src/utils/__init__.py diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..ed88099 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..9c6282f --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,35 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information +import os +import sys +sys.path.insert(0, os.path.abspath('../src')) + +project = 'プロジェクト名を設定してください' +copyright = '2025, 作成者名を設定してください' +author = '作成者名を設定してください' +release = '1.0.0' + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.napoleon', # GoogleスタイルやNumPyスタイルのdocstring対応 + 'sphinx.ext.viewcode', # ソースコードへのリンク + 'sphinx_rtd_theme'] + +templates_path = ['_templates'] +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +language = 'ja' + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +html_theme = 'sphinx_rtd_theme' +html_static_path = ['_static'] diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..e17effa --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,20 @@ +.. プロジェクト名を設定してください documentation master file, created by + sphinx-quickstart on Fri Dec 5 01:02:07 2025. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +プロジェクト名を設定してください documentation +============================================== + +Add your content using ``reStructuredText`` syntax. See the +`reStructuredText `_ +documentation for details. + + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + modules + + diff --git a/docs/main.rst b/docs/main.rst new file mode 100644 index 0000000..7874bd9 --- /dev/null +++ b/docs/main.rst @@ -0,0 +1,7 @@ +main module +=========== + +.. automodule:: main + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..8f56beb --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/modules.rst b/docs/modules.rst new file mode 100644 index 0000000..320ed56 --- /dev/null +++ b/docs/modules.rst @@ -0,0 +1,9 @@ +src +=== + +.. toctree:: + :maxdepth: 4 + + main + utils + diff --git a/docs/utils.rst b/docs/utils.rst new file mode 100644 index 0000000..296e746 --- /dev/null +++ b/docs/utils.rst @@ -0,0 +1,29 @@ +utils package +============= + +Submodules +---------- + +utils.custom\_logger module +--------------------------- + +.. automodule:: utils.custom_logger + :members: + :show-inheritance: + :undoc-members: + +utils.singleton module +---------------------- + +.. automodule:: utils.singleton + :members: + :show-inheritance: + :undoc-members: + +Module contents +--------------- + +.. automodule:: utils + :members: + :show-inheritance: + :undoc-members: diff --git a/readme/python_init.md b/readme/python_init.md index 87a75e1..1423d5d 100644 --- a/readme/python_init.md +++ b/readme/python_init.md @@ -79,4 +79,29 @@ ruff check . --output-format json --output-file ruff-report.json `--output-file`は`github`など様々な形式が指定できます -コメントを生成する方法を検討が必要 \ No newline at end of file +コメントを生成する方法を検討が必要 + +## Doc + +初期設定を行う + +```sh +mkdir docs +cd docs +sphinx-quickstart +``` + +rstファイルを自動生成する + +```sh +cd docs +sphinx-apidoc -o . ../src +``` + + +Docsを自動生成する + +``` +cd docs +make html +``` diff --git a/requirements-dev.txt b/requirements-dev.txt index 18ae90a..2c47131 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,3 +4,8 @@ pytest-cov coverage-badge # Linting tool ruff==0.14.7 +# Docs +sphinx +sphinx-rtd-theme +autodoc + diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 From 27c1c461e8863adaef75edab15b7fa8498aaa02c Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 02:16:37 +0900 Subject: [PATCH 21/49] =?UTF-8?q?terraform=E3=81=AE=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E3=81=AA=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme/deploy.md | 11 +++++++++ terraform/af.tf | 5 ++++ terraform/platform.tf | 10 ++++++++ terraform/provider.tf | 5 ++++ terraform/run_job.tf | 30 +++++++++++++++++++++++ terraform/sa.tf | 14 +++++++++++ terraform/sample.tfvars | 10 ++++++++ terraform/variables.tf | 53 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 readme/deploy.md create mode 100644 terraform/af.tf create mode 100644 terraform/platform.tf create mode 100644 terraform/provider.tf create mode 100644 terraform/run_job.tf create mode 100644 terraform/sa.tf create mode 100644 terraform/sample.tfvars create mode 100644 terraform/variables.tf diff --git a/readme/deploy.md b/readme/deploy.md new file mode 100644 index 0000000..0fc0931 --- /dev/null +++ b/readme/deploy.md @@ -0,0 +1,11 @@ +# デプロイの方法について + +## 環境について + +* terraform +* google cloud + * Cloud Run Job + + + + diff --git a/terraform/af.tf b/terraform/af.tf new file mode 100644 index 0000000..fd4fcf8 --- /dev/null +++ b/terraform/af.tf @@ -0,0 +1,5 @@ +resource "google_artifact_registry_repository" "repo" { + location = var.region + repository_id = "cicd-repo-${var.env_name}" + format = "DOCKER" +} \ No newline at end of file diff --git a/terraform/platform.tf b/terraform/platform.tf new file mode 100644 index 0000000..17fcd21 --- /dev/null +++ b/terraform/platform.tf @@ -0,0 +1,10 @@ +# Google CloudのAPIを有効化 + +resource "google_project_service" "services" { + for_each = toset([ + "run.googleapis.com", + "artifactregistry.googleapis.com", + "cloudbuild.googleapis.com", + ]) + service = each.key +} \ No newline at end of file diff --git a/terraform/provider.tf b/terraform/provider.tf new file mode 100644 index 0000000..fdeb7be --- /dev/null +++ b/terraform/provider.tf @@ -0,0 +1,5 @@ +# Google Providerの設定 +provider "google" { + project = var.project_id + region = var.region +} \ No newline at end of file diff --git a/terraform/run_job.tf b/terraform/run_job.tf new file mode 100644 index 0000000..9d5b1d1 --- /dev/null +++ b/terraform/run_job.tf @@ -0,0 +1,30 @@ +# Cloud Run Jobのリソース +resource "google_cloud_run_job" "job" { + name = "${var.job_name}-${var.env_name}-job" + location = var.region + + # サービスアカウントを指定 + service_account = google_service_account.job_sa.email + + template { + template { + containers { + image = var.container_image + + resources { + limits = { + cpu = var.cpu_limit + memory = var.memory_limit + } + } + + # env { + # 必要に応じ環境変数を設定 + # name = "ENV_VAR_NAME" + # } + } + timeout_seconds = var.timeout_seconds + } + } +} + diff --git a/terraform/sa.tf b/terraform/sa.tf new file mode 100644 index 0000000..798cc12 --- /dev/null +++ b/terraform/sa.tf @@ -0,0 +1,14 @@ +resource "google_service_account" "job_sa" { + account_id = "sa-${var.job_name}-${var.env_name}" + display_name = "Cloud Run Job Service Account for ${var.job_name} in ${var.env_name} environment" + description = "Cloud Run Job Service Account for ${var.job_name} in ${var.env_name} environment" + project = var.project_id +} + +# IAM role assignment +# Cloud Run Job実行に必要な権限を付与 +resource "google_project_iam_member" "run_job_invoker" { + project = var.project_id + role = "roles/run.invoker" + member = "serviceAccount:${google_service_account.job_sa.email}" +} \ No newline at end of file diff --git a/terraform/sample.tfvars b/terraform/sample.tfvars new file mode 100644 index 0000000..dd838f6 --- /dev/null +++ b/terraform/sample.tfvars @@ -0,0 +1,10 @@ +project_id = "プロジェクトIDを指定してください" +region = "asia-northeast1" +env_name = "dev" + +job_name = "ジョブ名を指定してください" +# コンテナイメージ(CI/CDから渡される想定) + +cpu_limit = "1" +memory_limit = "512Mi" +timeout_seconds = 900 diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..26937d7 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,53 @@ +# GCPプロジェクトIDとリージョン、環境名、ジョブ名の変数定義 +variable "project_id" { + description = "The ID of the GCP project to deploy resources into." + type = string +} +variable "region" { + description = "The GCP region to deploy resources into." + type = string + default = "asia-northeast1" # 東京 +} + +variable "env_name" { + description = "The environment name for the deployment." + type = string + default = "dev" + validation { + condition = contains(["dev", "staging", "prd"], var.env_name) + error_message = "env_name must be one of: dev, staging, prd." + } +} + +variable "job_name" { + description = "The name of the Cloud Run Job." + type = string + default = "get-news-ai" +} + + +# コンテナイメージの変数定義(CI/CDから渡される想定) +variable "container_image" { + description = "The container image for the Cloud Run Job." + type = string +} + + +# Cloud Run Jobの設定変数 +variable "cpu_limit" { + description = "The CPU limit for the Cloud Run Job container." + type = string + default = "1" +} + +variable "memory_limit" { + description = "The memory limit for the Cloud Run Job container." + type = string + default = "512Mi" +} + +variable "timeout_seconds" { + description = "The timeout for the Cloud Run Job." + type = number + default = 300 +} \ No newline at end of file From 6cb9f550f70a2df543754eb4476f82bb2bf087ec Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 10:49:58 +0900 Subject: [PATCH 22/49] =?UTF-8?q?=E5=88=9D=E5=9B=9ECloudRunJob=E8=B5=B7?= =?UTF-8?q?=E5=8B=95=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 19 +++++++++ readme/components_design/AGENT.md | 61 +++++++++++++++++++++++++++++ readme/deploy.md | 65 +++++++++++++++++++++++++++++++ terraform/run_job.tf | 21 +++++----- terraform/sample.tfvars | 2 +- terraform/variables.tf | 13 ++++--- 6 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 Dockerfile create mode 100644 readme/components_design/AGENT.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..91fd4eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.12-slim + +# 必要なパッケージをインストール +RUN apt-get update && apt-get install -y \ + curl + +# pythonパッケージのインストール +COPY requirements.txt . +RUN pip install --upgrade pip +RUN pip install --no-cache-dir -r requirements.txt + +# 作業ディレクトリを設定 +WORKDIR /app + +# アプリケーションコードをコピー +COPY ./src /app + +# コンテナ起動時に実行されるコマンドを指定 +CMD ["python", "main.py"] \ No newline at end of file diff --git a/readme/components_design/AGENT.md b/readme/components_design/AGENT.md new file mode 100644 index 0000000..fe51108 --- /dev/null +++ b/readme/components_design/AGENT.md @@ -0,0 +1,61 @@ + +# エージェントへの目的 + +`terraform`フォルダを確認して`readme/components_design`フォルダに +.drawioファイル(XML形式)で作成してください + +## 役割 + +あなたはシステム構成を考える専門家です。 + +## 規約 + +【要件】 + +* 出力ファイルは`system_components.drawio`としてください。 +* リクエストに指定がない場合は環境変数は`_dev.tfvars`を優先してください。 +* サービスアカウントやロールなどは記載しなくて良い。 +* **重要**terraformに存在しないコンポーネントは使用しないこと +* ユーザーが利用するコンポーネント図と、開発者が利用するコンポーネント図は分離してください + + +【レイアウト要件】 +- Region、VPCを大きな枠で表現 + - Region: 最も外側の枠として配置 + - VPC: Regionの内側に配置 +- 接続線が重ならないよう、コンポーネントを階段状に配置 +- 各コンポーネント間の間隔を100px以上確保 +- ユーザーはVPCの外側に配置(インターネット経由でアクセスする想定) +- コンポーネントは左から右に向かってデータフローを表現(User → Frontend → Backend → Database) + +【スタイル要件】 + +**枠のスタイル:** +- VPC: `fillColor=#D5E8D4;strokeColor=#82b366;dashed=1;verticalAlign=top;fontStyle=1;fontSize=14` +- Region: `fillColor=#E1F5FE;strokeColor=#01579B;dashed=1;verticalAlign=top;fontStyle=1;fontSize=14` +- 枠のラベルは左上に配置(`align=left;spacingLeft=10;spacingTop=5`) + +**接続線:** +- 双方向通信: `endArrow=classic;startArrow=classic;strokeWidth=2` +- 単方向通信: `endArrow=classic;strokeWidth=2` +- HTTPSアクセス: `strokeColor=#4285F4`(青) +- データベース接続: `strokeColor=#DB4437`(赤) +- ストレージアクセス: `strokeColor=#34A853`(緑) +- 接続線にラベルを付ける(例: "HTTPS", "API", "SQL") + +【座標とサイズの目安】 +- Region枠: 幅800-1000px、高さ500-700px +- VPC枠: Region内部で余白50px程度、幅700-900px、高さ400-600px +- コンポーネントアイコン: 78x78 または 80x80 +- コンポーネント間の横間隔: 150-200px +- コンポーネント間の縦間隔: 100-150px + + +**アイコン:** + +- ユーザー/クライアントアイコン + - `shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.user`(共通で使用可能) + +- コンポーネントのアイコンについて以下のマップを参考にしてください + - 以下のアイコンを使用する場合、必ず対応する mxCell テンプレートを使用すること。 + - id / x / y / width / height / parent などは適宜書き換えて構いません。 diff --git a/readme/deploy.md b/readme/deploy.md index 0fc0931..fea0c58 100644 --- a/readme/deploy.md +++ b/readme/deploy.md @@ -1,11 +1,76 @@ # デプロイの方法について +## インストール方法 + +MACの場合 + +```sh +brew tap hashicorp/tap +brew install hashicorp/tap/terraform +# 確認 +terraform -version +``` + + ## 環境について * terraform * google cloud * Cloud Run Job +## 実行する方法 + +```sh +# 初期化を実行する +cd terraform +# Terraformの初期化 +terraform init +# アーティファクトやバケットについては先に生成する +terraform apply \ +-var-file=_dev.tfvars \ +-auto-approve \ +-target="google_artifact_registry_repository.repo" +# DockerファイルをビルドしてGARにプッシュする場合 +cd ../ +# 1. Artifact Registryへの認証設定(初回のみ実行) +source deploy.env +gcloud auth configure-docker "${AR_REGION}-docker.pkg.dev" +# arm64 +source deploy.env +gcloud builds submit --tag "${IMAGE_URI}" . +echo "${IMAGE_URI}" + +# デプロイするコンポーネントを確認する +cd terraform +terraform plan \ +-var-file=_dev.tfvars \ +-var="hash_suffix=${HASH_SUFFIX}" + +# デプロイを実行する +terraform apply \ +-var-file=_dev.tfvars \ +-var="hash_suffix=${HASH_SUFFIX}" \ +-auto-approve +``` +ローカルでビルドで試す場合 +```sh +# デフォルトでビルドする場合 +docker build -t cloud-run-job-base . +# arm64でビルドしたい場合 +docker buildx build -platform linux/amd64,linux/arm64 -t cloud-run-job-base . +# Dockerを実行する(1回だけ実行してコンテナインスタンスを削除する場合) +docker run --rm cloud-run-job-base:latest +``` + +### CI/CDでデプロイを実行する場合 + +**Github(Gitea) Acrtionsで実行する場合** + + +**Cloud Buildで実行する場合** + + +### Big Quderyにデータが取得できた場合をトリガーにしてJOBを実行する方法 diff --git a/terraform/run_job.tf b/terraform/run_job.tf index 9d5b1d1..c6be644 100644 --- a/terraform/run_job.tf +++ b/terraform/run_job.tf @@ -1,30 +1,29 @@ # Cloud Run Jobのリソース -resource "google_cloud_run_job" "job" { +# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_job +resource "google_cloud_run_v2_job" "job" { name = "${var.job_name}-${var.env_name}-job" location = var.region - # サービスアカウントを指定 - service_account = google_service_account.job_sa.email template { template { - containers { - image = var.container_image + # サービスアカウントを指定 + service_account = google_service_account.job_sa.email + containers { + image = "${var.region}-docker.pkg.dev/${var.project_id}/cicd-repo-${var.env_name}/run-job-${var.job_name}-image:${var.hash_suffix}" resources { limits = { cpu = var.cpu_limit memory = var.memory_limit } } - - # env { - # 必要に応じ環境変数を設定 - # name = "ENV_VAR_NAME" - # } } - timeout_seconds = var.timeout_seconds + + timeout = var.timeout + } } + } diff --git a/terraform/sample.tfvars b/terraform/sample.tfvars index dd838f6..b094684 100644 --- a/terraform/sample.tfvars +++ b/terraform/sample.tfvars @@ -7,4 +7,4 @@ job_name = "ジョブ名を指定してください" cpu_limit = "1" memory_limit = "512Mi" -timeout_seconds = 900 +timeout = "1800s" diff --git a/terraform/variables.tf b/terraform/variables.tf index 26937d7..c298ec6 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -26,10 +26,11 @@ variable "job_name" { } -# コンテナイメージの変数定義(CI/CDから渡される想定) -variable "container_image" { +# コンテナイメージのハッシュ値変数定義(CI/CDから渡される想定) +variable "hash_suffix" { description = "The container image for the Cloud Run Job." type = string + default = null } @@ -46,8 +47,8 @@ variable "memory_limit" { default = "512Mi" } -variable "timeout_seconds" { - description = "The timeout for the Cloud Run Job." - type = number - default = 300 +variable "timeout" { + description = "The task timeout in seconds for the Cloud Run Job." + type = string + default = "1800s" } \ No newline at end of file From 6d141ca131d2a2194ef43a87398c5af827fad8d5 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:40:52 +0900 Subject: [PATCH 23/49] =?UTF-8?q?CD=E3=82=BD=E3=83=BC=E3=82=B9=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0(gcp=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 38 +++++++++++++++++++++++++++++ .gitignore | 7 ++++++ scripts/deploy/init_terraform.sh | 19 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 .github/workflows/deploy_to_gcp.yml create mode 100644 scripts/deploy/init_terraform.sh diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml new file mode 100644 index 0000000..051d0dd --- /dev/null +++ b/.github/workflows/deploy_to_gcp.yml @@ -0,0 +1,38 @@ +name: Gitea Deploy to GCP + +on: + workflow_dispatch: + + pull_request: + branches: + - deploy-prd + - deploy-dev + paths: + - 'src/**' + - 'terraform/**' + +jobs: + deploy: + name: Deploy to GCP + runs-on: gcloud-tf + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check Deploy Tools + run: | + gcloud --version + terraform --version + + + # - name: Set up Cloud SDK + # uses: google-github-actions/setup-gcloud@v1 + # with: + # project_id: ${{ secrets.GCP_PROJECT_ID }} + # service_account_key: ${{ secrets.GCP_SA_KEY }} + # export_default_credentials: true + + # - name: Run deployment script + # run: | + # chmod +x ./deploy.sh + # ./deploy.sh diff --git a/.gitignore b/.gitignore index 00b439c..2015f53 100644 --- a/.gitignore +++ b/.gitignore @@ -172,3 +172,10 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +# terraform.tfstate files +_*.tfvars +.terraform/ +.terraform.lock.hcl +*.tfstate +*.tfstate.backup +*deploy.env \ No newline at end of file diff --git a/scripts/deploy/init_terraform.sh b/scripts/deploy/init_terraform.sh new file mode 100644 index 0000000..7e57d64 --- /dev/null +++ b/scripts/deploy/init_terraform.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Safe mode(when error,kill script) +set -euo pipefail + +TF_DIR=${TF_DIR:-terraform} + +# GCS S3などで保存する +TF_STATE_BUCKET=${TF_STATE_BUCKET:-cicd-tfstate-bucket} +ENV=${ENV:-dev} +REPO_NAME=${REPO_NAME:-unknown} + +cd "$TF_DIR" + +# --- terraform init 実行 --- +terraform init \ + -backend-config="bucket=${TF_STATE_BUCKET}" \ + -backend-config="prefix=${REPO_NAME}/${ENV}" \ + \ No newline at end of file From e8eba504b3512bd9096fcce6fe2337a867425dbe Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:44:49 +0900 Subject: [PATCH 24/49] =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 051d0dd..09c4950 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -2,14 +2,10 @@ name: Gitea Deploy to GCP on: workflow_dispatch: - pull_request: branches: - deploy-prd - deploy-dev - paths: - - 'src/**' - - 'terraform/**' jobs: deploy: From ed2de27a8fea9f270fe903e7ce1aa7cc10b6c833 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:48:35 +0900 Subject: [PATCH 25/49] =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 09c4950..ecec961 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -8,17 +8,23 @@ on: - deploy-dev jobs: - deploy: - name: Deploy to GCP - runs-on: gcloud-tf + checkout: + name: Checkout code + runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 + gcp-deploy: + name: Deploy to GCP + runs-on: gcloud + steps: - name: Check Deploy Tools + runs-on: ubuntu-latest run: | gcloud --version terraform --version + ls -la # - name: Set up Cloud SDK From a73c68580093dadad02e9e2c87a8c08506a486fe Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:51:39 +0900 Subject: [PATCH 26/49] Test --- .github/workflows/deploy_to_gcp.yml | 34 +++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index ecec961..2cf6a5a 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -8,23 +8,29 @@ on: - deploy-dev jobs: - checkout: - name: Checkout code - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 - gcp-deploy: name: Deploy to GCP - runs-on: gcloud + runs-on: gcloud-tf steps: - - name: Check Deploy Tools - runs-on: ubuntu-latest - run: | - gcloud --version - terraform --version - ls -la + - name: Checkout code + uses: actions/checkout@v4 + + # checkout: + # name: Checkout code + # runs-on: ubuntu-latest + # steps: + # - name: Checkout code + # uses: actions/checkout@v3 + + # gcp-deploy: + # name: Deploy to GCP + # runs-on: gcloud-tf + # steps: + # - name: Check Deploy Tools + # run: | + # gcloud --version + # terraform --version + # ls -la # - name: Set up Cloud SDK From 09e6feb2651debd5f1275a1155a4adc6917c1a0e Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:53:48 +0900 Subject: [PATCH 27/49] test --- .github/workflows/deploy_to_gcp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 2cf6a5a..2fe96f5 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -13,7 +13,7 @@ jobs: runs-on: gcloud-tf steps: - name: Checkout code - uses: actions/checkout@v4 + uses: actions/checkout@v3 # checkout: # name: Checkout code From 251da802c4f9c3ae876173d19b9939d04de8bd17 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 20:56:03 +0900 Subject: [PATCH 28/49] Test --- .github/workflows/deploy_to_gcp.yml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 2fe96f5..c9723f9 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -8,12 +8,25 @@ on: - deploy-dev jobs: - gcp-deploy: + checkout: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + gcp-deploy: name: Deploy to GCP runs-on: gcloud-tf steps: - - name: Checkout code - uses: actions/checkout@v3 + - name: Check Deploy Tools + run: | + ls -la + gcloud --version + terraform --version + + + # steps: + # - name: Checkout code + # uses: actions/checkout@v3 # checkout: # name: Checkout code From 017b88d0c9d79a9a19799c3b22e743e962137c44 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:07:27 +0900 Subject: [PATCH 29/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index c9723f9..87939a7 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -8,15 +8,13 @@ on: - deploy-dev jobs: - checkout: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 gcp-deploy: name: Deploy to GCP runs-on: gcloud-tf steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Check Deploy Tools run: | ls -la From dd65fdd8b56c9b06dc6a02951a1a20526c420f6d Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:18:08 +0900 Subject: [PATCH 30/49] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 87939a7..e04d516 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -11,6 +11,9 @@ jobs: gcp-deploy: name: Deploy to GCP runs-on: gcloud-tf + env: + GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} + GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} steps: - name: Checkout code uses: actions/checkout@v3 @@ -18,9 +21,25 @@ jobs: - name: Check Deploy Tools run: | ls -la + echo "Checking gcloud and terraform versions..." gcloud --version terraform --version + - name: Check Gcloud auth + run: | + echo "HOME: ${HOME}" + printf '%s' "$GCP_SA_KEY" > $HOME/sa.json + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + + gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS" + gcloud config set project "$GCP_PROJECT_ID" + + echo "Check gcloud" + gcloud config list + gcloud --version + + + # steps: # - name: Checkout code From fa09f7199f38ac8aade350870cc3dd6aaeb1f292 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:33:53 +0900 Subject: [PATCH 31/49] =?UTF-8?q?init=20terraform=E3=82=92=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 5 +++++ scripts/deploy/init_terraform.sh | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index e04d516..04fc377 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -14,6 +14,7 @@ jobs: env: GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} + REPO_NAME: ${{ github.repository }} steps: - name: Checkout code uses: actions/checkout@v3 @@ -38,6 +39,10 @@ jobs: gcloud config list gcloud --version + - name: Exec Terraform init shell + run: | + ./scripts/terraform/init.sh + diff --git a/scripts/deploy/init_terraform.sh b/scripts/deploy/init_terraform.sh index 7e57d64..94e1a39 100644 --- a/scripts/deploy/init_terraform.sh +++ b/scripts/deploy/init_terraform.sh @@ -6,13 +6,16 @@ set -euo pipefail TF_DIR=${TF_DIR:-terraform} # GCS S3などで保存する -TF_STATE_BUCKET=${TF_STATE_BUCKET:-cicd-tfstate-bucket} +TF_STATE_BUCKET=${TF_STATE_BUCKET:-cicd-tfstate-bucket-20250906} ENV=${ENV:-dev} REPO_NAME=${REPO_NAME:-unknown} cd "$TF_DIR" +echo "$REPO_NAME" -# --- terraform init 実行 --- +ecgi + +# # --- terraform init 実行 --- terraform init \ -backend-config="bucket=${TF_STATE_BUCKET}" \ -backend-config="prefix=${REPO_NAME}/${ENV}" \ From 5811b76fb56f557c41cd6a51db66bd72fd82db7b Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:34:46 +0900 Subject: [PATCH 32/49] test --- .github/workflows/deploy_to_gcp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 04fc377..75c8e8d 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -41,7 +41,7 @@ jobs: - name: Exec Terraform init shell run: | - ./scripts/terraform/init.sh + ./scripts/deploy/init_terraform.sh From d5869b639c3d5b6faf6da47258291c9b70d93c4e Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:36:49 +0900 Subject: [PATCH 33/49] =?UTF-8?q?=E6=A8=A9=E9=99=90=E5=8F=8A=E3=81=B3?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy/init_terraform.sh | 1 - 1 file changed, 1 deletion(-) mode change 100644 => 100755 scripts/deploy/init_terraform.sh diff --git a/scripts/deploy/init_terraform.sh b/scripts/deploy/init_terraform.sh old mode 100644 new mode 100755 index 94e1a39..bad7202 --- a/scripts/deploy/init_terraform.sh +++ b/scripts/deploy/init_terraform.sh @@ -13,7 +13,6 @@ REPO_NAME=${REPO_NAME:-unknown} cd "$TF_DIR" echo "$REPO_NAME" -ecgi # # --- terraform init 実行 --- terraform init \ From 19d4d881717aea7984dfe7c0422c327b03bdb8fc Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:43:27 +0900 Subject: [PATCH 34/49] =?UTF-8?q?=E3=83=90=E3=83=83=E3=82=AF=E3=82=A8?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- terraform/provider.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/terraform/provider.tf b/terraform/provider.tf index fdeb7be..218805c 100644 --- a/terraform/provider.tf +++ b/terraform/provider.tf @@ -1,3 +1,7 @@ +terraform { + backend "gcs" {} +} + # Google Providerの設定 provider "google" { project = var.project_id From e7595d1365f1a52d1d13b67d206593f85f90b5a2 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 21:50:48 +0900 Subject: [PATCH 35/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 75c8e8d..6bd2a46 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -41,6 +41,11 @@ jobs: - name: Exec Terraform init shell run: | + ls -la ${HOME} + echo "$REPO_NAME" + echo "HOME: ${HOME}" + printf '%s' "$GCP_SA_KEY" > $HOME/sa.json + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/init_terraform.sh From 1be5bd53883544e757754162548c46ced0983aa9 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:11:26 +0900 Subject: [PATCH 36/49] =?UTF-8?q?=E3=83=87=E3=83=97=E3=83=AD=E3=82=A4?= =?UTF-8?q?=E3=82=BD=E3=83=BC=E3=82=B9=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 10 ++++---- scripts/deploy/build_image_to_gar.sh | 35 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 scripts/deploy/build_image_to_gar.sh diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 6bd2a46..7126f44 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -15,6 +15,7 @@ jobs: GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} REPO_NAME: ${{ github.repository }} + HASH_SUFFIX: ${{ github.sha }} steps: - name: Checkout code uses: actions/checkout@v3 @@ -41,13 +42,12 @@ jobs: - name: Exec Terraform init shell run: | - ls -la ${HOME} - echo "$REPO_NAME" - echo "HOME: ${HOME}" - printf '%s' "$GCP_SA_KEY" > $HOME/sa.json export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/init_terraform.sh - + - name: Exec Container Image Push to Artifact Registry + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/build_image_to_gar.sh diff --git a/scripts/deploy/build_image_to_gar.sh b/scripts/deploy/build_image_to_gar.sh new file mode 100644 index 0000000..376c717 --- /dev/null +++ b/scripts/deploy/build_image_to_gar.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# Google Container RegistryへDockerイメージをビルドしてプッシュするスクリプト +set -euo pipefail + +# 環境変数の設定 +REGION=${REGION:-asia-northeast1} +ENV=${ENV:-dev} +JOB_NAME=${JOB_NAME} +AR_REPO_NAME="cicd-repo-${ENV}" +HASH_SUFFIX=${HASH_SUFFIX:-$(date +%s)} + +# IMAGE_URIの設定 +# ローカル実行時は epoch 秒で自動採番。 +IMAGE_URI="${REGION}-docker.pkg.dev/${GCP_PROJECT_ID}/${AR_REPO_NAME}/run-job-${JOB_NAME}-image:${HASH_SUFFIX}" + +echo "REGION : ${REGION}" +echo "ENV : ${ENV}" +echo "JOB_NAME : ${JOB_NAME}" +echo "HASH_SUFFIX : ${HASH_SUFFIX}" +echo "IMAGE_URI : ${IMAGE_URI}" + +# Artifact Registry への認証設定 +gcloud auth configure-docker "${REGION}-docker.pkg.dev" + +# GARへDockerイメージをビルドしてプッシュ +gcloud builds submit --tag "${IMAGE_URI}" . + + + + + + + + + From 62f9f9c36160be6f8ca1cdae0cc927940d9e6397 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:13:05 +0900 Subject: [PATCH 37/49] =?UTF-8?q?=E3=83=93=E3=83=AB=E3=83=89=E7=94=A8?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy/build_image_to_gar.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/deploy/build_image_to_gar.sh diff --git a/scripts/deploy/build_image_to_gar.sh b/scripts/deploy/build_image_to_gar.sh old mode 100644 new mode 100755 From 57fea674e7dd82ef6b191b35371969ede5b8cf5c Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:14:59 +0900 Subject: [PATCH 38/49] test --- .github/workflows/deploy_to_gcp.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 7126f44..d488c6a 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -16,6 +16,7 @@ jobs: GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} REPO_NAME: ${{ github.repository }} HASH_SUFFIX: ${{ github.sha }} + JOB_NAME: ${{ vars.JOB_NAME }} steps: - name: Checkout code uses: actions/checkout@v3 From ab32a9e825b950d06d21dfe1919888f5033ea862 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:32:50 +0900 Subject: [PATCH 39/49] =?UTF-8?q?=E3=83=97=E3=83=A9=E3=83=B3=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 9 ++++++++- scripts/deploy/build_image_to_gar.sh | 3 +-- scripts/deploy/plan_terraform.sh | 24 ++++++++++++++++++++++++ terraform/dev.tfvars | 12 ++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100755 scripts/deploy/plan_terraform.sh create mode 100644 terraform/dev.tfvars diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index d488c6a..680d73f 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -17,6 +17,7 @@ jobs: REPO_NAME: ${{ github.repository }} HASH_SUFFIX: ${{ github.sha }} JOB_NAME: ${{ vars.JOB_NAME }} + HASH_SUFFIX: ${{ github.sha }} steps: - name: Checkout code uses: actions/checkout@v3 @@ -27,7 +28,7 @@ jobs: echo "Checking gcloud and terraform versions..." gcloud --version terraform --version - + - name: Check Gcloud auth run: | echo "HOME: ${HOME}" @@ -45,11 +46,17 @@ jobs: run: | export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/init_terraform.sh + - name: Exec Container Image Push to Artifact Registry run: | export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/build_image_to_gar.sh + - name: Exec Terraform init shell + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/plan_terraform.sh + # steps: diff --git a/scripts/deploy/build_image_to_gar.sh b/scripts/deploy/build_image_to_gar.sh index 376c717..0a94374 100755 --- a/scripts/deploy/build_image_to_gar.sh +++ b/scripts/deploy/build_image_to_gar.sh @@ -7,7 +7,7 @@ REGION=${REGION:-asia-northeast1} ENV=${ENV:-dev} JOB_NAME=${JOB_NAME} AR_REPO_NAME="cicd-repo-${ENV}" -HASH_SUFFIX=${HASH_SUFFIX:-$(date +%s)} +HASH_SUFFIX=${HASH_SUFFIX} # IMAGE_URIの設定 # ローカル実行時は epoch 秒で自動採番。 @@ -32,4 +32,3 @@ gcloud builds submit --tag "${IMAGE_URI}" . - diff --git a/scripts/deploy/plan_terraform.sh b/scripts/deploy/plan_terraform.sh new file mode 100755 index 0000000..0fba502 --- /dev/null +++ b/scripts/deploy/plan_terraform.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Safe mode(when error,kill script) +set -euo pipefail + +# 変数の設定({HOME}/hash.txt からハッシュ値を取得) +HASH_SUFFIX=${HASH_SUFFIX} + +TF_DIR=${TF_DIR:-terraform} +ENV=${ENV:-dev} + +cd "$TF_DIR" + +if [ -f "${ENV}.tfvars" ]; then + terraform plan \ + -out=tfplan \ + -var-file="${ENV}.tfvars" \ + -var="hash_suffix=${HASH_SUFFIX}" + +else + # error raise + echo "ERROR: ${ENV}.tfvars not found in $(pwd)" >&2 + exit 1 +fi \ No newline at end of file diff --git a/terraform/dev.tfvars b/terraform/dev.tfvars new file mode 100644 index 0000000..72900c2 --- /dev/null +++ b/terraform/dev.tfvars @@ -0,0 +1,12 @@ +project_id = "gcp-devel-project" +region = "asia-northeast1" +env_name = "dev" + +job_name = "base" +# コンテナイメージ(CI/CDから渡される想定) + +cpu_limit = "1" +memory_limit = "512Mi" +timeout = "1800s" + + From ebc4c1ba43cf931162d9216fc1e5592d6129bfa0 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:34:51 +0900 Subject: [PATCH 40/49] =?UTF-8?q?=E3=83=97=E3=83=A9=E3=83=B3=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 680d73f..2391908 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -52,7 +52,7 @@ jobs: export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/build_image_to_gar.sh - - name: Exec Terraform init shell + - name: Exec Terraform plan shell run: | export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/plan_terraform.sh From 1fcbf0e33ab712b03c99da862e8a886b141754d2 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:42:01 +0900 Subject: [PATCH 41/49] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy/build_image_to_gar.sh | 11 +++-------- scripts/deploy/plan_terraform.sh | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/scripts/deploy/build_image_to_gar.sh b/scripts/deploy/build_image_to_gar.sh index 0a94374..133bac5 100755 --- a/scripts/deploy/build_image_to_gar.sh +++ b/scripts/deploy/build_image_to_gar.sh @@ -9,10 +9,13 @@ JOB_NAME=${JOB_NAME} AR_REPO_NAME="cicd-repo-${ENV}" HASH_SUFFIX=${HASH_SUFFIX} + # IMAGE_URIの設定 # ローカル実行時は epoch 秒で自動採番。 IMAGE_URI="${REGION}-docker.pkg.dev/${GCP_PROJECT_ID}/${AR_REPO_NAME}/run-job-${JOB_NAME}-image:${HASH_SUFFIX}" + + echo "REGION : ${REGION}" echo "ENV : ${ENV}" echo "JOB_NAME : ${JOB_NAME}" @@ -24,11 +27,3 @@ gcloud auth configure-docker "${REGION}-docker.pkg.dev" # GARへDockerイメージをビルドしてプッシュ gcloud builds submit --tag "${IMAGE_URI}" . - - - - - - - - diff --git a/scripts/deploy/plan_terraform.sh b/scripts/deploy/plan_terraform.sh index 0fba502..3f87562 100755 --- a/scripts/deploy/plan_terraform.sh +++ b/scripts/deploy/plan_terraform.sh @@ -4,10 +4,9 @@ set -euo pipefail # 変数の設定({HOME}/hash.txt からハッシュ値を取得) -HASH_SUFFIX=${HASH_SUFFIX} - TF_DIR=${TF_DIR:-terraform} ENV=${ENV:-dev} +HASH_SUFFIX=${HASH_SUFFIX} cd "$TF_DIR" From e3c965849150f42d2b41ecfd29b5c5a1652882ad Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:43:39 +0900 Subject: [PATCH 42/49] test --- .github/workflows/deploy_to_gcp.yml | 105 ++++++++++------------------ 1 file changed, 35 insertions(+), 70 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 2391908..acffd04 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -6,7 +6,6 @@ on: branches: - deploy-prd - deploy-dev - jobs: gcp-deploy: name: Deploy to GCP @@ -22,73 +21,39 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - - name: Check Deploy Tools - run: | - ls -la - echo "Checking gcloud and terraform versions..." - gcloud --version - terraform --version - - - name: Check Gcloud auth - run: | - echo "HOME: ${HOME}" - printf '%s' "$GCP_SA_KEY" > $HOME/sa.json - export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - - gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS" - gcloud config set project "$GCP_PROJECT_ID" - - echo "Check gcloud" - gcloud config list - gcloud --version - - - name: Exec Terraform init shell - run: | - export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - ./scripts/deploy/init_terraform.sh - - - name: Exec Container Image Push to Artifact Registry - run: | - export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - ./scripts/deploy/build_image_to_gar.sh - - - name: Exec Terraform plan shell - run: | - export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - ./scripts/deploy/plan_terraform.sh - - - - # steps: - # - name: Checkout code - # uses: actions/checkout@v3 - - # checkout: - # name: Checkout code - # runs-on: ubuntu-latest - # steps: - # - name: Checkout code - # uses: actions/checkout@v3 - - # gcp-deploy: - # name: Deploy to GCP - # runs-on: gcloud-tf - # steps: - # - name: Check Deploy Tools - # run: | - # gcloud --version - # terraform --version - # ls -la - - - # - name: Set up Cloud SDK - # uses: google-github-actions/setup-gcloud@v1 - # with: - # project_id: ${{ secrets.GCP_PROJECT_ID }} - # service_account_key: ${{ secrets.GCP_SA_KEY }} - # export_default_credentials: true - - # - name: Run deployment script + # - name: Check Deploy Tools # run: | - # chmod +x ./deploy.sh - # ./deploy.sh + # ls -la + # echo "Checking gcloud and terraform versions..." + # gcloud --version + # terraform --version + + # - name: Check Gcloud auth + # run: | + # echo "HOME: ${HOME}" + # printf '%s' "$GCP_SA_KEY" > $HOME/sa.json + # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + + # gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS" + # gcloud config set project "$GCP_PROJECT_ID" + + # echo "Check gcloud" + # gcloud config list + # gcloud --version + + # - name: Exec Terraform init shell + # run: | + # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + # ./scripts/deploy/init_terraform.sh + + # - name: Exec Container Image Push to Artifact Registry + # run: | + # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + # ./scripts/deploy/build_image_to_gar.sh + + # - name: Exec Terraform plan shell + # run: | + # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + # ./scripts/deploy/plan_terraform.sh + + From 843375d950eb2cf442ab73e6bcc8227d28f9bf34 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:49:34 +0900 Subject: [PATCH 43/49] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index acffd04..4819fbf 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -1,4 +1,4 @@ -name: Gitea Deploy to GCP +name: Gitea Deploy to GCP AR on: workflow_dispatch: @@ -9,7 +9,7 @@ on: jobs: gcp-deploy: name: Deploy to GCP - runs-on: gcloud-tf + runs-on: ubuntu-latest env: GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} From 2f50cbbd6e4e1e37c804efdee4d7d87a5399b3a3 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:51:30 +0900 Subject: [PATCH 44/49] test --- .github/workflows/{deploy_to_gcp.yml => deploy_to_run_job.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{deploy_to_gcp.yml => deploy_to_run_job.yml} (100%) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_run_job.yml similarity index 100% rename from .github/workflows/deploy_to_gcp.yml rename to .github/workflows/deploy_to_run_job.yml From 4fd5007a3e5b092c12d10927fc0e9f928a4fc222 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:52:20 +0900 Subject: [PATCH 45/49] test --- .github/workflows/deploy_to_run_job.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy_to_run_job.yml b/.github/workflows/deploy_to_run_job.yml index 4819fbf..7537cfe 100644 --- a/.github/workflows/deploy_to_run_job.yml +++ b/.github/workflows/deploy_to_run_job.yml @@ -19,7 +19,9 @@ jobs: HASH_SUFFIX: ${{ github.sha }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v3 + + # - name: Check Deploy Tools # run: | From 77214306bff1aba88bdd259b355d6334320f8d84 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:52:38 +0900 Subject: [PATCH 46/49] test --- .github/workflows/deploy_to_run_job.yml | 38 ------------------------- 1 file changed, 38 deletions(-) diff --git a/.github/workflows/deploy_to_run_job.yml b/.github/workflows/deploy_to_run_job.yml index 7537cfe..6ae31e5 100644 --- a/.github/workflows/deploy_to_run_job.yml +++ b/.github/workflows/deploy_to_run_job.yml @@ -16,46 +16,8 @@ jobs: REPO_NAME: ${{ github.repository }} HASH_SUFFIX: ${{ github.sha }} JOB_NAME: ${{ vars.JOB_NAME }} - HASH_SUFFIX: ${{ github.sha }} steps: - name: Checkout code uses: actions/checkout@v3 - - # - name: Check Deploy Tools - # run: | - # ls -la - # echo "Checking gcloud and terraform versions..." - # gcloud --version - # terraform --version - - # - name: Check Gcloud auth - # run: | - # echo "HOME: ${HOME}" - # printf '%s' "$GCP_SA_KEY" > $HOME/sa.json - # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - - # gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS" - # gcloud config set project "$GCP_PROJECT_ID" - - # echo "Check gcloud" - # gcloud config list - # gcloud --version - - # - name: Exec Terraform init shell - # run: | - # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - # ./scripts/deploy/init_terraform.sh - - # - name: Exec Container Image Push to Artifact Registry - # run: | - # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - # ./scripts/deploy/build_image_to_gar.sh - - # - name: Exec Terraform plan shell - # run: | - # export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" - # ./scripts/deploy/plan_terraform.sh - - From 2edea8d13c3ec63609dbebb9ac521549e6982499 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 22:53:37 +0900 Subject: [PATCH 47/49] test --- .github/workflows/deploy_to_gcp.yml | 59 +++++++++++++++++++++++++ .github/workflows/deploy_to_run_job.yml | 23 ---------- 2 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/deploy_to_gcp.yml delete mode 100644 .github/workflows/deploy_to_run_job.yml diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml new file mode 100644 index 0000000..0920e60 --- /dev/null +++ b/.github/workflows/deploy_to_gcp.yml @@ -0,0 +1,59 @@ +name: Gitea Deploy to GCP + +on: + workflow_dispatch: + pull_request: + branches: + - deploy-prd + - deploy-dev + +jobs: + gcp-deploy: + name: Deploy to GCP + runs-on: gcloud-tf + env: + GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} + GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} + REPO_NAME: ${{ github.repository }} + HASH_SUFFIX: ${{ github.sha }} + JOB_NAME: ${{ vars.JOB_NAME }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check Deploy Tools + run: | + ls -la + echo "Checking gcloud and terraform versions..." + gcloud --version + terraform --version + + - name: Check Gcloud auth + run: | + echo "HOME: ${HOME}" + printf '%s' "$GCP_SA_KEY" > $HOME/sa.json + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + + gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS" + gcloud config set project "$GCP_PROJECT_ID" + + echo "Check gcloud" + gcloud config list + gcloud --version + + - name: Exec Terraform init shell + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/init_terraform.sh + + - name: Exec Container Image Push to Artifact Registry + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/build_image_to_gar.sh + + - name: Exec Terraform plan shell + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/plan_terraform.sh + + diff --git a/.github/workflows/deploy_to_run_job.yml b/.github/workflows/deploy_to_run_job.yml deleted file mode 100644 index 6ae31e5..0000000 --- a/.github/workflows/deploy_to_run_job.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Gitea Deploy to GCP AR - -on: - workflow_dispatch: - pull_request: - branches: - - deploy-prd - - deploy-dev -jobs: - gcp-deploy: - name: Deploy to GCP - runs-on: ubuntu-latest - env: - GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} - GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} - REPO_NAME: ${{ github.repository }} - HASH_SUFFIX: ${{ github.sha }} - JOB_NAME: ${{ vars.JOB_NAME }} - steps: - - name: Checkout code - uses: actions/checkout@v3 - - From 3c1c8159d426f7bdda4ed6df57824fea2b3e3062 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 23:01:26 +0900 Subject: [PATCH 48/49] =?UTF-8?q?=E3=83=87=E3=83=97=E3=83=AD=E3=82=A4?= =?UTF-8?q?=E3=82=BD=E3=83=BC=E3=82=B9=E3=82=92=E6=95=B4=E5=82=99=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 9 +++++++++ scripts/deploy/applay_terraform.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 scripts/deploy/applay_terraform.sh diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index 0920e60..c955af1 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -56,4 +56,13 @@ jobs: export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" ./scripts/deploy/plan_terraform.sh + - name: Exec Terraform apply shell + run: | + export GOOGLE_APPLICATION_CREDENTIALS="$HOME/sa.json" + ./scripts/deploy/apply_terraform.sh + + - name: Clean up Gcloud auth file + run: | + rm -f $HOME/sa.json + echo "Cleaned up Gcloud auth file." diff --git a/scripts/deploy/applay_terraform.sh b/scripts/deploy/applay_terraform.sh new file mode 100755 index 0000000..5334ae0 --- /dev/null +++ b/scripts/deploy/applay_terraform.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Safe mode(when error,kill script) +set -euo pipefail + +# 変数の設定({HOME}/hash.txt からハッシュ値を取得) +TF_DIR=${TF_DIR:-terraform} +ENV=${ENV:-dev} + +cd "$TF_DIR" + +# --- デプロイ条件 --- +if [[ "${BRANCH_NAME:-}" =~ ^.*deploy$ ]]; then + echo "Start terraform apply (ENV=${ENV}, DIR=${TF_DIR}) ..." +else + echo "Skip terraform apply (branch=${BRANCH_NAME:-})" + exit 0 +fi + +# --- plan 結果があるか確認 --- +if [[ ! -f tfplan ]]; then + echo "ERROR: tfplan not found in $(pwd). Run plan step first." >&2 + exit 1 +fi + +terraform apply -auto-approve tfplan From c8ea858abe76571107fe7eba5d18f105f9009628 Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Fri, 5 Dec 2025 23:02:47 +0900 Subject: [PATCH 49/49] =?UTF-8?q?=E3=83=96=E3=83=A9=E3=83=B3=E3=83=81?= =?UTF-8?q?=E3=83=8D=E3=83=BC=E3=83=A0=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy_to_gcp.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy_to_gcp.yml b/.github/workflows/deploy_to_gcp.yml index c955af1..bad81f7 100644 --- a/.github/workflows/deploy_to_gcp.yml +++ b/.github/workflows/deploy_to_gcp.yml @@ -17,6 +17,7 @@ jobs: REPO_NAME: ${{ github.repository }} HASH_SUFFIX: ${{ github.sha }} JOB_NAME: ${{ vars.JOB_NAME }} + BRANCH_NAME: ${{ github.ref_name }} steps: - name: Checkout code uses: actions/checkout@v3