From d8ec02250c3dff51e7d69ea2f390b5edce4c3819 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 11:59:43 +0800 Subject: [PATCH 01/16] =?UTF-8?q?feat(codex):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=AE=89=E8=A3=9D=20Codex=20CLI=E3=80=81=E5=AF=AB=E5=85=A5=20O?= =?UTF-8?q?Auth=20token=20=E4=B8=A6=E5=9F=B7=E8=A1=8C=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=A9=9E=E7=9A=84=20composite=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- action.yml | 55 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/action.yml b/action.yml index 646c3c6..457fc47 100644 --- a/action.yml +++ b/action.yml @@ -1,33 +1,48 @@ -name: 'Composite Action Template' -description: 'Composite Action 範本' +name: 'Codex CLI' +description: 'Codex CLI 工具' author: 'Jeffery' inputs: - text: - description: '輸入的文字' + prompt: + description: '傳給 Codex CLI 的提示詞' required: false - default: 'Hello, World!' + default: "請自我介紹" + oauth: + description: 'base64 編碼的 Codex OAuth token 檔案內容' + required: true outputs: text: description: '輸出的文字' - value: ${{ steps.change.outputs.text }} + value: ${{ steps.codex.outputs.text }} runs: using: 'composite' steps: - - name: 交換 - id: change + - name: 安裝工具 env: - GITEA_SERVER_URL: ${{ gitea.server_url }} - GITEA_REPOSITORY: ${{ gitea.repository }} - GITEA_TOKEN: ${{ gitea.token }} - TEXT: ${{ inputs.text }} + OAUTH: ${{ inputs.oauth }} run: | - echo "Gitea Server Url: $GITEA_SERVER_URL" + npm install -g @openai/codex - echo "Gitea Repository: $GITEA_REPOSITORY" + oauth_file="$HOME/.codex/auth.json" + install -d -m 700 "$(dirname "$oauth_file")" + printf '%s' "$OAUTH" | base64 -d > "$oauth_file" + chmod 600 "$oauth_file" + shell: bash + - name: 執行工具 + id: codex + env: + MODEL: ${{ vars.CODEX_MODEL }} + PROMPT: ${{ inputs.prompt }} + run: | + if [ -z "$MODEL" ]; then + echo 'CODEX_MODEL repository variable is required.' >&2 + exit 1 + fi - echo "Gitea Token: $GITEA_TOKEN" - - echo "Text: $TEXT" - - echo "text=$TEXT" >> "$GITHUB_OUTPUT" - shell: bash \ No newline at end of file + text="$(codex exec --model "$MODEL" "$PROMPT")" + printf '%s\n' "$text" + { + echo 'text<> "$GITHUB_OUTPUT" + shell: bash -- 2.53.0 From dc9ab5f2357748b4509c7432c1c0bd2f6a190d88 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 11:59:43 +0800 Subject: [PATCH 02/16] =?UTF-8?q?chore(ci):=20=E6=94=B9=E7=94=A8=20release?= =?UTF-8?q?-tag-version=20=E8=A8=88=E7=AE=97=E7=89=88=E6=9C=AC=E4=B8=A6?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=20codex=20=E6=B8=AC=E8=A9=A6=E4=BB=BB?= =?UTF-8?q?=E5=8B=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 15c4240..a351fbc 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -1,19 +1,35 @@ name: CI + on: pull_request: branches-ignore: - master types: [opened, synchronize] + jobs: - ai-code-review: - name: AI Code Review + release-tag-version: + name: Release Tag Version runs-on: ubuntu - permissions: - contents: write - pull-requests: write - issues: write + outputs: + version: ${{ steps.release-tag-version.outputs.version }} steps: - - name: AI 程式碼審查 by OpenCode - uses: https://gitea.jsc.idv.tw/composite-actions/opencode-code-review@${{ vars.ACTION_OPENCODE_CODE_REVIEW_VERSION }} + - name: 計算版本號 + id: release-tag-version + uses: https://gitea.jsc.idv.tw/composite-actions/release-tag-version@${{ vars.ACTION_RELEASE_TAG_VERSION }} with: - token: ${{ secrets.TOKEN }} + is_beta: 'true' + + codex: + name: Codex + runs-on: ubuntu + needs: release-tag-version + steps: + - name: 測試工具 + id: codex + uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} + with: + oauth: ${{ secrets.CODEX_OAUTH }} + prompt: "請告訴我目前登入的帳號,只要電子郵件不要其他任何資訊" + - name: 檢查輸出 + if: ${{ steps.codex.outputs.text != vars.CODEX_EMAIL }} + run: exit 1 -- 2.53.0 From 5a21b30e9c9a7506622ba8a262db6f426ec5c456 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 11:59:43 +0800 Subject: [PATCH 03/16] =?UTF-8?q?docs(README):=20=E6=96=B0=E5=A2=9E=20Code?= =?UTF-8?q?x=20CLI=20composite=20action=20=E4=BD=BF=E7=94=A8=E8=AA=AA?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f59026 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# Codex CLI Composite Action + +此 repository 提供一個 Gitea/GitHub Actions composite action,用於安裝 Codex CLI、寫入 OAuth token、執行指定提示詞,並將 CLI 輸出寫入 action output。 + +## 專案列表 + +### 專案描述 + +| 專案名稱 | 專案描述 | +| --- | --- | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 提供 Codex CLI composite action,讓 workflow 可透過 `oauth` 與 `prompt` 輸入執行 CLI,並從 repository variable 讀取模型名稱後取得文字輸出。 | + +### 參考專案 + +| 專案名稱 | 參考專案列表 | +| --- | --- | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 無 | + +### NuGet 套件 + +| 專案名稱 | NuGet 套件列表 | +| --- | --- | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 無 | + +## 功能列表 + +此 repository 未包含可列入 README 的 public method、public constructor、public extension method 或 public operator。 + +## 使用範例 + +### 在 workflow 中呼叫 Codex CLI + +以下範例示範在 workflow step 中呼叫此 composite action,傳入 OAuth token 與提示詞,並在後續步驟讀取 `text` output。模型名稱由 action 直接讀取 `vars.CODEX_MODEL`。 + +```yaml +- name: 執行 Codex CLI + id: codex + uses: https://gitea.jsc.idv.tw/composite-actions/codex@v1 + with: + oauth: ${{ secrets.CODEX_OAUTH }} + prompt: "請自我介紹" + +- name: 使用輸出文字 + run: printf '%s\n' '${{ steps.codex.outputs.text }}' +``` + +前置條件: + +- `secrets.CODEX_OAUTH` 必須是 Codex OAuth token 檔案內容的 base64 字串。 +- `vars.CODEX_MODEL` 必須是 Codex CLI 可用的模型名稱。 +- runner 必須可透過 npm 安裝 `@openai/codex`。 + +預期結果: + +- action 會安裝 `codex` CLI。 +- action 會將 OAuth token 寫入 `$HOME/.codex/auth.json` 並設定檔案權限為 `600`。 +- action 會執行 `codex exec --model "$MODEL" "$PROMPT"`,並把 stdout 寫入 `steps.codex.outputs.text`。 -- 2.53.0 From e4fc162b55f09a87b5d329dcaeca77d19f2f2e6d Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 12:17:04 +0800 Subject: [PATCH 04/16] =?UTF-8?q?fix(codex):=20=E7=82=BA=20codex=20exec=20?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=20--skip-git-repo-check=20=E4=BB=A5=E7=95=A5?= =?UTF-8?q?=E9=81=8E=20git=20repo=20=E6=AA=A2=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 457fc47..4a75eaf 100644 --- a/action.yml +++ b/action.yml @@ -38,7 +38,7 @@ runs: exit 1 fi - text="$(codex exec --model "$MODEL" "$PROMPT")" + text="$(codex exec --skip-git-repo-check --model "$MODEL" "$PROMPT")" printf '%s\n' "$text" { echo 'text< Date: Mon, 29 Jun 2026 12:28:43 +0800 Subject: [PATCH 05/16] =?UTF-8?q?fix(codex):=20=E4=BF=AE=E6=AD=A3=20runner?= =?UTF-8?q?=20=E6=B2=99=E7=AE=B1=E4=B8=A6=E6=94=B9=E4=BB=A5=20OAuth=20id?= =?UTF-8?q?=5Ftoken=20=E7=9A=84=20email=20=E9=A9=97=E8=AD=89=E7=99=BB?= =?UTF-8?q?=E5=85=A5=E5=B8=B3=E8=99=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit codex exec 加上 -s danger-full-access 避免 runner 容器內 bubblewrap 因 無法建立 user namespace 而失敗;action 新增 email output(解析 auth.json id_token 的 email claim),CI 改用 steps.codex.outputs.email 比對 vars.CODEX_EMAIL,不再依賴模型自行回報帳號。 --- .gitea/workflows/ci.yaml | 6 +++--- action.yml | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index a351fbc..373af0d 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -29,7 +29,7 @@ jobs: uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} with: oauth: ${{ secrets.CODEX_OAUTH }} - prompt: "請告訴我目前登入的帳號,只要電子郵件不要其他任何資訊" - - name: 檢查輸出 - if: ${{ steps.codex.outputs.text != vars.CODEX_EMAIL }} + prompt: "請自我介紹" + - name: 檢查登入帳號 + if: ${{ steps.codex.outputs.email != vars.CODEX_EMAIL }} run: exit 1 diff --git a/action.yml b/action.yml index 4a75eaf..b18fc6d 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ outputs: text: description: '輸出的文字' value: ${{ steps.codex.outputs.text }} + email: + description: '目前登入帳號的電子郵件(取自 OAuth id_token)' + value: ${{ steps.codex.outputs.email }} runs: using: 'composite' steps: @@ -38,11 +41,23 @@ runs: exit 1 fi - text="$(codex exec --skip-git-repo-check --model "$MODEL" "$PROMPT")" + text="$(codex exec --skip-git-repo-check -s danger-full-access --model "$MODEL" "$PROMPT")" printf '%s\n' "$text" + + # 從 OAuth id_token(JWT)解出目前登入帳號的 email;正規表示式對冒號後空白容錯 + auth_file="$HOME/.codex/auth.json" + id_token="$(grep -oE -m1 '"id_token"[[:space:]]*:[[:space:]]*"[^"]*"' "$auth_file" | sed -E 's/.*:[[:space:]]*"//; s/"$//' || true)" + payload="$(printf '%s' "$id_token" | cut -d. -f2 | tr '_-' '/+')" + case $((${#payload} % 4)) in + 2) payload="${payload}==" ;; + 3) payload="${payload}=" ;; + esac + email="$(printf '%s' "$payload" | base64 -d 2>/dev/null | grep -oE -m1 '"email"[[:space:]]*:[[:space:]]*"[^"]*"' | sed -E 's/.*:[[:space:]]*"//; s/"$//' || true)" + { echo 'text<> "$GITHUB_OUTPUT" shell: bash -- 2.53.0 From 7ee6921c638fa95597c5f2e97c4076e350fa6e06 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 12:35:35 +0800 Subject: [PATCH 06/16] =?UTF-8?q?fix(codex):=20=E6=94=B9=E4=BB=A5=20codex?= =?UTF-8?q?=20exec=20=E7=9C=9F=E5=AF=A6=E5=BE=80=E8=BF=94=E9=A9=97?= =?UTF-8?q?=E8=AD=89=E7=99=BB=E5=85=A5=EF=BC=8C=E7=A7=BB=E9=99=A4=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=A7=A3=E6=9E=90=20OAuth=20token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不再解析 auth.json 的 id_token 來取得 email(只證明 secret 解得開、 無法確保工具真的登入成功)。改為透過 codex exec 實際呼叫模型:登入無效 或 token 過期時 codex exec 會直接失敗,CI 再以 steps.codex.outputs.text 是否為空做最終把關。action 移除 email output,保留 -s danger-full-access 以避免 runner 容器內 bubblewrap 沙箱失敗。 --- .gitea/workflows/ci.yaml | 4 ++-- action.yml | 15 --------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 373af0d..6b8066b 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -30,6 +30,6 @@ jobs: with: oauth: ${{ secrets.CODEX_OAUTH }} prompt: "請自我介紹" - - name: 檢查登入帳號 - if: ${{ steps.codex.outputs.email != vars.CODEX_EMAIL }} + - name: 檢查登入 + if: ${{ steps.codex.outputs.text == '' }} run: exit 1 diff --git a/action.yml b/action.yml index b18fc6d..78beab2 100644 --- a/action.yml +++ b/action.yml @@ -13,9 +13,6 @@ outputs: text: description: '輸出的文字' value: ${{ steps.codex.outputs.text }} - email: - description: '目前登入帳號的電子郵件(取自 OAuth id_token)' - value: ${{ steps.codex.outputs.email }} runs: using: 'composite' steps: @@ -43,21 +40,9 @@ runs: text="$(codex exec --skip-git-repo-check -s danger-full-access --model "$MODEL" "$PROMPT")" printf '%s\n' "$text" - - # 從 OAuth id_token(JWT)解出目前登入帳號的 email;正規表示式對冒號後空白容錯 - auth_file="$HOME/.codex/auth.json" - id_token="$(grep -oE -m1 '"id_token"[[:space:]]*:[[:space:]]*"[^"]*"' "$auth_file" | sed -E 's/.*:[[:space:]]*"//; s/"$//' || true)" - payload="$(printf '%s' "$id_token" | cut -d. -f2 | tr '_-' '/+')" - case $((${#payload} % 4)) in - 2) payload="${payload}==" ;; - 3) payload="${payload}=" ;; - esac - email="$(printf '%s' "$payload" | base64 -d 2>/dev/null | grep -oE -m1 '"email"[[:space:]]*:[[:space:]]*"[^"]*"' | sed -E 's/.*:[[:space:]]*"//; s/"$//' || true)" - { echo 'text<> "$GITHUB_OUTPUT" shell: bash -- 2.53.0 From 6e771e2e5233b6e53a5e226f844e7d7df61ba186 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 12:48:48 +0800 Subject: [PATCH 07/16] =?UTF-8?q?fix(codex):=20=E6=94=B9=E7=94=A8=20codex?= =?UTF-8?q?=20app-server=20account/read=20=E5=8F=96=E5=BE=97=E7=99=BB?= =?UTF-8?q?=E5=85=A5=E5=B8=B3=E8=99=9F=20email=20=E9=A9=97=E8=AD=89?= =?UTF-8?q?=E8=BA=AB=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不解析本地 OAuth token,改透過 codex 自身的 app-server JSON-RPC account/read(即 TUI /status 的 Account 來源)取得登入帳號 email, 新增 email output;CI 以 steps.codex.outputs.email 比對 vars.CODEX_EMAIL 確認是正確帳號。codex exec 仍保留以確保 token 實際有效。 --- .gitea/workflows/ci.yaml | 4 ++-- action.yml | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 6b8066b..373af0d 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -30,6 +30,6 @@ jobs: with: oauth: ${{ secrets.CODEX_OAUTH }} prompt: "請自我介紹" - - name: 檢查登入 - if: ${{ steps.codex.outputs.text == '' }} + - name: 檢查登入帳號 + if: ${{ steps.codex.outputs.email != vars.CODEX_EMAIL }} run: exit 1 diff --git a/action.yml b/action.yml index 78beab2..51f1987 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ outputs: text: description: '輸出的文字' value: ${{ steps.codex.outputs.text }} + email: + description: '目前登入帳號的電子郵件(由 codex app-server account/read 取得)' + value: ${{ steps.account.outputs.email }} runs: using: 'composite' steps: @@ -46,3 +49,45 @@ runs: echo 'CODEX_OUTPUT' } >> "$GITHUB_OUTPUT" shell: bash + - name: 取得登入帳號 + id: account + run: | + cat > "$RUNNER_TEMP/codex_account.py" <<'PY' + import json, subprocess, time + + p = subprocess.Popen( + ["codex", "app-server"], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, text=True, bufsize=1, + ) + + def send(obj): + p.stdin.write(json.dumps(obj) + "\n") + p.stdin.flush() + + send({"jsonrpc": "2.0", "id": 1, "method": "initialize", + "params": {"clientInfo": {"name": "ci", "version": "1.0"}}}) + send({"jsonrpc": "2.0", "method": "initialized", "params": {}}) + send({"jsonrpc": "2.0", "id": 2, "method": "account/read", "params": {}}) + + email = "" + deadline = time.time() + 25 + while time.time() < deadline: + line = p.stdout.readline() + if not line: + break + try: + msg = json.loads(line) + except Exception: + continue + if msg.get("id") == 2: + account = (msg.get("result") or {}).get("account") or {} + email = account.get("email") or "" + break + + p.terminate() + print(email) + PY + email="$(python3 "$RUNNER_TEMP/codex_account.py")" + printf 'email=%s\n' "$email" >> "$GITHUB_OUTPUT" + shell: bash -- 2.53.0 From 672900762ec8927d25631c2578af1c56fba27e17 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 12:52:01 +0800 Subject: [PATCH 08/16] =?UTF-8?q?refactor(codex):=20=E5=B0=87=20app-server?= =?UTF-8?q?=20=E5=8F=96=E5=B8=B3=E8=99=9F=E9=82=8F=E8=BC=AF=E6=8A=BD?= =?UTF-8?q?=E5=88=B0=20app/codex=5Faccount.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把原本內嵌在 action.yml heredoc 的 python 抽成獨立檔 app/codex_account.py, action 改以 $GITHUB_ACTION_PATH/app/codex_account.py 呼叫,便於閱讀與維護。 --- action.yml | 38 +---------------------------- app/codex_account.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 app/codex_account.py diff --git a/action.yml b/action.yml index 51f1987..0a325e9 100644 --- a/action.yml +++ b/action.yml @@ -52,42 +52,6 @@ runs: - name: 取得登入帳號 id: account run: | - cat > "$RUNNER_TEMP/codex_account.py" <<'PY' - import json, subprocess, time - - p = subprocess.Popen( - ["codex", "app-server"], - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.DEVNULL, text=True, bufsize=1, - ) - - def send(obj): - p.stdin.write(json.dumps(obj) + "\n") - p.stdin.flush() - - send({"jsonrpc": "2.0", "id": 1, "method": "initialize", - "params": {"clientInfo": {"name": "ci", "version": "1.0"}}}) - send({"jsonrpc": "2.0", "method": "initialized", "params": {}}) - send({"jsonrpc": "2.0", "id": 2, "method": "account/read", "params": {}}) - - email = "" - deadline = time.time() + 25 - while time.time() < deadline: - line = p.stdout.readline() - if not line: - break - try: - msg = json.loads(line) - except Exception: - continue - if msg.get("id") == 2: - account = (msg.get("result") or {}).get("account") or {} - email = account.get("email") or "" - break - - p.terminate() - print(email) - PY - email="$(python3 "$RUNNER_TEMP/codex_account.py")" + email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" printf 'email=%s\n' "$email" >> "$GITHUB_OUTPUT" shell: bash diff --git a/app/codex_account.py b/app/codex_account.py new file mode 100644 index 0000000..10f5648 --- /dev/null +++ b/app/codex_account.py @@ -0,0 +1,57 @@ +"""透過 codex app-server 的 JSON-RPC account/read 取得目前登入帳號的 email。 + +這是 TUI `/status` Account 欄位的程式化來源,不解析本地 OAuth token, +而是由 codex 自身回報登入帳號。將 email 印到 stdout(取不到時印空字串)。 +""" + +import json +import subprocess +import time + + +def read_account_email(timeout_seconds: float = 25.0) -> str: + process = subprocess.Popen( + ["codex", "app-server"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + text=True, + bufsize=1, + ) + + def send(obj): + process.stdin.write(json.dumps(obj) + "\n") + process.stdin.flush() + + send({ + "jsonrpc": "2.0", + "id": 1, + "method": "initialize", + "params": {"clientInfo": {"name": "ci", "version": "1.0"}}, + }) + send({"jsonrpc": "2.0", "method": "initialized", "params": {}}) + send({"jsonrpc": "2.0", "id": 2, "method": "account/read", "params": {}}) + + email = "" + deadline = time.time() + timeout_seconds + try: + while time.time() < deadline: + line = process.stdout.readline() + if not line: + break + try: + message = json.loads(line) + except ValueError: + continue + if message.get("id") == 2: + account = (message.get("result") or {}).get("account") or {} + email = account.get("email") or "" + break + finally: + process.terminate() + + return email + + +if __name__ == "__main__": + print(read_account_email()) -- 2.53.0 From d05610d4e56bb4cca1f86977a66b1e8e36036faf Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 12:56:00 +0800 Subject: [PATCH 09/16] =?UTF-8?q?refactor(codex):=20oauth=20=E6=94=B9?= =?UTF-8?q?=E5=BE=9E=20secrets=20=E5=8F=96=E5=BE=97=E3=80=81=E5=B8=B3?= =?UTF-8?q?=E8=99=9F=E9=A9=97=E8=AD=89=E6=94=B6=E9=80=B2=20action=20?= =?UTF-8?q?=E5=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit oauth 不再經由 inputs 傳入,action 直接讀 secrets.CODEX_OAUTH(與 vars.CODEX_MODEL 相同模式)。登入帳號驗證移入 action:讀 vars.CODEX_EMAIL 與 app-server 取得的 email 比對,不再輸出 email 到 outputs、不在 CI 端比對。 --- .gitea/workflows/ci.yaml | 4 ---- action.yml | 24 ++++++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 373af0d..40ec896 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -28,8 +28,4 @@ jobs: id: codex uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} with: - oauth: ${{ secrets.CODEX_OAUTH }} prompt: "請自我介紹" - - name: 檢查登入帳號 - if: ${{ steps.codex.outputs.email != vars.CODEX_EMAIL }} - run: exit 1 diff --git a/action.yml b/action.yml index 0a325e9..2886409 100644 --- a/action.yml +++ b/action.yml @@ -6,22 +6,16 @@ inputs: description: '傳給 Codex CLI 的提示詞' required: false default: "請自我介紹" - oauth: - description: 'base64 編碼的 Codex OAuth token 檔案內容' - required: true outputs: text: description: '輸出的文字' value: ${{ steps.codex.outputs.text }} - email: - description: '目前登入帳號的電子郵件(由 codex app-server account/read 取得)' - value: ${{ steps.account.outputs.email }} runs: using: 'composite' steps: - name: 安裝工具 env: - OAUTH: ${{ inputs.oauth }} + OAUTH: ${{ secrets.CODEX_OAUTH }} run: | npm install -g @openai/codex @@ -49,9 +43,19 @@ runs: echo 'CODEX_OUTPUT' } >> "$GITHUB_OUTPUT" shell: bash - - name: 取得登入帳號 - id: account + - name: 驗證登入帳號 + env: + EXPECTED_EMAIL: ${{ vars.CODEX_EMAIL }} run: | + if [ -z "$EXPECTED_EMAIL" ]; then + echo 'CODEX_EMAIL repository variable is required.' >&2 + exit 1 + fi + email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" - printf 'email=%s\n' "$email" >> "$GITHUB_OUTPUT" + if [ "$email" != "$EXPECTED_EMAIL" ]; then + echo '登入帳號與 CODEX_EMAIL 不符' >&2 + exit 1 + fi + echo '登入帳號驗證通過' shell: bash -- 2.53.0 From 08890b5b9d99e8517bdcef86b234dfe21828a19e Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 13:35:42 +0800 Subject: [PATCH 10/16] =?UTF-8?q?fix(codex):=20=E6=94=B9=E7=94=B1=20job=20?= =?UTF-8?q?=E5=B1=A4=20env=20=E5=BE=9E=20secrets=20=E6=B3=A8=E5=85=A5=20CO?= =?UTF-8?q?DEX=5FOAUTH?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit composite action 內無法解析 secrets context(Gitea 與 GitHub 皆然, vars 可、secrets 不可),導致 OAUTH 為空。改由呼叫端 workflow 在 job 層 以 env: CODEX_OAUTH: ${{ secrets.CODEX_OAUTH }} 注入,action 從繼承的 環境變數讀取並加空值把關;secret 仍源自 secrets、不經 inputs 傳遞。 --- .gitea/workflows/ci.yaml | 2 ++ action.yml | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 40ec896..3db0222 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -23,6 +23,8 @@ jobs: name: Codex runs-on: ubuntu needs: release-tag-version + env: + CODEX_OAUTH: ${{ secrets.CODEX_OAUTH }} steps: - name: 測試工具 id: codex diff --git a/action.yml b/action.yml index 2886409..80abb0a 100644 --- a/action.yml +++ b/action.yml @@ -14,14 +14,17 @@ runs: using: 'composite' steps: - name: 安裝工具 - env: - OAUTH: ${{ secrets.CODEX_OAUTH }} run: | + if [ -z "$CODEX_OAUTH" ]; then + echo 'CODEX_OAUTH environment variable is required (set it from secrets.CODEX_OAUTH in the caller workflow).' >&2 + exit 1 + fi + npm install -g @openai/codex oauth_file="$HOME/.codex/auth.json" install -d -m 700 "$(dirname "$oauth_file")" - printf '%s' "$OAUTH" | base64 -d > "$oauth_file" + printf '%s' "$CODEX_OAUTH" | base64 -d > "$oauth_file" chmod 600 "$oauth_file" shell: bash - name: 執行工具 -- 2.53.0 From a309afdf58bfb02a259a3468b3446e7f6858050d Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 13:42:36 +0800 Subject: [PATCH 11/16] =?UTF-8?q?fix(codex):=20oauth=20=E6=94=B9=E5=9B=9E?= =?UTF-8?q?=E7=B6=93=20inputs.oauth=20=E5=82=B3=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit composite action 讀不到 secrets context,改回由呼叫端以 with: oauth: ${{ secrets.CODEX_OAUTH }} 經 inputs.oauth 傳入;action 從 inputs.oauth 讀取並保留空值把關,移除 job 層 CODEX_OAUTH env。 --- .gitea/workflows/ci.yaml | 3 +-- action.yml | 11 ++++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 3db0222..d8d33d5 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -23,11 +23,10 @@ jobs: name: Codex runs-on: ubuntu needs: release-tag-version - env: - CODEX_OAUTH: ${{ secrets.CODEX_OAUTH }} steps: - name: 測試工具 id: codex uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} with: + oauth: ${{ secrets.CODEX_OAUTH }} prompt: "請自我介紹" diff --git a/action.yml b/action.yml index 80abb0a..25df1ec 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,9 @@ inputs: description: '傳給 Codex CLI 的提示詞' required: false default: "請自我介紹" + oauth: + description: 'base64 編碼的 Codex OAuth token 檔案內容' + required: true outputs: text: description: '輸出的文字' @@ -14,9 +17,11 @@ runs: using: 'composite' steps: - name: 安裝工具 + env: + OAUTH: ${{ inputs.oauth }} run: | - if [ -z "$CODEX_OAUTH" ]; then - echo 'CODEX_OAUTH environment variable is required (set it from secrets.CODEX_OAUTH in the caller workflow).' >&2 + if [ -z "$OAUTH" ]; then + echo 'oauth input (secrets.CODEX_OAUTH) is required and must not be empty.' >&2 exit 1 fi @@ -24,7 +29,7 @@ runs: oauth_file="$HOME/.codex/auth.json" install -d -m 700 "$(dirname "$oauth_file")" - printf '%s' "$CODEX_OAUTH" | base64 -d > "$oauth_file" + printf '%s' "$OAUTH" | base64 -d > "$oauth_file" chmod 600 "$oauth_file" shell: bash - name: 執行工具 -- 2.53.0 From 4077596fccc5237b4904b59e2db2adc213033c6d Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 05:58:47 +0000 Subject: [PATCH 12/16] refactor(codex): move email validation into main execution step and remove redundant check --- action.yml | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/action.yml b/action.yml index 25df1ec..f09fc6d 100644 --- a/action.yml +++ b/action.yml @@ -36,13 +36,22 @@ runs: id: codex env: MODEL: ${{ vars.CODEX_MODEL }} + EXPECTED_EMAIL: ${{ vars.CODEX_EMAIL }} PROMPT: ${{ inputs.prompt }} run: | if [ -z "$MODEL" ]; then echo 'CODEX_MODEL repository variable is required.' >&2 exit 1 fi - + if [ -z "$EXPECTED_EMAIL" ]; then + echo 'CODEX_EMAIL repository variable is required.' >&2 + exit 1 + fi + if [ -z "$PROMPT" ]; then + email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" + echo "text=$email" >> "$GITHUB_OUTPUT" + exit 0 + fi text="$(codex exec --skip-git-repo-check -s danger-full-access --model "$MODEL" "$PROMPT")" printf '%s\n' "$text" { @@ -51,19 +60,6 @@ runs: echo 'CODEX_OUTPUT' } >> "$GITHUB_OUTPUT" shell: bash - - name: 驗證登入帳號 - env: - EXPECTED_EMAIL: ${{ vars.CODEX_EMAIL }} - run: | - if [ -z "$EXPECTED_EMAIL" ]; then - echo 'CODEX_EMAIL repository variable is required.' >&2 - exit 1 - fi - - email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" - if [ "$email" != "$EXPECTED_EMAIL" ]; then - echo '登入帳號與 CODEX_EMAIL 不符' >&2 - exit 1 - fi - echo '登入帳號驗證通過' - shell: bash + - name: 檢查輸出 + if: ${{ steps.codex.outputs.text != vars.ANTIGRAVITY_EMAIL }} + run: exit 1 -- 2.53.0 From 03d082d0f3f074fa210f9ec658a09b88aeaa923c Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 14:09:23 +0800 Subject: [PATCH 13/16] =?UTF-8?q?refactor(codex):=20=E7=A7=BB=E9=99=A4=20a?= =?UTF-8?q?ction=20=E5=85=A7=E7=99=BB=E5=85=A5=E5=B8=B3=E8=99=9F=E9=A9=97?= =?UTF-8?q?=E8=AD=89=EF=BC=8C=E6=94=B9=E7=94=B1=E5=91=BC=E5=8F=AB=E7=AB=AF?= =?UTF-8?q?=E6=AA=A2=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- action.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/action.yml b/action.yml index f09fc6d..4a2de65 100644 --- a/action.yml +++ b/action.yml @@ -36,17 +36,12 @@ runs: id: codex env: MODEL: ${{ vars.CODEX_MODEL }} - EXPECTED_EMAIL: ${{ vars.CODEX_EMAIL }} PROMPT: ${{ inputs.prompt }} run: | if [ -z "$MODEL" ]; then echo 'CODEX_MODEL repository variable is required.' >&2 exit 1 fi - if [ -z "$EXPECTED_EMAIL" ]; then - echo 'CODEX_EMAIL repository variable is required.' >&2 - exit 1 - fi if [ -z "$PROMPT" ]; then email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" echo "text=$email" >> "$GITHUB_OUTPUT" @@ -60,6 +55,3 @@ runs: echo 'CODEX_OUTPUT' } >> "$GITHUB_OUTPUT" shell: bash - - name: 檢查輸出 - if: ${{ steps.codex.outputs.text != vars.ANTIGRAVITY_EMAIL }} - run: exit 1 -- 2.53.0 From 0c8b0ad511aac6c1befbe7682022e1c030dbe002 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 06:11:06 +0000 Subject: [PATCH 14/16] fix(ci): add output check to validate response against expected email --- .gitea/workflows/ci.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index d8d33d5..5c8b55f 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -30,3 +30,6 @@ jobs: with: oauth: ${{ secrets.CODEX_OAUTH }} prompt: "請自我介紹" + - name: 檢查輸出 + if: ${{ steps.codex.outputs.text != vars.CODEX_EMAIL }} + run: exit 1 -- 2.53.0 From b09237647627ec0a76213446985ec510186fcfc1 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 06:12:07 +0000 Subject: [PATCH 15/16] refactor(codex): remove default prompt from inputs in action.yml --- .gitea/workflows/ci.yaml | 1 - action.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 5c8b55f..95aa463 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -29,7 +29,6 @@ jobs: uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} with: oauth: ${{ secrets.CODEX_OAUTH }} - prompt: "請自我介紹" - name: 檢查輸出 if: ${{ steps.codex.outputs.text != vars.CODEX_EMAIL }} run: exit 1 diff --git a/action.yml b/action.yml index 4a2de65..ef029ee 100644 --- a/action.yml +++ b/action.yml @@ -5,7 +5,6 @@ inputs: prompt: description: '傳給 Codex CLI 的提示詞' required: false - default: "請自我介紹" oauth: description: 'base64 編碼的 Codex OAuth token 檔案內容' required: true -- 2.53.0 From 19cb276eea2484f8a0ec41791a7d4e7925bb6271 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 29 Jun 2026 14:55:50 +0800 Subject: [PATCH 16/16] =?UTF-8?q?docs(codex):=20=E8=A3=9C=E9=BD=8A=20read?= =?UTF-8?q?=5Faccount=5Femail=20docstring=20=E8=88=87=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E6=AA=94=E8=A8=BB=E8=A7=A3=E4=B8=A6=E9=87=8D=E5=BB=BA=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/cd.yaml | 23 ++++++++++++++ .gitea/workflows/ci.yaml | 43 +++++++++++++++++++++++++ README.md | 62 +++++++++++++++++++++++------------- action.yml | 68 ++++++++++++++++++++++++++++++++++++++++ app/codex_account.py | 16 ++++++++++ 5 files changed, 190 insertions(+), 22 deletions(-) diff --git a/.gitea/workflows/cd.yaml b/.gitea/workflows/cd.yaml index 30b1952..8730aef 100644 --- a/.gitea/workflows/cd.yaml +++ b/.gitea/workflows/cd.yaml @@ -1,12 +1,35 @@ +# ============================================================================= +# 用途:CD(Continuous Delivery)持續交付 workflow。 +# 當有 commit push 到 master 分支時觸發,呼叫 release-tag-version +# composite action 進行「釋出並標註正式成品版本」(建立 release / 打 tag), +# 將 master 上的成果標記為一個正式可發布的版本。 +# 更新日期:2026/06/29 14:18:26 +# ============================================================================= + +# workflow 顯示名稱,會出現在 Gitea Actions 的 workflow 清單與執行紀錄中,方便辨識。 name: CD +# 觸發條件:監聽 push 事件。 on: push: + # 只在指定分支發生 push 時觸發,避免其他分支誤觸發正式釋出流程。 branches: + # 僅限 master 分支:master 視為正式(穩定)分支,push 進來代表要正式釋出成品版本。 - master +# 此 workflow 包含的 job 清單。 jobs: + # job 識別碼:release-tag-version,負責釋出並標註成品版本。 release-tag-version: + # job 顯示名稱,呈現於 Actions 執行畫面,便於辨識此 job 的職責。 name: Release Tag Version + # 指定執行此 job 的 runner 標籤;使用 ubuntu runner 執行後續步驟。 runs-on: ubuntu + # 此 job 的執行步驟序列。 steps: + # 步驟名稱:釋出並標註成品版本(建立 release / 打版本 tag)。 - name: 釋出並標註成品版本 + # 呼叫外部 composite action「release-tag-version」執行實際的釋出與 tag 標註邏輯。 + # 版本以變數 vars.ACTION_RELEASE_TAG_VERSION 釘選,確保使用指定版本的 action, + # 避免上游 action 變動造成釋出行為不可預期。 + # 副作用:此步驟會在遠端建立 release 或推送版本 tag(正式釋出動作)。 + # 需人工確認:vars.ACTION_RELEASE_TAG_VERSION 變數需在 repo / org 層級正確設定,否則 action 解析會失敗。 uses: https://gitea.jsc.idv.tw/composite-actions/release-tag-version@${{ vars.ACTION_RELEASE_TAG_VERSION }} diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 95aa463..b254a36 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -1,34 +1,77 @@ +# ============================================================================= +# 用途:PR(非 master 分支、opened/synchronize)觸發的 CI。 +# 流程:先用 release-tag-version composite action 算出 beta 版本號,再用該版本號 +# 叫 codex composite action 執行(未帶 prompt,走取登入 email 分支), +# 最後以「檢查輸出」step 驗證輸出的登入帳號 email 是否等於 vars.CODEX_EMAIL。 +# 更新日期:2026/06/29 14:18:26 +# ============================================================================= + +# workflow 顯示名稱,於 Gitea Actions 列表中辨識此工作流程。 name: CI +# 觸發條件設定區塊。 on: + # 針對 pull_request 事件觸發。 pull_request: + # 略過目標分支為 master 的 PR(亦即只在「非 master 分支」的 PR 上跑), + # 避免對 master 的合併 PR 重複執行 CI。 branches-ignore: - master + # 僅在 PR 被開啟(opened)或有新 commit 推入(synchronize)時觸發, + # 其他事件(如 reopened、labeled)不觸發以節省資源。 types: [opened, synchronize] +# 工作(jobs)定義區塊。 jobs: + # 第一個 job:計算 beta 版本號,供後續 codex job 取用。 release-tag-version: + # job 顯示名稱。 name: Release Tag Version + # 指定執行的 runner 標籤(ubuntu)。 runs-on: ubuntu + # 對外輸出值,讓相依此 job 的其他 job 可引用。 outputs: + # 將 release-tag-version step 算出的 version 輸出,鍵名為 version。 version: ${{ steps.release-tag-version.outputs.version }} + # 步驟清單。 steps: + # 呼叫 release-tag-version composite action 計算版本號。 - name: 計算版本號 + # 此 step 的 id,供 outputs 透過 steps.release-tag-version 引用其輸出。 id: release-tag-version + # 使用外部 composite action;版本由 repo 變數 ACTION_RELEASE_TAG_VERSION 決定, + # 便於集中管理所引用 action 的版本。 uses: https://gitea.jsc.idv.tw/composite-actions/release-tag-version@${{ vars.ACTION_RELEASE_TAG_VERSION }} with: + # 指定產生 beta 版本號(而非正式版),用於 CI 測試情境。 is_beta: 'true' + # 第二個 job:用前一 job 算出的版本號叫 codex action,再驗證其輸出。 codex: + # job 顯示名稱。 name: Codex + # 指定執行的 runner 標籤(ubuntu)。 runs-on: ubuntu + # 相依 release-tag-version job,需等其成功後才執行, + # 以取得其輸出的版本號。 needs: release-tag-version + # 步驟清單。 steps: + # 步驟一:呼叫 codex composite action(未帶 prompt,走「取登入 email」分支)。 - name: 測試工具 + # 此 step 的 id,供後續「檢查輸出」step 引用其 outputs.text。 id: codex + # 使用 codex composite action;版本標籤由前一 job 輸出的 version 組成 + # (前綴 v),確保測試所用 codex action 與本次算出的 beta 版本一致。 uses: https://gitea.jsc.idv.tw/composite-actions/codex@v${{ needs.release-tag-version.outputs.version }} with: + # 由 secrets 注入 codex 登入用 OAuth 憑證,避免明文外洩。 + # 注意:未提供 prompt,codex action 會回傳目前登入帳號的 email。 oauth: ${{ secrets.CODEX_OAUTH }} + # 步驟二:驗證登入身分。 - name: 檢查輸出 + # 當 codex 輸出的登入 email 與 repo 變數 CODEX_EMAIL 不符時條件成立, + # 代表登入帳號不是預期帳號。 if: ${{ steps.codex.outputs.text != vars.CODEX_EMAIL }} + # 條件成立即以非零狀態碼退出,使 CI 失敗(擋下非預期登入帳號)。 run: exit 1 diff --git a/README.md b/README.md index 1f59026..43b6816 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Codex CLI Composite Action -此 repository 提供一個 Gitea/GitHub Actions composite action,用於安裝 Codex CLI、寫入 OAuth token、執行指定提示詞,並將 CLI 輸出寫入 action output。 +提供一個 Gitea/GitHub Actions composite action,用於安裝 Codex CLI、寫入 OAuth token、執行指定提示詞,並將 CLI 輸出寫入 action output;另提供透過 `codex app-server` 取得目前登入帳號 email 的工具函式。 + +> 更新時間:2026/06/29 14:18:26 ## 專案列表 @@ -8,50 +10,66 @@ | 專案名稱 | 專案描述 | | --- | --- | -| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 提供 Codex CLI composite action,讓 workflow 可透過 `oauth` 與 `prompt` 輸入執行 CLI,並從 repository variable 讀取模型名稱後取得文字輸出。 | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex/src/branch/develop/) | 提供 Codex CLI composite action(安裝 `@openai/codex`、寫入 OAuth token、執行提示詞並輸出文字),並提供透過 `codex app-server` JSON-RPC `account/read` 取得目前登入帳號 email 的工具函式。 | ### 參考專案 | 專案名稱 | 參考專案列表 | | --- | --- | -| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 無 | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex/src/branch/develop/) | 無 | ### NuGet 套件 | 專案名稱 | NuGet 套件列表 | | --- | --- | -| [codex](https://gitea.jsc.idv.tw/composite-actions/codex) | 無 | +| [codex](https://gitea.jsc.idv.tw/composite-actions/codex/src/branch/develop/) | 無 | ## 功能列表 -此 repository 未包含可列入 README 的 public method、public constructor、public extension method 或 public operator。 +### codex + +| 功能名稱 | 功能描述 | +| --- | --- | +| [codex_account.read_account_email](https://gitea.jsc.idv.tw/composite-actions/codex/src/branch/develop/app/codex_account.py#L12) | [透過 codex app-server 的 JSON-RPC `account/read` 取得目前登入帳號的 email,取不到或逾時回傳空字串。](#codex_accountread_account_email) | ## 使用範例 -### 在 workflow 中呼叫 Codex CLI + +### codex_account.read_account_email -以下範例示範在 workflow step 中呼叫此 composite action,傳入 OAuth token 與提示詞,並在後續步驟讀取 `text` output。模型名稱由 action 直接讀取 `vars.CODEX_MODEL`。 +透過 `codex app-server` 的 JSON-RPC `account/read` 取得目前登入帳號的 email。函式會啟動 `codex app-server` 子行程,依序送出 `initialize` / `initialized` / `account/read` 三筆 JSON-RPC 訊息,讀取回應中 `result.account.email` 作為回傳值;讀到 EOF、JSON 解析失敗或超過 `timeout_seconds` 逾時都會停止,取不到時回傳空字串 `""`,且不論成功或失敗都會在 `finally` 終止子行程。 -```yaml -- name: 執行 Codex CLI - id: codex - uses: https://gitea.jsc.idv.tw/composite-actions/codex@v1 - with: - oauth: ${{ secrets.CODEX_OAUTH }} - prompt: "請自我介紹" +簽名: -- name: 使用輸出文字 - run: printf '%s\n' '${{ steps.codex.outputs.text }}' +```python +def read_account_email(timeout_seconds: float = 25.0) -> str ``` 前置條件: -- `secrets.CODEX_OAUTH` 必須是 Codex OAuth token 檔案內容的 base64 字串。 -- `vars.CODEX_MODEL` 必須是 Codex CLI 可用的模型名稱。 -- runner 必須可透過 npm 安裝 `@openai/codex`。 +- `codex` CLI 已安裝(例如本 action 的安裝步驟已執行)。 +- `$HOME/.codex/auth.json` 已寫入有效的 OAuth token。 + +典型呼叫方式: + +```python +from app.codex_account import read_account_email + +# 取得目前登入帳號 email;可自訂等待 app-server 回應的逾時秒數 +email = read_account_email(timeout_seconds=25.0) +if email: + print(f"目前登入帳號:{email}") +else: + print("取不到登入帳號(未登入或逾時)") +``` 預期結果: -- action 會安裝 `codex` CLI。 -- action 會將 OAuth token 寫入 `$HOME/.codex/auth.json` 並設定檔案權限為 `600`。 -- action 會執行 `codex exec --model "$MODEL" "$PROMPT"`,並把 stdout 寫入 `steps.codex.outputs.text`。 +- 成功時回傳登入帳號的 email 字串。 +- 未登入、回應缺漏或逾時時回傳空字串 `""`。 + +此模組亦可直接以指令執行,會將 email 印到 stdout(取不到時印空字串): + +```bash +python3 app/codex_account.py +``` diff --git a/action.yml b/action.yml index ef029ee..28e24b3 100644 --- a/action.yml +++ b/action.yml @@ -1,56 +1,124 @@ +# ============================================================================= +# 檔案用途:提供 Codex CLI 的 composite action(Gitea/GitHub 複合動作) +# ----------------------------------------------------------------------------- +# 此 action 主要做三件事: +# 1. 安裝 @openai/codex CLI 工具(透過 npm 全域安裝)。 +# 2. 將傳入的 base64 編碼 OAuth token 解碼後寫入 $HOME/.codex/auth.json, +# 讓 codex CLI 取得登入憑證。 +# 3. 執行 inputs.prompt 指定的提示詞,並把 Codex 的輸出寫入 +# steps.codex.outputs.text 供後續 step 使用。 +# - 若未提供 prompt(PROMPT 為空),則改呼叫 app/codex_account.py 取得目前 +# 登入帳號的 email,寫入 text 輸出後結束(用於驗證身分)。 +# 更新日期:2026/06/29 14:18:26 +# ============================================================================= + +# action 名稱(顯示於工作流程記錄中) name: 'Codex CLI' +# action 說明文字 description: 'Codex CLI 工具' +# action 作者 author: 'Jeffery' +# 輸入參數定義區塊 inputs: + # prompt:傳給 Codex CLI 的提示詞 prompt: + # 參數說明 description: '傳給 Codex CLI 的提示詞' + # 非必填;未提供(為空)時會走「取登入 email」分支 required: false + # oauth:base64 編碼的 Codex OAuth token 檔案內容 oauth: + # 參數說明 description: 'base64 編碼的 Codex OAuth token 檔案內容' + # 必填;缺少時安裝步驟會直接報錯退出 required: true +# 輸出參數定義區塊 outputs: + # text:將 Codex 執行結果(或登入 email)對外輸出 text: + # 參數說明 description: '輸出的文字' + # 取自 id 為 codex 的 step 所寫入的 text 輸出 value: ${{ steps.codex.outputs.text }} +# 執行定義區塊 runs: + # 使用 composite(複合)類型,串接多個 shell step using: 'composite' + # 步驟清單 steps: + # 步驟一:安裝 Codex CLI 並寫入 OAuth 憑證 - name: 安裝工具 + # 步驟環境變數 env: + # 將 inputs.oauth 注入為 OAUTH 環境變數(base64 內容) OAUTH: ${{ inputs.oauth }} + # 執行的 shell 指令區塊 run: | + # 檢查 OAUTH 是否為空;為空代表呼叫端未提供 secrets.CODEX_OAUTH if [ -z "$OAUTH" ]; then + # 輸出錯誤訊息至 stderr(>&2) echo 'oauth input (secrets.CODEX_OAUTH) is required and must not be empty.' >&2 + # 以非零狀態碼退出,使工作流程失敗 exit 1 fi + # 全域安裝 Codex CLI 套件,安裝後可直接使用 codex 指令 npm install -g @openai/codex + # 設定 OAuth 憑證檔的路徑(codex CLI 預期讀取此檔) oauth_file="$HOME/.codex/auth.json" + # 建立憑證檔所在目錄;-m 700 限定僅擁有者可進入(保護憑證) install -d -m 700 "$(dirname "$oauth_file")" + # 將 base64 內容解碼(base64 -d)後寫入憑證檔 printf '%s' "$OAUTH" | base64 -d > "$oauth_file" + # 將憑證檔權限設為 600,僅擁有者可讀寫(避免憑證外洩) chmod 600 "$oauth_file" + # 指定以 bash 執行此 run 區塊 shell: bash + # 步驟二:執行 Codex CLI 或取得登入 email - name: 執行工具 + # 步驟識別碼,供 outputs.text 引用其輸出 id: codex + # 步驟環境變數 env: + # MODEL:取自 repository 變數 CODEX_MODEL(指定使用的模型) MODEL: ${{ vars.CODEX_MODEL }} + # PROMPT:取自 inputs.prompt(要執行的提示詞) PROMPT: ${{ inputs.prompt }} + # 執行的 shell 指令區塊 run: | + # 檢查 MODEL 是否為空;未設定 CODEX_MODEL 則無法執行 if [ -z "$MODEL" ]; then + # 輸出錯誤訊息至 stderr echo 'CODEX_MODEL repository variable is required.' >&2 + # 以非零狀態碼退出,使工作流程失敗 exit 1 fi + # 當 PROMPT 為空時,改走「取得登入帳號 email」分支 if [ -z "$PROMPT" ]; then + # 呼叫 codex_account.py 取得目前登入帳號的 email($GITHUB_ACTION_PATH 為 action 根目錄) email="$(python3 "$GITHUB_ACTION_PATH/app/codex_account.py")" + # 將 email 寫入 text 輸出($GITHUB_OUTPUT 為步驟輸出檔) echo "text=$email" >> "$GITHUB_OUTPUT" + # 以 0 正常退出,不再執行 codex exec exit 0 fi + # 執行 Codex CLI: + # --skip-git-repo-check 跳過 git repo 檢查(允許在非 git 目錄執行) + # -s danger-full-access 沙箱模式設為完整存取(具高風險副作用,可讀寫檔案/網路) + # --model "$MODEL" 指定使用的模型 + # "$PROMPT" 為要執行的提示詞;輸出以命令替換存入 text 變數 text="$(codex exec --skip-git-repo-check -s danger-full-access --model "$MODEL" "$PROMPT")" + # 將 Codex 輸出列印到標準輸出(方便在記錄中檢視) printf '%s\n' "$text" + # 以 heredoc 方式將多行輸出寫入 $GITHUB_OUTPUT(避免換行破壞輸出格式) { + # 宣告 text 輸出,使用 CODEX_OUTPUT 作為 heredoc 結束標記 echo 'text<> "$GITHUB_OUTPUT" + # 指定以 bash 執行此 run 區塊 shell: bash diff --git a/app/codex_account.py b/app/codex_account.py index 10f5648..e73ae55 100644 --- a/app/codex_account.py +++ b/app/codex_account.py @@ -10,6 +10,21 @@ import time def read_account_email(timeout_seconds: float = 25.0) -> str: + """透過 codex app-server 的 JSON-RPC account/read 取得目前登入帳號的 email。 + + 啟動 ``codex app-server`` 子行程,依序送出 initialize / initialized / + account/read 三筆 JSON-RPC 訊息,並讀取其回報的登入帳號 email。 + + Args: + timeout_seconds: 等待 app-server 回應的秒數上限,預設 25.0。 + + Returns: + 登入帳號的 email;取不到或逾時時回傳空字串 ""。 + + 使用情境: + 在 CI 中驗證登入身分時呼叫。前置條件為 codex CLI 已安裝, + 且 $HOME/.codex/auth.json 已寫入有效的 OAuth token。 + """ process = subprocess.Popen( ["codex", "app-server"], stdin=subprocess.PIPE, @@ -20,6 +35,7 @@ def read_account_email(timeout_seconds: float = 25.0) -> str: ) def send(obj): + """將 dict 物件序列化成 JSON 後寫入 app-server 的 stdin 並 flush。""" process.stdin.write(json.dumps(obj) + "\n") process.stdin.flush() -- 2.53.0