openai/codex-action

Public

mirrored from https://github.com/openai/codex-actionAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
pr18

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

action.yml

265lines · modecode

1name: "Codex Exec Action"
2description: "Run `codex exec` with a prompt."
3author: "OpenAI"
4inputs:
5 prompt:
6 description: "Prompt to pass to Codex. `prompt` or `prompt-file` must be provided."
7 required: false
8 default: ""
9 prompt-file:
10 description: "Path to file that contains the prompt to pass to Codex. `prompt` or `prompt-file` must be provided."
11 required: false
12 default: ""
13 output-file:
14 description: "File where the final Codex message is written. Leave empty to skip writing a file."
15 required: false
16 default: ""
17 openai-api-key:
18 description: "OpenAI API key used by Codex."
19 required: false
20 default: ""
21 working-directory:
22 description: "Working directory that Codex should use. Defaults to the repository root."
23 required: false
24 default: ""
25 sandbox:
26 description: |
27 Sandbox mode for Codex. One of `workspace-write` (default), `read-only` or `danger-full-access`.
28 required: false
29 default: "workspace-write"
30 codex-version:
31 description: "Version of `@openai/codex` to install."
32 required: false
33 default: ""
34 codex-args:
35 description: "Additional args to pass through to `codex exec`. If this value starts with `[`, it will be parsed as a JSON array; otherwise, it will be parsed as a shell-like string."
36 required: false
37 default: ""
38 output-schema:
39 description: "Inline schema contents to use with `codex exec --output-schema`."
40 required: false
41 default: ""
42 output-schema-file:
43 description: "File path to the schema that should be passed to `codex exec --output-schema`."
44 required: false
45 default: ""
46 model:
47 description: "Model the agent should use."
48 required: false
49 default: ""
50 codex-home:
51 description: "Directory to use as the Codex home directory. If empty, the default Codex home directory will be used."
52 required: false
53 default: ""
54 safety-strategy:
55 description: |
56 Specify one of the following options (on Windows, the only supported option is `unsafe`):
57
58 * `drop-sudo` (default, IRREVERSIBLE) Drop sudo privileges (if any) from
59 the default user before running Codex, and run Codex as that user. This
60 is only supported on Linux and macOS runners. This option is
61 irreversible: if the default user has sudo privileges, they will be
62 removed permanently for the duration of the job.
63 * `unprivileged-user` Run Codex as the specified user specified by the
64 `codex-user` option (the user must already exist). Note the caller is
65 responsible for ensuring the specified user has the privileges it needs
66 to perform the requested actions. For example, the copy of the repo
67 created by `actions/checkout` is not world-readable by default.
68 * `read-only` Run Codex in a sandbox that can read any file on disk,
69 but cannot write to disk or access the network. Note Codex will still
70 run as the default user for this Action, which likely has sudo
71 privileges, so it could read `openai-api-key` from memory and reveal it
72 by printing it to the output of the GitHub Action.
73 * `unsafe` (NOT RECOMMENDED) Do not try to restrict Codex's privileges at all.
74 This is extremely dangerous, as the default user for this Action likely
75 has sudo privileges, which means it can read secrets stored in memory
76 (such as the value of `openai-api-key`) and print them to the output
77 of the GitHub Action or exfiltrate them in other ways.
78 required: false
79 default: "drop-sudo"
80 codex-user:
81 description: "If `safety-strategy` is set to `unprivileged-user`, this specifies the UNIX username to run Codex as."
82 required: false
83 default: ""
84 allow-users:
85 description: "Comma-separated list of GitHub usernames who can run this action, or '*' to allow all users. Note users who have write access to the GitHub repo have access by default and do not need to be listed here."
86 required: false
87 default: ""
88 allow-bots:
89 description: "Allow runs triggered by GitHub Apps/bot accounts to bypass the write-access check."
90 required: false
91 default: "false"
92outputs:
93 final-message:
94 description: "Raw output emitted by `codex exec`."
95 value: ${{ steps.run_codex.outputs['final-message'] }}
96runs:
97 using: "composite"
98 steps:
99 - name: Validate Windows safety strategy
100 if: ${{ runner.os == 'Windows' }}
101 shell: bash
102 run: |
103 if [ "${{ inputs['safety-strategy'] }}" != "unsafe" ]; then
104 echo "On Windows, inputs['safety-strategy'] must be 'unsafe'" >&2
105 echo "because no viable sandboxing options are available at this time." >&2
106 exit 1
107 fi
108
109 - name: Ensure Node.js available
110 uses: actions/setup-node@v4
111 with:
112 node-version: "20"
113
114 - name: Check repository write access
115 env:
116 GITHUB_TOKEN: ${{ github.token }}
117 shell: bash
118 run: |
119 node "${{ github.action_path }}/dist/main.js" check-write-access \
120 --allow-bots "${{ inputs['allow-bots'] }}" \
121 --allow-users "${{ inputs['allow-users'] }}"
122
123 - name: Install Codex CLI
124 shell: bash
125 run: npm install -g "@openai/codex@${{ inputs['codex-version'] }}"
126
127 - name: Install Codex Responses API proxy
128 shell: bash
129 run: npm install -g "@openai/codex-responses-api-proxy@${{ inputs['codex-version'] }}"
130
131 - name: Resolve Codex home
132 id: resolve_home
133 shell: bash
134 run: |
135 node "${{ github.action_path }}/dist/main.js" resolve-codex-home "${{ inputs['codex-home'] }}"
136
137 - name: Determine server info path
138 id: derive_server_info
139 shell: bash
140 run: |
141 server_info_file="${{ steps.resolve_home.outputs.codex-home }}/${{ github.run_id }}.json"
142 echo "server_info_file=$server_info_file" >> "$GITHUB_OUTPUT"
143
144 - name: Check Responses API proxy status
145 id: start_proxy
146 if: ${{ inputs['openai-api-key'] != '' }}
147 shell: bash
148 run: |
149 server_info_file="${{ steps.derive_server_info.outputs.server_info_file }}"
150 if [ -s "$server_info_file" ]; then
151 echo "Responses API proxy already appears to be running (found $server_info_file)."
152 echo "server_info_file_exists=true" >> "$GITHUB_OUTPUT"
153 else
154 echo "server_info_file_exists=false" >> "$GITHUB_OUTPUT"
155 fi
156
157 # This is its own step to minimize the runtime logic that has access to the
158 # API key. Note we use `env -u OPENAI_API_KEY` to ensure extra copies of the
159 # key do not end up in the memory of the `codex-responses-api-proxy` process
160 # where environment variables are stored.
161 - name: Start Responses API proxy
162 if: ${{ inputs['openai-api-key'] != '' && steps.start_proxy.outputs.server_info_file_exists == 'false' }}
163 env:
164 OPENAI_API_KEY: ${{ inputs['openai-api-key'] }}
165 shell: bash
166 run: |
167 (
168 printenv OPENAI_API_KEY | env -u OPENAI_API_KEY codex-responses-api-proxy --http-shutdown --server-info "${{ steps.derive_server_info.outputs.server_info_file }}"
169 ) &
170
171 - name: Wait for Responses API proxy
172 if: ${{ inputs['openai-api-key'] != '' && steps.start_proxy.outputs.server_info_file_exists == 'false' }}
173 shell: bash
174 run: |
175 server_info_file="${{ steps.derive_server_info.outputs.server_info_file }}"
176 for _ in {1..10}; do
177 if [ -s "$server_info_file" ]; then
178 break
179 fi
180 sleep 1
181 done
182 if [ ! -s "$server_info_file" ]; then
183 echo "responses-api-proxy did not write server info" >&2
184 exit 1
185 fi
186
187 if [ "${RUNNER_OS}" != "Windows" ]; then
188 chmod 444 "$server_info_file"
189 sudo chown root "$server_info_file"
190 fi
191
192 # This step has an output named `port`.
193 - name: Read server info
194 id: read_server_info
195 if: ${{ inputs['openai-api-key'] != '' || inputs.prompt != '' || inputs['prompt-file'] != '' }}
196 shell: bash
197 run: node "${{ github.action_path }}/dist/main.js" read-server-info "${{ steps.derive_server_info.outputs.server_info_file }}"
198
199 - name: Write Codex proxy config
200 if: ${{ inputs['openai-api-key'] != '' }}
201 shell: bash
202 run: |
203 node "${{ github.action_path }}/dist/main.js" write-proxy-config \
204 --codex-home "${{ steps.resolve_home.outputs.codex-home }}" \
205 --port "${{ steps.read_server_info.outputs.port }}"
206
207 - name: Drop sudo privilege, if appropriate
208 if: ${{ inputs['safety-strategy'] == 'drop-sudo' && inputs['openai-api-key'] != '' }}
209 shell: bash
210 run: |
211 case "${RUNNER_OS}" in
212 Linux)
213 node "${{ github.action_path }}/dist/main.js" drop-sudo --user runner --group sudo
214 ;;
215 macOS)
216 node "${{ github.action_path }}/dist/main.js" drop-sudo --user runner --group admin
217 ;;
218 *)
219 echo "Unsupported OS for drop-sudo: ${RUNNER_OS}" >&2
220 exit 1
221 ;;
222 esac
223
224 - name: Verify sudo privilege removed
225 if: ${{ inputs['safety-strategy'] == 'drop-sudo' && inputs['openai-api-key'] != '' }}
226 shell: bash
227 run: |
228 if sudo -n true 2>/dev/null; then
229 echo "Expected sudo to be disabled, but sudo succeeded." >&2
230 exit 1
231 fi
232 echo "Confirmed sudo privilege is disabled."
233
234 - name: Run codex exec
235 id: run_codex
236 if: ${{ inputs.prompt != '' || inputs['prompt-file'] != '' }}
237 env:
238 CODEX_PROMPT: ${{ inputs.prompt }}
239 CODEX_PROMPT_FILE: ${{ inputs['prompt-file'] }}
240 CODEX_OUTPUT_FILE: ${{ inputs['output-file'] }}
241 CODEX_HOME: ${{ steps.resolve_home.outputs.codex-home }}
242 CODEX_WORKING_DIRECTORY: ${{ inputs['working-directory'] || github.workspace }}
243 CODEX_SANDBOX: ${{ inputs.sandbox }}
244 CODEX_ARGS: ${{ inputs['codex-args'] }}
245 CODEX_OUTPUT_SCHEMA: ${{ inputs['output-schema'] }}
246 CODEX_OUTPUT_SCHEMA_FILE: ${{ inputs['output-schema-file'] }}
247 CODEX_MODEL: ${{ inputs.model }}
248 CODEX_SAFETY_STRATEGY: ${{ inputs['safety-strategy'] }}
249 CODEX_USER: ${{ inputs['codex-user'] }}
250 FORCE_COLOR: 1
251 shell: bash
252 run: |
253 node "${{ github.action_path }}/dist/main.js" run-codex-exec \
254 --prompt "${CODEX_PROMPT}" \
255 --prompt-file "${CODEX_PROMPT_FILE}" \
256 --output-file "$CODEX_OUTPUT_FILE" \
257 --codex-home "$CODEX_HOME" \
258 --cd "$CODEX_WORKING_DIRECTORY" \
259 --extra-args "$CODEX_ARGS" \
260 --output-schema "$CODEX_OUTPUT_SCHEMA" \
261 --output-schema-file "$CODEX_OUTPUT_SCHEMA_FILE" \
262 --sandbox "$CODEX_SANDBOX" \
263 --model "$CODEX_MODEL" \
264 --safety-strategy "$CODEX_SAFETY_STRATEGY" \
265 --codex-user "$CODEX_USER"
266