ci.yml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. name: UNA CI
  2. on:
  3. push:
  4. branches: [ "master" ]
  5. pull_request:
  6. branches: [ "master" ]
  7. types: [opened, synchronize, reopened, closed]
  8. permissions:
  9. contents: read
  10. pull-requests: write
  11. # A newer push to a PR, or closing it, cancels that PR's run in progress, so the teardown can't
  12. # race a preview deploy that is still creating the environment. Master pushes never cancel.
  13. concurrency:
  14. group: ci-${{ github.event.pull_request.number && format('pr-{0}', github.event.pull_request.number) || github.sha }}
  15. cancel-in-progress: ${{ github.event_name == 'pull_request' }}
  16. jobs:
  17. dependencies:
  18. # Composer (plugins/) and Yarn (plugins_public/, via the postinstall build) are the same
  19. # tree for every PHP version, so they are installed once and the test legs download them.
  20. name: Install dependencies
  21. if: github.event_name == 'push' || github.event.action != 'closed'
  22. runs-on: ubuntu-latest
  23. steps:
  24. - uses: actions/checkout@v4
  25. # WARNING: after `composer update` or `composer upgrade`, replace this commit SHA
  26. # with the current shivammathur/setup-php release. Both Setup PHP steps must match.
  27. - name: Setup PHP
  28. uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
  29. with:
  30. php-version: '8.5'
  31. extensions: intl, curl, gd, mbstring, zip, exif, pdo_mysql
  32. ini-values: short_open_tag=On, memory_limit=256M
  33. tools: composer
  34. - name: Setup Node
  35. uses: actions/setup-node@v4
  36. with:
  37. node-version: '24'
  38. cache: yarn
  39. - name: Install PHP dependencies
  40. run: composer install --prefer-dist --no-progress --no-dev
  41. - name: Install JS dependencies
  42. run: yarn install --frozen-lockfile
  43. - name: Upload dependencies
  44. uses: actions/upload-artifact@v4
  45. with:
  46. name: una-dependencies
  47. retention-days: 1
  48. include-hidden-files: true
  49. if-no-files-found: error
  50. path: |
  51. plugins
  52. plugins_public
  53. test:
  54. # Units and Integration each get a runner per PHP version. Deploy waits on every leg, and a
  55. # failure in one does not cancel the other. The package is built only on the PHP 8.5 Units
  56. # leg so it is uploaded once.
  57. name: ${{ matrix.suite == 'Units' && 'Unit tests' || 'Integration tests' }} (PHP ${{ matrix.php }})
  58. needs: dependencies
  59. if: github.event_name == 'push' || github.event.action != 'closed'
  60. runs-on: ubuntu-latest
  61. strategy:
  62. fail-fast: false
  63. matrix:
  64. suite: [Units, Integration]
  65. php: ['8.3', '8.4', '8.5']
  66. services:
  67. mysql:
  68. image: mariadb:11
  69. env:
  70. MYSQL_ROOT_PASSWORD: root
  71. MYSQL_DATABASE: una
  72. MYSQL_USER: una
  73. MYSQL_PASSWORD: una
  74. ports:
  75. - 3306:3306
  76. options: >-
  77. --health-cmd="healthcheck.sh --connect --innodb_initialized"
  78. --health-interval=10s
  79. --health-timeout=5s
  80. --health-retries=10
  81. steps:
  82. - uses: actions/checkout@v4
  83. # WARNING: after `composer update` or `composer upgrade`, replace this commit SHA
  84. # with the current shivammathur/setup-php release. Both Setup PHP steps must match.
  85. - name: Setup PHP
  86. uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
  87. with:
  88. php-version: ${{ matrix.php }}
  89. extensions: intl, curl, gd, mbstring, zip, exif, pdo_mysql
  90. ini-values: short_open_tag=On, memory_limit=256M
  91. tools: phing, phpunit:12
  92. - name: Download dependencies
  93. uses: actions/download-artifact@v4
  94. with:
  95. name: una-dependencies
  96. - name: Prepare writable paths
  97. run: |
  98. mkdir -p cache cache_public logs tmp storage
  99. chmod a+rwX inc cache cache_public logs tmp storage
  100. test -f plugins/ffmpeg/ffmpeg.exe && chmod 0755 plugins/ffmpeg/ffmpeg.exe
  101. - name: Install UNA
  102. working-directory: install
  103. run: >
  104. php cmd.php
  105. -m bx_antispam
  106. --db_host=127.0.0.1
  107. --db_name=una
  108. --db_user=una
  109. --db_password=una
  110. --server_http_host=localhost
  111. --server_php_self=/install/index.php
  112. - name: Run PHPUnit
  113. run: phpunit -c tests/phpunit.xml --testsuite "${{ matrix.suite }}"
  114. - name: Build package
  115. if: success() && matrix.suite == 'Units' && matrix.php == '8.5'
  116. run: phing package -Dbuild=${{ github.run_number }}
  117. - name: Upload package artifact
  118. if: success() && matrix.suite == 'Units' && matrix.php == '8.5'
  119. uses: actions/upload-artifact@v4
  120. with:
  121. name: una-package
  122. path: packages/*.zip
  123. if-no-files-found: error
  124. neo-checks:
  125. # The jobs above don't look at neo/. `yarn lint` and `yarn typecheck` aren't clean on master
  126. # yet, so this fails only on errors the PR adds: both run on the PR and on its base, and
  127. # neo/scripts/check-new-errors.js compares them per file and rule (line numbers ignored).
  128. name: NEO lint and typecheck
  129. if: github.event_name == 'pull_request' && github.event.action != 'closed'
  130. runs-on: ubuntu-latest
  131. env:
  132. BASE_REF: ${{ github.event.pull_request.base.ref }}
  133. steps:
  134. - uses: actions/checkout@v4
  135. with:
  136. fetch-depth: 0
  137. - name: Check for NEO changes
  138. id: changes
  139. run: |
  140. if git diff --quiet "origin/$BASE_REF...HEAD" -- neo/; then
  141. echo "neo=false" >> "$GITHUB_OUTPUT"
  142. echo "No changes under neo/, nothing to check."
  143. else
  144. echo "neo=true" >> "$GITHUB_OUTPUT"
  145. fi
  146. - name: Setup Node
  147. if: steps.changes.outputs.neo == 'true'
  148. uses: actions/setup-node@v4
  149. with:
  150. node-version: '24'
  151. - name: Install NEO dependencies
  152. if: steps.changes.outputs.neo == 'true'
  153. working-directory: neo
  154. # yarn.lock isn't committed (see neo/AGENTS.md), so this resolves the package.json ranges.
  155. # The npm registry now and then answers 502 for one tarball; retry instead of failing the PR.
  156. run: |
  157. # --ignore-scripts keeps dependencies' install scripts from running in CI. NEO's own
  158. # postinstall is only patch-package, so run that one explicitly: the result is the
  159. # same lint and typecheck output as a plain install.
  160. for attempt in 1 2 3; do
  161. if yarn install --ignore-scripts --non-interactive --network-timeout 600000; then
  162. yarn patch-package
  163. exit 0
  164. fi
  165. echo "yarn install failed (attempt $attempt of 3)"
  166. sleep 15
  167. done
  168. exit 1
  169. - name: Lint and typecheck the PR
  170. if: steps.changes.outputs.neo == 'true'
  171. working-directory: neo
  172. run: |
  173. yarn --silent lint -f json -o "$RUNNER_TEMP/lint-head.json" || true
  174. node scripts/typecheck.js > "$RUNNER_TEMP/typecheck-head.txt" 2>&1 || true
  175. - name: Lint and typecheck the base branch
  176. if: steps.changes.outputs.neo == 'true'
  177. run: |
  178. git worktree add "$RUNNER_TEMP/base" "origin/$BASE_REF"
  179. cd "$RUNNER_TEMP/base/neo"
  180. # Reuse the PR's install instead of a second `yarn install`.
  181. ln -s "$GITHUB_WORKSPACE/neo/node_modules" node_modules
  182. for app in apps/next apps/expo; do
  183. if [ -d "$GITHUB_WORKSPACE/neo/$app/node_modules" ]; then
  184. ln -s "$GITHUB_WORKSPACE/neo/$app/node_modules" "$app/node_modules"
  185. fi
  186. done
  187. yarn --silent lint -f json -o "$RUNNER_TEMP/lint-base.json" || true
  188. node scripts/typecheck.js > "$RUNNER_TEMP/typecheck-base.txt" 2>&1 || true
  189. - name: Fail on new errors
  190. if: steps.changes.outputs.neo == 'true'
  191. working-directory: neo
  192. run: >-
  193. node scripts/check-new-errors.js
  194. --lint "$RUNNER_TEMP/lint-head.json" "$RUNNER_TEMP/lint-base.json"
  195. --typecheck "$RUNNER_TEMP/typecheck-head.txt" "$RUNNER_TEMP/typecheck-base.txt"
  196. preview-env:
  197. # Same-repo PRs only: forks and Dependabot get no secrets, and Dependabot PRs need no preview.
  198. name: Prepare PR environment
  199. if: >-
  200. github.event_name == 'pull_request' && github.event.action != 'closed'
  201. && github.event.pull_request.head.repo.full_name == github.repository
  202. && github.event.pull_request.user.login != 'dependabot[bot]'
  203. runs-on: ubuntu-latest
  204. env:
  205. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  206. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  207. RAILWAY_BASE_ENVIRONMENT: ${{ secrets.RAILWAY_ENVIRONMENT }}
  208. RAILWAY_TEMPLATE_ENVIRONMENT: pr-template
  209. RAILWAY_PR_ENVIRONMENT: pr-${{ github.event.pull_request.number }}
  210. UNA_PREVIEW_API_KEY: ${{ secrets.UNA_PREVIEW_API_KEY }}
  211. steps:
  212. - uses: actions/checkout@v4
  213. with:
  214. sparse-checkout: |
  215. .github/actions
  216. scripts/railway
  217. - uses: ./.github/actions/railway-cli
  218. - name: Provision PR environment
  219. run: |
  220. if [ -z "$RAILWAY_API_TOKEN" ] || [ -z "$RAILWAY_PROJECT_ID" ]; then
  221. echo "Set secrets RAILWAY_API_TOKEN (account token) and RAILWAY_PROJECT_ID" >&2
  222. exit 1
  223. fi
  224. base="${RAILWAY_BASE_ENVIRONMENT:-production}"
  225. template="${RAILWAY_TEMPLATE_ENVIRONMENT:-pr-template}"
  226. railway link --project "$RAILWAY_PROJECT_ID" --environment "$base" --json
  227. # Railway services are project-scoped: mariadb, una and neo already exist, so `railway add`
  228. # cannot create them again and a plain new environment holds no instances of them
  229. # (every preview after the first failed this way). A PR environment therefore gets
  230. # its instances by duplicating the template environment. An environment left over
  231. # from a failed run has no una instance; recreate it.
  232. if ! railway service status --service una --environment "$RAILWAY_PR_ENVIRONMENT" >/dev/null 2>&1; then
  233. railway environment delete "$RAILWAY_PR_ENVIRONMENT" -y >/dev/null 2>&1 || true
  234. railway environment new "$RAILWAY_PR_ENVIRONMENT" --duplicate "$template" --json
  235. # a duplicate may carry the template's deployments; the builds below replace them
  236. railway down --service una --environment "$RAILWAY_PR_ENVIRONMENT" -y >/dev/null 2>&1 || true
  237. railway down --service neo --environment "$RAILWAY_PR_ENVIRONMENT" -y >/dev/null 2>&1 || true
  238. fi
  239. railway link --project "$RAILWAY_PROJECT_ID" --environment "$RAILWAY_PR_ENVIRONMENT" --json
  240. # An environment duplicated before the neo service existed lacks it.
  241. scripts/railway/neo.sh instance "$RAILWAY_PR_ENVIRONMENT"
  242. # The duplicated una instance is what `railway up` deploys into. Setting its variables
  243. # doubles as the readiness check: it fails with "ServiceInstance not found" until it exists.
  244. for i in $(seq 1 12); do
  245. railway variable set --service una --environment "$RAILWAY_PR_ENVIRONMENT" --skip-deploys \
  246. MYSQLHOST=mariadb.railway.internal \
  247. MYSQLPORT=3306 \
  248. MYSQLDATABASE=una \
  249. MYSQLUSER=una \
  250. MYSQLPASSWORD=una \
  251. UNA_DEMO_SEED=1 \
  252. ${UNA_PREVIEW_API_KEY:+UNA_API_KEY="$UNA_PREVIEW_API_KEY"} \
  253. && break
  254. sleep 5
  255. done
  256. # Previews live on their Railway domains (<service>-pr-<n>.up.railway.app), which share
  257. # Railway's wildcard certificate. Custom pr-<n> / api-pr-<n> hosts on unacms.app needed two
  258. # Let's Encrypt certificates per PR, and a busy week ran into Let's Encrypt's rate limit
  259. # (PR #5934 got none). UNA_AUTO_HOSTNAME makes UNA build its URLs from its host.
  260. railway domain --service una --environment "$RAILWAY_PR_ENVIRONMENT" || true
  261. railway domain --service neo --environment "$RAILWAY_PR_ENVIRONMENT" || true
  262. service_host() {
  263. railway domain list --service "$1" --environment "$RAILWAY_PR_ENVIRONMENT" --json | jq -r '.domains[] | select(.type == "service") | .domain' | head -n1
  264. }
  265. una_host=$(service_host una)
  266. neo_host=$(service_host neo)
  267. [ -n "$una_host" ] || { echo "No Railway domain for una" >&2; exit 1; }
  268. [ -n "$neo_host" ] || { echo "No Railway domain for neo" >&2; exit 1; }
  269. una_url="https://${una_host#https://}"
  270. neo_url="https://${neo_host#https://}"
  271. railway variable set --service una --environment "$RAILWAY_PR_ENVIRONMENT" --skip-deploys UNA_CLIENT_URL="$neo_url"
  272. scripts/railway/neo.sh vars "$RAILWAY_PR_ENVIRONMENT" "$una_url" "$neo_url"
  273. deploy:
  274. name: Deploy PR to Railway (una)
  275. needs: [test, preview-env]
  276. runs-on: ubuntu-latest
  277. env:
  278. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  279. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  280. RAILWAY_PR_ENVIRONMENT: pr-${{ github.event.pull_request.number }}
  281. steps:
  282. - uses: actions/checkout@v4
  283. - uses: actions/download-artifact@v4
  284. with:
  285. name: una-package
  286. path: packages
  287. - name: Prepare artefact
  288. run: |
  289. mkdir -p deploy
  290. unzip -q packages/*.zip -d /tmp/una-pkg
  291. pkg_dir=$(find /tmp/una-pkg -mindepth 1 -maxdepth 1 -type d | head -n 1)
  292. cp -a "$pkg_dir"/. deploy/
  293. cp scripts/railway/Dockerfile scripts/railway/entrypoint.sh scripts/railway/railway.toml scripts/railway/.railwayignore \
  294. scripts/railway/configure.php scripts/railway/seed.php deploy/
  295. chmod +x deploy/entrypoint.sh
  296. - uses: ./.github/actions/railway-cli
  297. - name: Deploy artefact
  298. run: railway up ./deploy --path-as-root --no-gitignore --verbose --ci --service una --environment "$RAILWAY_PR_ENVIRONMENT" --project "$RAILWAY_PROJECT_ID"
  299. deploy-neo:
  300. # NEO isn't covered by `test`, so it builds alongside it.
  301. name: Deploy PR to Railway (neo)
  302. needs: preview-env
  303. runs-on: ubuntu-latest
  304. env:
  305. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  306. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  307. RAILWAY_PR_ENVIRONMENT: pr-${{ github.event.pull_request.number }}
  308. steps:
  309. - uses: actions/checkout@v4
  310. - uses: ./.github/actions/railway-cli
  311. - name: Deploy NEO
  312. run: |
  313. scripts/railway/neo.sh stage /tmp/deploy-neo
  314. railway up /tmp/deploy-neo --path-as-root --no-gitignore --verbose --ci --service neo --environment "$RAILWAY_PR_ENVIRONMENT" --project "$RAILWAY_PROJECT_ID"
  315. preview-comment:
  316. name: Comment preview URLs
  317. needs: [preview-env, deploy, deploy-neo]
  318. if: always() && needs.preview-env.result == 'success'
  319. runs-on: ubuntu-latest
  320. env:
  321. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  322. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  323. RAILWAY_PR_ENVIRONMENT: pr-${{ github.event.pull_request.number }}
  324. UNA_RESULT: ${{ needs.deploy.result }}
  325. NEO_RESULT: ${{ needs.deploy-neo.result }}
  326. GH_TOKEN: ${{ github.token }}
  327. PR_NUMBER: ${{ github.event.pull_request.number }}
  328. steps:
  329. - uses: actions/checkout@v4
  330. with:
  331. sparse-checkout: .github/actions
  332. - uses: ./.github/actions/railway-cli
  333. - name: Comment preview URLs on PR
  334. run: |
  335. railway link --project "$RAILWAY_PROJECT_ID" --environment "$RAILWAY_PR_ENVIRONMENT" --json >/dev/null
  336. service_url() {
  337. local host
  338. host=$(railway domain list --service "$1" --environment "$RAILWAY_PR_ENVIRONMENT" --json | jq -r '.domains[] | select(.type == "service") | .domain' | head -n1)
  339. [ -n "$host" ] && echo "https://${host#https://}"
  340. }
  341. una_url=$(service_url una || true)
  342. neo_url=$(service_url neo || true)
  343. state() { [ "$1" = success ] && echo "" || echo " (deploy: $1)"; }
  344. marker="<!-- railway-preview -->"
  345. body=$(printf '%s\n**Preview**: %s (NEO web client%s)\n**API**: %s (UNA%s)\n\nRailway environment: `%s`. A fresh UNA install takes about 10 minutes.' \
  346. "$marker" "$neo_url" "$(state "$NEO_RESULT")" "$una_url" "$(state "$UNA_RESULT")" "$RAILWAY_PR_ENVIRONMENT")
  347. comment_id=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate \
  348. --jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | contains(\"${marker}\"))) | .id" \
  349. | head -n1)
  350. if [ -n "$comment_id" ]; then
  351. gh api --method PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${comment_id}" -f body="$body"
  352. else
  353. gh pr comment "$PR_NUMBER" --body "$body"
  354. fi
  355. deploy-production:
  356. # Runs next to the Jenkins deploy to ci.una.io, which is unchanged.
  357. name: Deploy master to Railway (api.unacms.app)
  358. needs: test
  359. if: github.event_name == 'push' && github.ref == 'refs/heads/master'
  360. runs-on: ubuntu-latest
  361. concurrency:
  362. group: railway-production
  363. cancel-in-progress: false
  364. environment:
  365. name: production
  366. url: https://api.unacms.app
  367. env:
  368. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  369. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  370. RAILWAY_ENVIRONMENT: production
  371. VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  372. VERCEL_TEAM: yasko
  373. UNA_API_KEY: ${{ secrets.UNA_API_KEY }}
  374. steps:
  375. - uses: actions/checkout@v4
  376. - uses: actions/download-artifact@v4
  377. with:
  378. name: una-package
  379. path: packages
  380. - name: Prepare artefact
  381. run: |
  382. mkdir -p deploy
  383. unzip -q packages/*.zip -d /tmp/una-pkg
  384. pkg_dir=$(find /tmp/una-pkg -mindepth 1 -maxdepth 1 -type d | head -n 1)
  385. cp -a "$pkg_dir"/. deploy/
  386. cp scripts/railway/Dockerfile scripts/railway/entrypoint.sh scripts/railway/railway.toml scripts/railway/.railwayignore \
  387. scripts/railway/configure.php scripts/railway/seed.php deploy/
  388. chmod +x deploy/entrypoint.sh
  389. - uses: ./.github/actions/railway-cli
  390. - name: Configure production
  391. run: |
  392. if [ -z "$RAILWAY_API_TOKEN" ] || [ -z "$RAILWAY_PROJECT_ID" ]; then
  393. echo "Set secrets RAILWAY_API_TOKEN (account token) and RAILWAY_PROJECT_ID" >&2
  394. exit 1
  395. fi
  396. railway link --project "$RAILWAY_PROJECT_ID" --environment "$RAILWAY_ENVIRONMENT" --json
  397. # Database credentials and UNA_HASH_SECRET live in the Railway environment, not here.
  398. railway variable set --service una --environment "$RAILWAY_ENVIRONMENT" --skip-deploys \
  399. UNA_CLIENT_URL=https://unacms.app \
  400. ${UNA_API_KEY:+UNA_API_KEY="$UNA_API_KEY"}
  401. - name: Deploy artefact
  402. run: railway up ./deploy --path-as-root --no-gitignore --verbose --ci --service una --environment "$RAILWAY_ENVIRONMENT" --project "$RAILWAY_PROJECT_ID"
  403. - name: api.unacms.app DNS
  404. if: env.VERCEL_TOKEN != ''
  405. run: scripts/railway/domains.sh railway-domain api.unacms.app "$RAILWAY_ENVIRONMENT" una
  406. - name: Smoke test
  407. # A fresh production install takes about 4 minutes; allow 10.
  408. run: |
  409. for i in $(seq 1 60); do
  410. code=$(curl -s -o /dev/null -w '%{http_code}' https://api.unacms.app/ || true)
  411. [ "$code" = "200" ] && { echo "api.unacms.app is up"; exit 0; }
  412. # Until the certificate is issued, check the Railway hostname instead.
  413. host=$(railway domain list --service una --environment "$RAILWAY_ENVIRONMENT" --json | jq -r '.domains[] | select(.type == "service") | .domain' | head -n1)
  414. if [ -n "$host" ] && [ "$(curl -s -o /dev/null -w '%{http_code}' "https://${host#https://}/" || true)" = "200" ]; then
  415. echo "up at https://$host (api.unacms.app answered $code; certificate may still be pending)"
  416. exit 0
  417. fi
  418. sleep 10
  419. done
  420. echo "UNA did not come up" >&2
  421. exit 1
  422. deploy-production-neo:
  423. # The NEO web client for unacms.app. NEO_PRODUCTION_HOST (a repository variable) is the
  424. # public host; until it is set, the client is only on its Railway domain.
  425. name: Deploy master to Railway (neo)
  426. if: github.event_name == 'push' && github.ref == 'refs/heads/master'
  427. runs-on: ubuntu-latest
  428. concurrency:
  429. group: railway-production-neo
  430. cancel-in-progress: false
  431. environment:
  432. name: production-neo
  433. url: ${{ steps.urls.outputs.app_url }}
  434. env:
  435. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  436. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  437. RAILWAY_ENVIRONMENT: production
  438. VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  439. VERCEL_TEAM: yasko
  440. NEO_PRODUCTION_HOST: ${{ vars.NEO_PRODUCTION_HOST }}
  441. steps:
  442. - uses: actions/checkout@v4
  443. - uses: ./.github/actions/railway-cli
  444. - name: Configure neo
  445. id: urls
  446. run: |
  447. railway link --project "$RAILWAY_PROJECT_ID" --environment "$RAILWAY_ENVIRONMENT" --json
  448. railway domain --service neo --environment "$RAILWAY_ENVIRONMENT" || true
  449. neo_host=$(railway domain list --service neo --environment "$RAILWAY_ENVIRONMENT" --json | jq -r '.domains[] | select(.type == "service") | .domain' | head -n1)
  450. neo_host=${neo_host#https://}
  451. [ -n "$neo_host" ] || { echo "No Railway domain for neo" >&2; exit 1; }
  452. app_url="https://${NEO_PRODUCTION_HOST:-$neo_host}"
  453. scripts/railway/neo.sh vars "$RAILWAY_ENVIRONMENT" https://api.unacms.app "$app_url"
  454. echo "app_url=$app_url" >> "$GITHUB_OUTPUT"
  455. echo "neo_url=https://$neo_host" >> "$GITHUB_OUTPUT"
  456. - name: Deploy NEO
  457. run: |
  458. scripts/railway/neo.sh stage /tmp/deploy-neo
  459. railway up /tmp/deploy-neo --path-as-root --no-gitignore --verbose --ci --service neo --environment "$RAILWAY_ENVIRONMENT" --project "$RAILWAY_PROJECT_ID"
  460. - name: Production host DNS
  461. if: env.NEO_PRODUCTION_HOST != '' && env.VERCEL_TOKEN != ''
  462. run: scripts/railway/domains.sh railway-domain "$NEO_PRODUCTION_HOST" "$RAILWAY_ENVIRONMENT" neo
  463. - name: Smoke test
  464. env:
  465. NEO_URL: ${{ steps.urls.outputs.neo_url }}
  466. run: |
  467. for i in $(seq 1 30); do
  468. code=$(curl -s -o /dev/null -w '%{http_code}' "$NEO_URL/" || true)
  469. [ "$code" = "200" ] && { echo "NEO is up at $NEO_URL"; exit 0; }
  470. sleep 10
  471. done
  472. echo "NEO did not answer 200 at $NEO_URL (last: $code)" >&2
  473. exit 1
  474. teardown:
  475. name: Remove PR Railway environment
  476. if: >-
  477. github.event_name == 'pull_request' && github.event.action == 'closed'
  478. && github.event.pull_request.head.repo.full_name == github.repository
  479. && github.event.pull_request.user.login != 'dependabot[bot]'
  480. runs-on: ubuntu-latest
  481. env:
  482. RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
  483. RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
  484. RAILWAY_BASE_ENVIRONMENT: ${{ secrets.RAILWAY_ENVIRONMENT }}
  485. RAILWAY_PR_ENVIRONMENT: pr-${{ github.event.pull_request.number }}
  486. VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
  487. VERCEL_TEAM: yasko
  488. PREVIEW_API_HOST: api-pr-${{ github.event.pull_request.number }}.unacms.app
  489. PREVIEW_CLIENT_HOST: pr-${{ github.event.pull_request.number }}.unacms.app
  490. steps:
  491. - uses: actions/checkout@v4
  492. with:
  493. sparse-checkout: |
  494. .github/actions
  495. scripts/railway
  496. # Previews created before they moved to Railway domains (2026-10-20) still have DNS records
  497. # on unacms.app; this step can go once those PRs are closed.
  498. - name: Remove preview domains
  499. if: env.VERCEL_TOKEN != ''
  500. continue-on-error: true
  501. run: |
  502. scripts/railway/domains.sh client-unalias "$PREVIEW_CLIENT_HOST"
  503. scripts/railway/domains.sh dns-remove "$PREVIEW_CLIENT_HOST"
  504. scripts/railway/domains.sh dns-remove "$PREVIEW_API_HOST"
  505. - uses: ./.github/actions/railway-cli
  506. - name: Delete PR environment
  507. run: |
  508. if [ -z "$RAILWAY_API_TOKEN" ] || [ -z "$RAILWAY_PROJECT_ID" ]; then
  509. echo "Set secrets RAILWAY_API_TOKEN (account token) and RAILWAY_PROJECT_ID" >&2
  510. exit 1
  511. fi
  512. base="${RAILWAY_BASE_ENVIRONMENT:-production}"
  513. railway link --project "$RAILWAY_PROJECT_ID" --environment "$base" --json
  514. # The run for the PR's last push is cancelled above (workflow concurrency), so nothing
  515. # can create the environment after this. A PR closed before its first deploy has none.
  516. if railway environment delete "$RAILWAY_PR_ENVIRONMENT" -y; then
  517. echo "Deleted $RAILWAY_PR_ENVIRONMENT"
  518. else
  519. echo "No environment $RAILWAY_PR_ENVIRONMENT to delete"
  520. fi