Skip to content

Instantly share code, notes, and snippets.

@chrisleekr
Last active June 25, 2025 06:23
Show Gist options
  • Save chrisleekr/5a558061a43b1f92c1d9e337953e5b55 to your computer and use it in GitHub Desktop.
Save chrisleekr/5a558061a43b1f92c1d9e337953e5b55 to your computer and use it in GitHub Desktop.
codex-setup-script.sh
#!/bin/bash
####
# # 1st edition setup script.
# # How to use:
# # 1. Copy the below setup script to the environment
# curl -L -o /tmp/codex-setup-script.sh "https://gist.githubusercontent.com/chrisleekr/5a558061a43b1f92c1d9e337953e5b55/raw/codex-setup-script.sh?$(date +%s)"
# chmod +x /tmp/codex-setup-script.sh
# /tmp/codex-setup-script.sh
# # Create database for your service
# sudo -u postgres psql -c "CREATE DATABASE svc_service_spec;"
# sudo -u postgres psql -c "ALTER ROLE postgres WITH PASSWORD NULL;"
####
set -e
POSTGRES_INSTALL=${POSTGRES_INSTALL:-"false"}
POSTGRES_VERSION=${POSTGRES_VERSION:-16}
if [ -z "$NPM_TOKEN" ]; then
echo "Error: NPM_TOKEN environment variable is not set"
exit 1
fi
REDIS_INSTALL=${REDIS_INSTALL:-"false"}
apt update
# Install postgres
if grep -q "cimg/postgres" .circleci/config.yml || [ "$POSTGRES_INSTALL" = "true" ]; then
echo "Installing postgres"
install -d /usr/share/postgresql-common/pgdg
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
# shellcheck source=/dev/null
. /etc/os-release
sh -c "echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
apt update
apt-get install -y "postgresql-$POSTGRES_VERSION"
service postgresql start
echo "local all all trust" >"/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf"
echo "host all all 127.0.0.1/32 trust" >>"/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf"
echo "host all all ::1/128 trust" >>"/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf"
# Reload PostgreSQL
service postgresql reload
echo "pg_hba.conf updated. PostgreSQL reloaded."
else
echo "No postgres found in .circleci/config.yml"
fi
# Install redis - simply install latest version
if grep -q "cimg/redis" .circleci/config.yml || [ "$REDIS_INSTALL" = "true" ]; then
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
apt-get update
apt-get install -y redis
redis-server --daemonize yes
else
echo "No redis found in .circleci/config.yml"
fi
# Install nvm
# shellcheck source=/dev/null
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install
nvm use
# Install yarn
npm install -g yarn
yarn install --frozen-lockfile
# Set instruction variable with the following content
INSTRUCTIONS=$(
cat <<'EOF'
## STRICT INSTRUCTIONS - FOLLOW EXACTLY
- Always run `nvm use` first to ensure you are using the correct Node.js version.
- Install Yarn globally with `npm install -g yarn`.
- Verify Yarn version by running `yarn --version`. You must be using Yarn 1.x.
- Do NOT run `yarn install` if the `node_modules` directory already exists. The dependencies are already installed.
- If you encounter `Failed to replace env in config: ${NPM_TOKEN}`, set a dummy token with `export NPM_TOKEN=dummy` in the shell.
- Set environment variables as defined in `.circleci/config.yml`:
- Check if each variable is already set in the shell environment.
- Only set variables that are missing by reading the `environment` section in `.circleci/config.yml`.
- Do not override existing shell environment variables.
- When testing changes:
- Strictly follow the steps in `.circleci/config.yml`.
- Ignore all `service-deployment/*` jobs.
- Never rerun `yarn install`—it has already been executed in the setup script.
- If you see this error:
- `This package doesn't seem to be present in your lockfile; run "yarn install" to update the lockfile at jR.getCandidates (/root/.cache/node/corepack/v1/yarn/4.9.2/yarn.j)`
- It means you are using the wrong Yarn version.
- Run `nvm use` again to ensure the correct Node.js version is active.
Follow these instructions exactly. Do not skip or modify any steps.
EOF
)
echo "$INSTRUCTIONS" >>./AGENTS.md
echo "AGENTS.md created"
echo "Setup script completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment