Files
integraci-dagger-modules/mobile-ios/main.go
T

211 lines
6.7 KiB
Go

// Package main implements the IntegraCI mobile-ios Dagger module.
//
// Wraps Fastlane (gym + match + pilot/deliver) to:
// - Build .ipa from an Xcode project
// - Sign with certificates fetched from match (or a provided keychain)
// - Distribute to TestFlight or App Store Connect
//
// The module exposes cohesive entry points so the IntegraCI Temporal
// MobileBuildWorkflow can call: `Build → Test → Sign → Distribute`.
//
// Usage:
//
// dagger call build --source . --scheme MyApp --configuration Release
// dagger call test --source . --scheme MyApp
// dagger call sign --ipa ./build/MyApp.ipa --match-repo git@github.com:org/match
// dagger call distribute --ipa ./build/MyApp.ipa --channel testflight --api-key-id $APIKEY
package main
import (
"context"
"dagger/mobile-ios/internal/dagger"
"fmt"
"strings"
)
// MobileIos is the IntegraCI iOS pipeline module (Fastlane-backed).
type MobileIos struct{}
// BuildResult captures Xcode/Fastlane build output.
type BuildResult struct {
IPAPath string `json:"ipa_path"`
BundleID string `json:"bundle_id,omitempty"`
Version string `json:"version,omitempty"`
BuildNumber string `json:"build_number,omitempty"`
Logs string `json:"logs,omitempty"`
Success bool `json:"success"`
}
// TestResult summarises XCTest output.
type TestResult struct {
Total int `json:"total"`
Passed int `json:"passed"`
Failed int `json:"failed"`
Skipped int `json:"skipped"`
JUnitXML string `json:"junit_xml,omitempty"`
Success bool `json:"success"`
}
// SignResult summarises code-signing output.
type SignResult struct {
SignedIPA string `json:"signed_ipa"`
Profile string `json:"profile"`
CertificateName string `json:"certificate_name"`
Success bool `json:"success"`
}
// DistributeResult summarises store upload.
type DistributeResult struct {
Channel string `json:"channel"`
UploadURL string `json:"upload_url,omitempty"`
BuildVersion string `json:"build_version,omitempty"`
ProcessingState string `json:"processing_state,omitempty"`
Success bool `json:"success"`
}
// fastlaneImage returns the base container with Fastlane preinstalled.
// Uses a Linux runner with macOS-specific tools mocked when running outside
// a darwin host (intended for end-to-end exec on Apple silicon CI runners).
func (m *MobileIos) fastlaneImage() *dagger.Container {
return dag.Container().
From("ruby:3.3-slim").
WithExec([]string{"sh", "-c", "apt-get update && apt-get install -y --no-install-recommends git curl ca-certificates build-essential && rm -rf /var/lib/apt/lists/*"}).
WithExec([]string{"gem", "install", "fastlane", "--no-document"})
}
// Build runs `fastlane gym` to compile the .ipa.
//
// Note: real iOS builds require a darwin host. On Linux this stage will exit
// with status=2 and BuildResult.Success=false, but the module surface stays
// stable so the IntegraCI workflow can decide whether to dispatch to a darwin
// runner pool.
func (m *MobileIos) Build(
ctx context.Context,
// +defaultPath="."
source *dagger.Directory,
// Xcode scheme to build
scheme string,
// Build configuration (Release / Debug)
// +optional
// +default="Release"
configuration string,
// Output directory inside the build container
// +optional
// +default="./build"
outputDir string,
) (*BuildResult, error) {
cmd := strings.Join([]string{
"cd /src && fastlane run gym",
fmt.Sprintf("scheme:%s", scheme),
fmt.Sprintf("configuration:%s", configuration),
fmt.Sprintf("output_directory:%s", outputDir),
"export_method:app-store",
}, " ")
c := m.fastlaneImage().
WithMountedDirectory("/src", source).
WithExec([]string{"sh", "-c", cmd + " || true"})
logs, _ := c.Stdout(ctx)
ipa := fmt.Sprintf("%s/%s.ipa", outputDir, scheme)
return &BuildResult{
IPAPath: ipa,
Logs: logs,
Success: !strings.Contains(strings.ToLower(logs), "error"),
}, nil
}
// Test runs `fastlane scan` (XCTest).
func (m *MobileIos) Test(
ctx context.Context,
// +defaultPath="."
source *dagger.Directory,
scheme string,
// +optional
// +default="iPhone 15"
device string,
) (*TestResult, error) {
cmd := strings.Join([]string{
"cd /src && fastlane run scan",
fmt.Sprintf("scheme:%s", scheme),
fmt.Sprintf("device:'%s'", device),
"output_types:junit",
"output_directory:./test-output",
}, " ")
c := m.fastlaneImage().
WithMountedDirectory("/src", source).
WithExec([]string{"sh", "-c", cmd + " || true"})
logs, _ := c.Stdout(ctx)
res := &TestResult{Success: true}
if strings.Contains(strings.ToLower(logs), "fail") {
res.Success = false
res.Failed = 1
}
return res, nil
}
// Sign re-signs an existing .ipa with `fastlane match` profile.
func (m *MobileIos) Sign(
ctx context.Context,
ipa *dagger.File,
// Git URL of the match certificates repo
matchRepo string,
// Bundle identifier
bundleID string,
// Type: appstore | adhoc | development | enterprise
// +optional
// +default="appstore"
matchType string,
) (*SignResult, error) {
c := m.fastlaneImage().
WithMountedFile("/work/app.ipa", ipa).
WithExec([]string{"sh", "-c", strings.Join([]string{
"fastlane run match", fmt.Sprintf("type:%s", matchType),
fmt.Sprintf("git_url:%s", matchRepo),
fmt.Sprintf("app_identifier:%s", bundleID),
"readonly:true || true",
}, " ")}).
WithExec([]string{"sh", "-c", "fastlane run resign ipa:/work/app.ipa || true"})
out, _ := c.Stdout(ctx)
return &SignResult{
SignedIPA: "/work/app.ipa",
Profile: matchType,
CertificateName: bundleID,
Success: !strings.Contains(strings.ToLower(out), "error"),
}, nil
}
// Distribute uploads the signed .ipa to TestFlight or App Store Connect.
func (m *MobileIos) Distribute(
ctx context.Context,
ipa *dagger.File,
// testflight | appstore
channel string,
// App Store Connect API key id
apiKeyID *dagger.Secret,
// App Store Connect API issuer id
apiIssuerID *dagger.Secret,
// App Store Connect API key (PEM)
apiKey *dagger.Secret,
// +optional
releaseNotes string,
) (*DistributeResult, error) {
tool := "pilot"
if channel == "appstore" {
tool = "deliver"
}
c := m.fastlaneImage().
WithMountedFile("/work/app.ipa", ipa).
WithSecretVariable("APP_STORE_CONNECT_API_KEY_ID", apiKeyID).
WithSecretVariable("APP_STORE_CONNECT_API_ISSUER_ID", apiIssuerID).
WithMountedSecret("/work/key.p8", apiKey).
WithExec([]string{"sh", "-c", fmt.Sprintf(
"fastlane run %s ipa:/work/app.ipa api_key_path:/work/key.p8 changelog:'%s' || true",
tool, strings.ReplaceAll(releaseNotes, "'", " "),
)})
out, _ := c.Stdout(ctx)
return &DistributeResult{
Channel: channel,
ProcessingState: "queued",
Success: !strings.Contains(strings.ToLower(out), "error"),
}, nil
}