diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..db8d5f8 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,26 @@ +# Cheap syntax checks, on every pull request. +name: Lint + +on: + pull_request: + push: + branches: [master] + +concurrency: + group: lint-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + runs-on: macos-latest + steps: + - uses: actions/checkout@v7 + - name: Shell syntax + run: | + for f in create_setup_build_scripts.sh samples/*.sh xcinstall/*.sh tests/*.sh templates/*.sh; do bash -n "$f" && echo "ok $f"; done + - name: Ruby syntax + run: | + for f in fastlane/CommonFastfile samples/*.rb; do ruby -c "$f" > /dev/null && echo "ok $f"; done + - name: YAML is valid + run: | + ruby -ryaml -e 'ARGV.each { |f| YAML.load_file(f, aliases: true); puts "ok #{f}" }' templates/github-actions/*.yml .github/workflows/*.yml diff --git a/.github/workflows/smoke.yml b/.github/workflows/smoke.yml new file mode 100644 index 0000000..0066265 --- /dev/null +++ b/.github/workflows/smoke.yml @@ -0,0 +1,40 @@ +# End-to-end test (tests/smoke.sh): generates a sample iOS project, runs the generator on it, then +# build / tests / warnings / coverage / doctor. Slow (macOS + simulator), so it only runs when something +# that ends up in generated projects, or the test itself, changes. +name: Smoke test + +on: + pull_request: + paths: &paths + - create_setup_build_scripts.sh + - fastlane/** + - templates/** + - samples/** + - xcinstall/** + - tests/** + - versions.env + - .github/workflows/smoke.yml + push: + branches: [master] + paths: *paths + workflow_dispatch: + +concurrency: + group: smoke-${{ github.ref }} + cancel-in-progress: true + +jobs: + lanes: + # Fast: portal and lint lanes with stubs (tests/lanes.sh) + runs-on: macos-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v7 + - run: tests/lanes.sh + + smoke: + runs-on: macos-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v7 + - run: tests/smoke.sh diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml new file mode 100644 index 0000000..142e23b --- /dev/null +++ b/.github/workflows/versions.yml @@ -0,0 +1,84 @@ +# Keeps the versions new projects get (versions.env) current without anybody picking them: +# every Monday it tries the newest Ruby with the end-to-end smoke test; if it passes, it opens a PR bumping +# versions.env, if it fails, it opens an issue. It also reports outdated actions used in templates/. +name: Versions + +on: + schedule: + - cron: "0 6 * * 1" + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + issues: write + +jobs: + ruby: + runs-on: macos-latest + timeout-minutes: 90 + env: + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v7 + + - name: Find the newest stable Ruby + id: ruby + run: | + brew install rbenv > /dev/null 2>&1 || true + # Upstream ruby-build as an rbenv plugin: Homebrew's can lag behind Ruby releases + git clone -q --depth 1 https://github.com/rbenv/ruby-build.git "$(rbenv root)/plugins/ruby-build" + latest=$(rbenv install -l | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) + current=$(grep -E '^RUBY_VERSION=' versions.env | cut -d= -f2) + echo "latest=$latest" >> "$GITHUB_OUTPUT" + echo "current=$current" >> "$GITHUB_OUTPUT" + echo "Pinned: $current, newest: $latest" + + - name: Smoke test with the newest Ruby + id: smoke + if: steps.ruby.outputs.latest != steps.ruby.outputs.current + continue-on-error: true + env: + XCODE_SCRIPTS_RUBY: ${{ steps.ruby.outputs.latest }} + run: tests/smoke.sh + + - name: Open a PR bumping versions.env + if: steps.smoke.outcome == 'success' + env: + LATEST: ${{ steps.ruby.outputs.latest }} + CURRENT: ${{ steps.ruby.outputs.current }} + run: | + branch="bump-ruby-$LATEST" + sed -i '' -e "s/^RUBY_VERSION=.*/RUBY_VERSION=$LATEST/" versions.env + git config user.name "github-actions" + git config user.email "github-actions@users.noreply.github.com" + git checkout -b "$branch" + git commit -am "Bump Ruby for new projects: $CURRENT -> $LATEST" + git push origin "$branch" + gh pr create --title "Bump Ruby for new projects: $CURRENT -> $LATEST" \ + --body "tests/smoke.sh passed with Ruby $LATEST (run: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID). Merging makes it the default for newly generated projects." + + - name: Open an issue when the newest Ruby breaks the setup + if: steps.smoke.outcome == 'failure' + env: + LATEST: ${{ steps.ruby.outputs.latest }} + run: | + gh label create versions --color FBCA04 --description "Dependency / version freshness" 2>/dev/null || true + gh issue create --label versions --title "Ruby $LATEST breaks the generated setup" \ + --body "tests/smoke.sh failed with Ruby $LATEST: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID. New projects keep getting the version in versions.env until this is fixed." + + actions: + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v7 + - id: check + continue-on-error: true + run: tests/check-actions.sh + - name: Open an issue for outdated actions + if: steps.check.outcome == 'failure' + run: | + gh label create versions --color FBCA04 --description "Dependency / version freshness" 2>/dev/null || true + gh issue create --label versions --title "Actions in templates/ are outdated" \ + --body "tests/check-actions.sh reports newer majors: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b83356 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# macOS / editors +.DS_Store +.idea/ +.vscode/ + +# Ruby (local tooling and tests) +.bundle/ +vendor/bundle/ +Gemfile.lock +.ruby-version + +# Artifacts produced when trying the scripts on a sample project +deriveddata/ +gymbuildlog/ +coverage/ +fastlane/test_output/ +fastlane/test_output_ui/ +fastlane/report.xml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..31ba694 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,34 @@ +# Changelog + +## 2.0.0 (2026-12-02) + +Breaking: generated projects are now SPM-only, and the repo moved to GitHub. + +- All URLs moved from the retired gitlab.inqbarna.com to https://github.com/InQBarna/xcode-scripts (HTTPS). +- Generated Fastfile imports `CommonFastfile` with `version: '~> 2.0'`. Releases are git tags. +- Removed CocoaPods: no `pod init`/`pod install`, no Podfile/workspace lookup; build uses `-project`. +- Homebrew prefix detected (Apple Silicon), bash shebangs, `exit 1` instead of `exit -1`. +- `build.sh`: simulator runtime and device default to the newest installed ones (override with `RUNTIME`, `DEVICE`). +- `CommonFastfile`: removed `compileBitcode`, `fastlane_version` raised to 2.242.0 (the lowest version tested). +- Build/test logic moved from the generated `build.sh` into shared lanes: `iq_build` / `iq_test` helpers in `CommonFastfile`, `build` and `test` lanes in the generated Fastfile. `build.sh` is now a thin wrapper. Warnings from Swift packages no longer count. Fixed `build.sh` bugs (inverted `JOB_NAME` check, UI tests run against a non-existent target). +- Generated `.gitignore` also ignores build/test artifacts and `fastlane/AuthKey_*.p8` / `fastlane/.env*`; this repo got a `.gitignore`. +- CI templates in `templates/` (GitHub Actions `ci.yml` and `distribute.yml`, `Jenkinsfile`), offered by the generator (`CI_PROVIDER`). +- New `iq_asc_api_key` helper: App Store Connect key from `ASC_KEY_ID` / `ASC_ISSUER_ID` / `ASC_KEY_P8` env vars (CI secrets) or a key file. The generated Fastfile only configures it for lanes other than `build`, `test`, `lint` and `bump_version` (which work offline). +- `slather` and `xchtmlreport` removed. `iq_test` reads the xcresult with fastlane's `trainer` (junit) and `xccov` (coverage, converted to `coverage/cobertura.xml` by `iq_cobertura_from_xcresult`). Results are now written even when tests fail. Build-only runs write empty junit files but no `coverage/cobertura.xml` (the Jenkins Coverage plugin fails on an empty one). The Gemfile only has `fastlane` and `xcodeproj`. +- `xcinstall/install.sh` rewritten on top of `xcodes` (`xcode-install` / `xcversion` are deprecated, `xcinstall/Gemfile` removed). `.xcode-version` (created by the generator) selects the Xcode in `build.sh` and the GitHub Actions workflow. +- TestFlight: helper renamed `iq_beta` -> `iq_testflight` and generated lane `beta` -> `ship_testflight` (a lane called `testflight` clashes with fastlane's action of the same name). `iq_beta` stays as a deprecated alias that warns, so existing `lane :beta` keep working. `INTENT=testflight` in `build.sh` / Jenkinsfile (`INTENT=appstore` still accepted by `build.sh`). Git tags stay `appstore/v...`. +- Generator can run unattended (`TEAM_ID`, `MATCH_GIT_URL`, `CI_PROVIDER`) and from a clone (`XCODE_SCRIPTS_USE_LOCAL=1`); helper scripts are no longer written to fixed `/tmp` paths. +- `tests/smoke.sh` + `tests/fixtures/SampleApp`: end-to-end test of the generator and the generated build/test setup, run by `.github/workflows/smoke.yml` (only when generator, lanes, templates, samples, tests or versions change) next to the always-on `lint.yml`. It found and fixed two bugs: `build.sh` rejected any bundler other than 2.x, and building for testing through `gym` silently dropped the test bundles (it appends its own `build` action), so unit tests could not run; `scan(build_for_testing: true)` is used instead. +- Versions come from one place, `versions.env` (Ruby 4.0.10, the version the smoke test passes with; `XCODE_SCRIPTS_RUBY=x.y.z` overrides), instead of being hardcoded in the scripts. The generator writes it to the project's `.ruby-version`; `setup.sh`, `build.sh` and the Gemfile (`ruby file: '.ruby-version'`) read it from there, so each project owns its version. Bundler is the one shipped with that Ruby, Gemfile.lock records it. CI templates use `macos-latest`; generated projects get a `dependabot.yml` for actions and gems. Verified with Ruby 4.0.10, bundler 4.0.20, fastlane 2.242.0. +- `scripts/doctor.sh` (from `templates/doctor.sh`) checks Xcode, simulator, Ruby, gems, shared scheme and coverage, fastlane files, signing and CI, a committed `.p8` key, and prints a fix for each problem (`--offline` skips network checks). +- `.github/workflows/versions.yml` (weekly): smoke-tests the newest Ruby and opens a PR bumping `versions.env` when it passes, or an issue when it fails; `tests/check-actions.sh` reports outdated actions in the templates. +- New lanes: `check_certs` (expiry report, `strict:true` for CI), `sync_devices` (devices.txt from the portal with the API key), `lint` (SwiftLint / SwiftFormat), and `bump_version` in the generated Fastfile (`iq_bump_version`). `tests/lanes.sh` tests them with stubs; `tests/smoke.sh` runs `bump_version` for real. +- `setup.sh` installs the Ruby even when Homebrew's `ruby-build` is too old to know it (falls back to upstream `ruby-build` as an rbenv plugin); found by the first run on a fresh hosted runner. +- `scripts/doctor.sh` warns when the locale is not UTF-8 (fastlane crashes with "invalid byte sequence in US-ASCII" when run directly; `build.sh` sets it itself). `tests/smoke.sh` now behaves like a developer shell (UTF-8, rbenv active) and is run locally in a clean environment (`env -i`) to catch assumptions about the machine. +- Build-only runs write empty but well-formed junit / Cobertura files (a 0 byte file can make the Jenkins plugins fail to parse it). The build log is found by whatever name fastlane gave it, Jenkins job names are sanitized before they go into the simulator name, and an empty `ASC_KEY_ID` / `ASC_ISSUER_ID` (unset CI secret) no longer overrides the Fastfile values. +- `build.sh` gives fastlane's `xcodebuild -showBuildSettings` 30 seconds instead of 3 (`FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT` to change it): it timed out on a busy machine and the lane died before running anything. +- `setup_match.sh`: fixed inverted check that tried to re-import the common Fastfile when already present. + +## 1.x + +See git history (`renew_certs` lane added Nov 2026). diff --git a/README.md b/README.md index 0f890e9..c2f38fd 100644 --- a/README.md +++ b/README.md @@ -1,90 +1,164 @@ -# Goals +# xcode-scripts -We want to start a fresh project ready for CICD, which means +Common tooling to start a **new iOS project ready for CI/CD** (Swift Package Manager only). +It generates the scripts and fastlane setup so every project builds, tests and ships the same way. - - Easily build te project in any (CICD) machine (with some minimum requirements). And guarantee 0 warnings! - - Easily run the tests and guarante they pass. And report coverage! - - Easily send a test version to stakeholders - - Easily send a vertion to AppStoreConnect for a new release +## What you get -## How to set up a new project +- `scripts/setup.sh`: installs what a machine needs (rbenv, the Ruby from `.ruby-version`, gems). Idempotent. +- `scripts/doctor.sh`: checks the whole setup (Xcode, Ruby, gems, scheme, fastlane, signing, CI) and says how to fix what is wrong. +- `scripts/build.sh`: builds the project with **0 warnings allowed**; with `INTENT=test` also runs unit/UI tests and reports coverage. +- `fastlane/Fastfile` importing the shared lanes from this repo (`fastlane/CommonFastfile`), pinned to a major version. +- Build number build phase (`YYYYMMddHHmm`), match setup, `.gitignore`. -1. Grab the requirements: - - Have the repository created on github/your platform +### Shared lanes (`fastlane/CommonFastfile`) + +| Lane | What it does | +|---|---| +| `update_devices_and_profiles` | Registers `fastlane/devices.txt` on the dev portal and regenerates development/adhoc/appstore profiles with match | +| `install_profiles` | Installs existing profiles read-only (no dev portal access needed) | +| `renew_certs` | Renews expired certificates and regenerates profiles. `dry_run:true` only lists what would be deleted | +| `check_certs` | Lists the match profiles of your Matchfile app identifiers and the certificates they use, with their expiry (`warn_days:30`); other certificates of the team are ignored. `strict:true` fails when something is expired, expiring or invalid: run it weekly on CI so renewal is never a surprise. Read only, needs just the API key | +| `sync_devices` | Writes the enabled iOS devices of the developer portal to `fastlane/devices.txt` and shows what changed (`dry_run:true` to only look). Uses the API key, so no username/password needed | +| `lint` | Runs SwiftLint (`--strict`, `strict:false` to allow warnings) and SwiftFormat (`--lint`, nothing is modified) when configured/installed. Fails if a tool has a config file but is not installed | +| `bump_version` | Generated in your Fastfile: `bump_version bump:patch\|minor\|major` or `bump_version version:1.2.3`; changes the marketing version and commits it | + +Helper methods called from the lanes of your project's Fastfile (the generator creates them): + +| Helper | Project lane | What it does | +|---|---|---| +| `iq_build` | `build` | Builds for the simulator, fails on warnings (warnings inside Swift packages are ignored) | +| `iq_test` | `test` | Build (0 warnings), unit tests with coverage (`coverage/cobertura.xml`), UI tests with junit report (`fastlane/test_output_ui/report.junit`) | +| `iq_testflight` | `ship_testflight` | Builds and uploads to TestFlight, uploads dSYMs, bumps the version, pushes a git tag | +| `iq_firebase_v2` | `firebase` | Same, for Firebase App Distribution (AdHoc) | + +`scripts/build.sh` is just a wrapper: no `INTENT` runs `fastlane build`, `INTENT=test` runs `fastlane test`, `firebase`/`testflight` run the distribution lanes. +Tunable with env vars: `RUNTIME`, `DEVICE`, `MAX_WARNINGS` (default 0), `DO_NOT_DELETE_SIMULATOR`. When `BUILD_NUMBER` is set (Jenkins) a dedicated throw-away simulator is created per job. +UI tests are reported but don't fail the lane by default (`fail_on_ui_test_failure: true` in the `test` lane to change it); UI tests are skipped when the project has no UI test target. +Results are written even when tests fail (the lane fails at the end): junit with fastlane's `trainer`, coverage converted from the xcresult with `xccov` to `coverage/cobertura.xml` (Swift packages and test targets excluded; `coverage_ignore: [...]` in the `test` lane adds more). No extra gems or brew packages are needed. + +## Versioning + +Generated Fastfiles import this repo with `version: '~> 2.0'`, so projects only receive compatible changes. +Releases are git tags (`v2.x.y`); see [CHANGELOG.md](CHANGELOG.md). To pick up a new major version, bump the version in your Fastfile. + +## Set up a new project + +1. Grab the requirements (seriously, grab them first): + - Repository created on GitHub - App name - - Bundle identifier created on dev portal - - Apple team id (from dev portal) + - Bundle identifier created on the dev portal + - Apple team id - Match git repo url - - Firebase app id (from firebase, looks like: 1:XXXX:ios:XXXX) - - Auth p8 file (from dev portal) - - Key id - - Issuer id - - Auth p8 file (from dev portal) - - App created on appstoreconnect + - Firebase app id (looks like `1:XXXX:ios:XXXX`), if using Firebase distribution + - App Store Connect API key: key id, issuer id and the `.p8` file + - App created on App Store Connect + +2. Clone your project repository and create the Xcode project at its root (iOS app, existing bundle id). Close Xcode. + +3. From the project root, run the generator (it asks for some input, so use process substitution, not `curl | bash`): +``` +bash <(curl -fsSL https://raw.githubusercontent.com/InQBarna/xcode-scripts/master/create_setup_build_scripts.sh) +``` + or, with this repo cloned next to your project: `../xcode-scripts/create_setup_build_scripts.sh`. + Use `XCODE_SCRIPTS_REF=` to test another version of the helper scripts. + +4. Finish the manual bits: + - Put the Firebase id in `fastlane/Fastfile` + - Download the `.p8` file to `fastlane/` and set key id and issuer id in `fastlane/Fastfile` + - Make sure the scheme is **shared** (Product > Scheme > Manage Schemes) and committed + - Scheme > Test: enable "Gather coverage" for **all targets** + +5. Verify: `./scripts/setup.sh` and then `./scripts/build.sh` (`INTENT=test ./scripts/build.sh` to run tests). + + Simulator defaults to the newest installed iOS runtime and iPhone; override with `RUNTIME=18.2 DEVICE="iPhone 16"`. + Xcode other than `/Applications/Xcode.app`: `XCODE_EXTRA_PATH=-16.2` (uses `/Applications/Xcode-16.2.app`). + +## CI (GitHub Actions and Jenkins) + +The generator asks which CI templates to add (`CI_PROVIDER=github|jenkins|both|none` to skip the question). Existing files are never overwritten. +Templates live in `templates/` and call the same lanes you run locally, so CI and your machine behave the same. + +| File created | What it does | +|---|---| +| `.github/workflows/ci.yml` | On every PR and push to main: `fastlane test`, uploads coverage and junit results as artifacts | +| `.github/workflows/distribute.yml` | Manual run (Actions tab): sends a build to Firebase or TestFlight with `fastlane firebase` / `ship_testflight` | +| `Jenkinsfile` | `Setup` + `Build / test` (publishes junit and cobertura) + `Distribute`, chosen with the `INTENT` parameter | + +Secrets for distribution (GitHub repository secrets / Jenkins credentials): -2. Seriously... grab the requirements above or it will be harder to setup this later +| Name | Content | +|---|---| +| `MATCH_PASSPHRASE` | Passphrase of the match repo | +| `MATCH_GIT_BASIC_AUTHORIZATION` | GitHub only: base64 of `user:token` with read access to the match repo | +| `ASC_KEY_ID`, `ASC_ISSUER_ID`, `ASC_KEY_P8` | App Store Connect API key (content of the `.p8`, so the key is never committed) | +| `FIREBASE_TOKEN` | Firebase distribution only, from `firebase login:ci` | -3. Clone your project repository +The generated Fastfile calls `iq_asc_api_key` in `before_all`, which uses those `ASC_*` env vars when present and falls back to the key file for local runs. `build` and `test` never need the key. -4. Create the xcode project on the root of the repository. - - Create an iOS project - - Choose an existing bunddle id in dev portal - - Close it before moving to step 3. +## App Store / TestFlight + +- (Optional) Write devices to `fastlane/devices.txt`, or download them with + `bundle exec ruby ../xcode-scripts/samples/read_devices_from_developer_portal.rb` (needs username/password credentials, not API keys). +- Create the profiles: `bundle exec fastlane update_devices_and_profiles` +- (Optional) Unset "Automatically manage signing" and select the new profiles for each configuration. +- Ship: `bundle exec fastlane ship_testflight` + +## Firebase App Distribution (optional) + +- Add Firebase with SPM following console.firebase.com (add `GoogleService-Info.plist`, etc.). + If Crashlytics symbol upload fails with SPM, set `UPLOAD_SYMBOLS_PATH` (see the commented example in `scripts/build.sh`). +- Enable beta distribution on the Firebase website. +- Add the plugin: `bundle exec fastlane add_plugin firebase_app_distribution` +- Locally, be logged in with `firebase login`. +- Ship: `bundle exec fastlane firebase` + +## Other usual setup (`samples/`) + +| Script | Purpose | +|---|---| +| `add_adhoc_config.rb` | Adds an AdHoc config copying Release (then select profiles and point `iq_firebase_v2` to `adhoc`) | +| `add_appstore_config.rb` | Adds an AppStore config | +| `add_swiftlint_build_phase.rb` / `add_swiftformat_build_phase.rb` | Adds the lint/format build phase | +| `add_bundleversion_build_phase.rb` | Build number build phase (done by the generator) | +| `add_settings_bundle.rb`, `add_fluent.rb` | Settings bundle / Fluent localization | +| `read_devices_from_developer_portal.rb` | Downloads registered devices into `fastlane/devices.txt` | + +Run them with `bundle exec ruby ../xcode-scripts/samples/