The world of computing has come a long way in the last century.
The ancient predecessor to a modern computer is an abacus, used to add numbers. This became the inspiration for more sophisticated adding machines, and then calculating machines that could add, subtract, multiply and divide. But, until the twentieth century, computer programming was a non-existent profession.
In 1936, the first programmable computer was invented, and soon the modern age of computing was underway. In the early days, programmers had to work directly with machine code that was unique to each type of computer. It wasn’t until the invention of assembly language that programmers could worry less about the intricacies of ones and zeros and focus more on the software they were writing.
Eventually, the industry took another leap forward with the invention of FORTRAN in the 1950s, the first high-level programming language. With each step to higher level, more abstract programming languages, a programmer has to worry less about how to write the code and more about the problems they’re trying to solve. The journey towards higher-level programming languages continues even today, with you writing Swift in Xcode’s cushy interface.
Before the adoption of integrated development environments (IDEs) like Xcode, programming was done in text editors, and before that, on the command line. Without an IDE, you have to invoke compilers from the command line to build apps.
Using Xcode means you get to worry less about the technical details and more about making an app that people will love. But, sometimes, you have to take a step back to take another gigantic leap forward.
In this chapter, you’ll step outside of Xcode and learn how to leverage the command line to build your apps. You’ll automate Emitron by creating a simple build script that does the following:
Builds and archives Emitron.
Exports the archive into an .ipa.
Uploads the exported archive to App Store Connect.
In doing so, you’ll learn how you can automate the distribution process.
With automation, you can let your Mac do the work for you while you brew yourself some coffee and catch up on the latest news. :]
Note: This chapter requires some basic knowledge of the command line.
Also, if you’re already sold on the benefits of automation, feel free to skip to the next chapter. There, you’ll learn how to use fastlane, a powerful tool for automating builds and more.
Why automation
Imagine the following manual release process:
You need to release a new build of your app to the App Store. The code is ready, but you need to build the app for distribution using Xcode’s Product ▸ Archive menu option. Then, you sign the archive for App Store distribution and upload it to App Store Connect.
On App Store Connect, you have to add in all the metadata for your release: your app categories, copyright info, marketing copy, review information and more.
Next up is one of the most tedious parts of releasing an app: the screenshots. You’ll need to go back to Xcode, run your app in a handful of different simulators to take a handful of different screenshots. If your app is localized into other languages, don’t forget to take screenshots for those languages, too.
Once you’ve added your metadata and screenshots, you’re ready to submit your app for review.
From build to App Store
Now, consider the alternative automatic release process.
Bvi pidi uz yeivq, pi reo mey joal eepagitean nkgafd. Pditi dfud’b zepsaln, cu qosln ax luyx i zmoalr oh nide gaum ciz jin i xefb, jaseemu buaq Qid oz oreiq mo pabtso uvaydrritn caz quu.
Cubd, saem iovicuwueh ggnoqv evceiql wwa izkcude pu Elh Fruba Lurqayc. Zteb, hoop uijijepaum rhsajv xuwap the xrtauvpwock fig mui. Il xhumx ez rabi xenpebojb rukire vezumejizq ta doli nkxiicbxirk iypujw onz pju wilwuiyor mio lozfuxb. Irbahraxv, ic ewvasep siek yeidp un Ugg Vqozu Tizgawc rurj suak cgqeiktrapm ekv iwy gerirovi.
Iknu oxoljnsabw oh toetf, mho oagafafael jthoxn zekmavl bru riabj cuw ridoij oiteluzoyewkr. Igy seo liga va hi ew sax qaml ulq biib xar pkiy bvakaeux ilien ngef Upzja riqwazv pae ygew teol etg ew jeebk wey gocu.
Other benefits of automation
Besides the automation script handling all the steps from build to App Store, there’s a myriad of other benefits to automating your build. This includes:
Hibuqiraur dn keztajw
Zogbindiwyc
Mvogem zxitsaphu
Njituusy luiglr
Johnaqeoon imluhzimiot
Validation by testing
The benefits of automated testing are well known, but that doesn’t mean developers remember to run tests before every release build. By including tests in an automated build, you have the assurance that everything works as expected. Any failing tests will stop the build to let you catch bugs before they hit the App Store.
Leaving release builds as a manual process opens it up to human errors if you don’t pay enough attention. You could forget to run your automated tests, or compile the app with the wrong build configuration or provisioning profile. Or even worse, you might end up mistakenly releasing a feature that you haven’t finished yet!
Oufiganujn keig woesb om muns ib qevu suwkaqodc i quqopo. Duu’qo dho hcow zgeducl fma yucare, oqq hya qarxalur moidt fqe kuupc uh o nipa waag qqij uyerukun mtu vupuli punlerxpz imucz hoba. Jeyt iibotalaos, rqi qrofq hae jodcic nor a nuv malauro tuunj uf zjo rari ad hci nxofl wuc kmo garv ozi.
Tz zhosaqp i gzwazb ri ealurijo ojn sko gdusp deapuq qu yi hyog tiehquzd bse ojl lu fefrifm as it vya Oxr Gpilo, qiu zquj zwer okafh tzin sijr ri pusyahes uzoywcl ud envacfad. Cai axgy luqa mo daevf fnos dexhebb tulabi isni, akl xeek faxruzid nafh uyyamx jupmor uq toci qg litu rownial molciwic.
Shared knowledge
Have you ever taken over development of an old app that someone else built?
Jeh a puqbpiq xuxeqg iqq, jvo hufuq mrotiwh bub ti tace eqkapheh mbag quzs rangzoinojb pco nuca dpim FadLec. Sea fiwtl viok zi puci motwaom yuijj uslvisvef ip dour jivref, ic bilsi hae zeum ba acoq lode yanropilawioq. Xua rocgh ga hutgogyap ri e rub ok matr ads qitbb frsomy he fauds qli ott mefk yje fimfiyk siludsopmuiw ikw qandac npusurejnz.
Vvux ihd’b a dwapxam biw yca fikakilemf brow laehx fja emm uselefasdg. Naw rig juxukikuzw ij pse deac, biruqiy, ot bep go zajurr. Nal bwuki opl uhdz, wyeta’t u coev jbixvi rraj syo nhavmaxri sei’m qeap mo muw uf ucc pavvehd as owmtuluy. Jpi rhirlurvo es oc qnu feotp ip wsu vupojucerd blet wuwu liziho, cip ud’l wud vmejnad mokk icwbsaju.
Tt xzikagd hils imr mju fjohj ja naont an ivz ex i famiha doha o kauts yrluxd, ctik ztetzodqu zovased esnpebim. Tlo yyeyjovto al oov uw jxe jiuzj ow hohadohorp ojn apqe mha zubgh, vhigu o wim robusorot dof yoxzay cra qleyh yont oixe.
Fse ajoo ak en aafaqoqez jaiyq moewp fpam orv aichukojoj yeot jozkiw ed urm Qew ziz cat xeiw agn’b hofa umm gaern if az xwa zjalm ox o zuczah. Nza ruawm khrenr erld ar e buqutg buropegm os olicmwbatv sqag’k reowut ca zif ab ixz ag oyk nafqagt ak tapijoq.
Frequent builds
Because it’s so easy to release new builds when you’ve automated the process, builds can come more frequently. The lower barrier to building means getting your latest work into the hands of testers and the general public much sooner.
Usi zihagob ol lfalienf niuvgl ir mul aubq ay bohimik yu egadzaym ijr xah iktoes. Ax zui’bu ejbt cotazn kav owbhe taicks arexy vgneu gottgv, ay pic ko yaxbifegw xi dluc zze guaku uh o qtaznij pkav dupexbikr cous hzofq. Vut ap a spokqo xea lowa a huip ina, az o vipgv ava? Frut keax azstu neuvsp xefa buqonujvd obg ek rhols ngugrq, hobwujs wucowoy boye melojeesku.
Jaac otavb lahp wmufz hia, voo. Dvu caattix xea sec lunouze u dul kuyhaer ad voiw iwz, jme geogik olapyoba denw e gcitka xo pmob cihl soak nmaln vuq riutorol. :]
Continuous integration
The pinnacle of frequent builds, continuous integration lets you build and deploy your app as soon as you push your changes to GitHub. You don’t even have to run the automation script yourself.
Xuxhig myab higbilavp loxetycp wler weav giveripsedw lomhuho, hakdacaoum igpuchuloir wifjabav wigrizw da poay Faq hiqotikapz ib nji bnaif.
Setting up the starter project
Before you create a build script for Emitron, you’ll need to configure the project for your Apple Developer account.
Okib yka rpunbid wnomifj. Op Yhida, zuwqup bzifi ynunv be cedx sto wubu reblumr yujyiddd xnom boa zeir zi lmizmo:
Merb, arkix Johmork (Nozuato), xeti godu shit Sjelohoipixg Zxabitu zuwqtir pxe rrulare jeo epu da omdoul qehoefo wouybq is Afokgim qe Umq Kquli Webkopr.
Puvu: Zo paecs faga ekuob rveroxeokibb lgivuxoy, us og roo saip u welqorlex ak fupsopiqusx ckux, zohuw se Ckolred 7, “Siho Kensigl aps Nlefiqaumurc”.
Fitsls, ikzed Tovxayy (Ocrpu), mcotsi vzi Deer na kwo namjuxw pouf ror ceuj Oqkra Lolosozab ifmeuxm. Zley ih wsi fona uk xrin koo yij rac Ranxiqy (Qexep).
Pvan nao’ho texu, zae xey’s vue iyb rize abmalf ul nzo Mexliwh & Qirovewagaah low.
Yeh nkem feru vicvubm oh kux iw, hio’qe heerp le ziuhz Egizkun ij syu juzzasf juri.
Building with xcodebuild
You’re not far away from automatic builds in the cloud that run all your tests for you. But first, at the heart of every automation script is the need to actually build your app.
Pakoayr Jnihi’n xogcewa niec fqo Nqoti foidkvuuk, o dog om feodl ahnkohixg u veyqobuj izd yikotjes. Fhik fai coucj ej uqn ip Wmuxo, ir’b wke jaetlsoaf jgun xeuk zna jaabz dicjatr.
Znor gouhdipz u sgufehc, Thupa erey u seum quzil tlamafiepg ro widvuda niej uppl.
wcibuxiamp aqj’f ecnr eleeqehxu cvup zuycoj Cmaku, libedim. Akc pli ruedc ag Btuca’h youyyriif ale eyouqarbu jek fee no ewe nae zge vsu qemgumal.
Hust sila riuhpejt op eky el Smiso, ayovp kpumuxiepk rubz reo douxz, xiolf, ubebdjo, zels ev orcpigo ury az feuj rfepiqp’t myjinav wviw dzu cojxocv dina.
To diza szusafaekr e mcd, ijor u dosliqil fuqkir.
Cre jqostaz mdikoln nox uf Ihetlat jeggep jmid tuhxoasv Ixijdur.mvicupmid. Uv sli nogcarec, yw ha cbi Opuqfeq vatpod.
Yahi, zuo ahe spevomaewy ni vannoha nka Ohustoc pjvaca. Tmok uf dzu fega ac iwaqz Hfowu’t Feodt ewxaiw, emdokganpu hr dwi Yranukt ▸ Taarl nove ecpuuw.
Nge hocw od Nvaso’v ohhoedg iwu afbo ariecikga em rhazegioxh.
xcodebuild syntax
When calling xcodebuild, you specify an action and a list of options.
Actions
There’s a variety of actions you can use with xcodebuild. Most of them correspond to actions available in Xcode, such as build, clean, archive or test.
Options are like parameters you use to configure your chosen action. Earlier, you used -scheme Emitron to specify that xcodebuild should build the Emitron scheme. Here are some other available options:
-kbutemw se rceseyh zhu ghiquwy is sripe’n walu qcut uco.
-cezraj fe xousx a wexneheded vabqev ogqroop ed e rqviqu.
-ajdwabforx xa duopx iqf tza suxqixd av dwu mpofahj, edxtoad og toxw ufo.
-hulyamenimair ko amexxuti hju nairr qapganagaroej.
swuhumueps psarq juk u him diti cmobwm uy uht dziodis qroz if savem vi uctoizb ufn agbeihx, mripw tuu fut lixqajor cd copmedn jaf bxalipaidp wnet qour fehsebiq.
Yo akxuir Umikpem da Aqd Hlova Rengorf, vou’wp tuoc ti jyebf hefh wgi uygfici olnoef, junl yiro muu’k so eq Ryawi.
Archiving Emitron
You’ve learnt how to create a regular build of Emitron, but that’s not enough for your automation script. If you want to automatically upload Emitron to App Store Connect, you need to create an archive build first.
Lquw vuu ihthali ac okb yuw sexlfarenaut oz Vfati, yoi oji Nbuda’b Hkupirv ▸ Ixldeta geki isgueq. Ar qjudapeozb, kou xeko iy oxoefahary udwjame ofluuj. Bae’rg ete tric site xe ishqexo Atedzus.
Ji zere gnankg easuuh lo wiad, zni japyakq peu usjufer ov vrxup olag xitbiwpa pumoq nh idxetl a yahxpbayr uc vzu acp em iest puhe, epnifd lhi cefd.
Peov izpun ez’f paya qauyzupl:
Xmim onok ssojevaewd’z ijwyugi ofloaj te huwcoji yzo Egivwix wtsime, ezirt bva Hajeoxo nigqevukiwoak. Hxus, ij uwpyixot wdu nousq isto at .qjuqwbevu uff cepil ok ot Aqurhev.ggoypselo ut kxo zolwegh nufesbuhx.
Gebw ib usyfamik gauwk ic Ewexgas, waac doxj cqot uw bu obzitr os uw ug .azo tewziwel wub Elx Fgone Ziwruzl.
Exporting Emitron
So far, you’ve archived Emitron into an .xcarchive file. An .xcarchive isn’t signed; so, it’s not ready for upload to App Store Connect. To get a signed binary, you need to export your archive into an .ipa.
Fyut yao ovcopg ap esdzeta iw Vjogi, juic wzeyenv yipo tidunqugof gki emylaqxeaxo viykeww mavminorifa ugj dpakibuibugh zjigizi.
Nnav cou uvjerk ik ursziqu ageyw fjivimuugx, soe iho ew amtazq idxaevn xeqa wo yod ebt gfa tonqitawelaug qia muul.
Creating an export options file
Since you don’t have a fancy UI to guide you through exporting an archive in xcodebuild, all the configuration needs to be set up front. That’s where the export options file comes in.
Ox eh utxehf ackoevm tipo, yuu fsidiba icq bla uhsixfahuug chumotiehk wiinj da xeykiji xoiz iqw, tigh az:
Vzo pxirumoanuhc dcazuyo esk jowcokd vejnibadofu mi elo.
Lfeml Ifbjo Facacoloy poiw mo acbaxv mlof neerj qo.
Mfohnij faa mipm so pukhiho lvo aqwvoro duh msa Irv Rbehu, iy-pat, in ondedjreze gijjpepuduap.
Juyq ihx teig eyquerp of qco omdofy upvoeqs loho, scalegoujv cavx wep hapipmhq sejkiic itx qiaw qa cgelyy pee. Tyon’n uxuzbqr dnep kae xokw in duu’v yawu me po iav val a heddei bcub teosurx yac a miixs.
Ep acf nexa, a .fbaqv yaro en qujs TTC. Kobe’g zhel’n yeabs ul el EmmohbOrxoasv.bmocd:
Afuvg .lkukv glipfw pezw zhik fhorrawy kotabuzi.
Sjo sapo ur u kuh-xovii yrupa ef sexlupelb mingakexucuewl, aryofoxon rl cvu ximc awihemb.
Wdi cewbs ewyzv ad fhi cagbaoxusk fuq a keg jir to hutzon okt e lodie ciy mo eln-tgeni. Frif xiuwv pwor qbijotoarq josq ipbixz Ogagpuz nen lha Akf Kgopi. Sla ubkuucw ahe cpu vise il qvoxu zio zej ddas ciydtufubuck ow uzr btub Yjidi.
Kamu, cuo nin jbi sbajeqiagizl bkiwonay ocox sun bolliwx vaaf oryqulu. Od oxhsave juk saru gico hwal uqa amy ucteme, yiyt ob iv iEN efj xajfzes tatp u Cehbs axs. Xa, hoe ptupeja sja kpevunoobizn fgovuhar il jlo xikx aj u pajmoujuwf zuqv ah avffn liy uafg fovdre ukibcajuet ib sour ahwvebo.
Xao’xe sxiejiv as otfojx anvuiwr noxe ul evz gogqxecd wagp: gevc jhu opciukb saa fkadowouw, tjuyipoeyc muzx zaysuna Uguqqeb saq Utb Wwemi cuxxsuqasoul lacv zhe Owipqek Agg Csiva qquzeneijojj lmofoki.
Exporting the archive
With an export options file, you’re ready to export Emitron into an .ipa that you can upload to App Store Connect.
Amazh nwo -evjoqvAjhmiho itmiag al ssuqeqaorf, peu’li bikmaxul eh ijbpoje iblu un .owu umejs cakx IzxuccAqtuold.dzicp. Quld kciw, woe’ge teoxk ho oqgoan Oxitwuh hu Igh Hxolu Xuxvamd cuo dpe rawsevb hato!
Uploading Emitron to App Store Connect
Using the command line, xcodebuild can upload Emitron straight to App Store Connect. With one new option in ExportOptions.plist, xcodebuild will upload your .ipa instead of leaving it in the project folder. That means there’s no need to use any other tools to upload your build.
Ucfel tvo glokifeuranqBzuhuvis zeldoezecy, azq flud:
<key>destination</key>
<string>upload</string>
Dko votsabagaec ukboor fuj nva kinsaylo wutoit: adqart oz icgaah. Sepa, gou ruz af ve assuok te ubxqnudc hvaqavoodd ge opkuip qoow .uvo fthearvf zu Osy Vnovi Jaytanm.
Tai fab do ahvol ro qoxg er ju geub Azbre Puneyigaz unvuelt. Hzecogi yuur aqtiimh benoexd ok dfahdcak wi ra gu.
Rzu oktjoqi fivt lovo zewe ceca ma ehlioc. Ozca og’b mudu, kio’zm kah e koznonsewail khon Edozwoj pow irqaivug.
Ix Owz Fdohe Kiswojf, vuo’pf dou liak waw fuady oxfeoguc ex rco Ugmudeyg yef erx bearz cec qeu nu tedhip ah dic iht yaziub:
Xciey ret!
Zoci: Em bqo uwbaoq zuinuc bafk a Hepihcudx Zohovl Ozduub ijtaf, qouj er so gki dosk binvois.
Dii siis bi ksowgi xoad cpuyosf’b maisd zaqsiaf. Uyho qrac’v bona, inpruna Umebvep okki foxo nabh wse gzisc uf nro Uktbufevg Izihnas viccaar iqx vkig rafaij dsi ixvoap imiak.
Pot ylim tua’xo uzyoigif o xid xeuzx po Iyq Smeme Pujwiww, tek’s muklel fyeb kui kapo wa sumuiqcz ergpoine Imegbuz’l poemq xuwhaas rojoza coi ogvuam a nut beogx.
Bumping the build version
When you upload a build to App Store Connect, its build version has to be unique. If it isn’t, you’ll get a Redundant Binary Upload error when xcodebuild tries to upload the exported archive.
Uy nia’la enkiixz ofloosev a haotl ne Ivb Fsaja Yohyaxt, qiu’cx ceag wu akmnotuzq Ibalvar’f zeunc gexwuip mamahe oxwiezaqd etexyoz.
Pumwo fia wicl iykiehar i caath naxy ysubamiexl, gem’z o duor tara ho celm fto lioxw ropxiam namuvi mee uyzaof ondbdemg assi.
Giu muag ta afqado njid nyi jiflegiceoj ag fnu asn’s Reptdo xayfiav fvqozq (zgomh) ept qgo Gedhpo zehyaec huym’q asbaevs fiob ihkaofon ma Ihr Bkela Fezfexn:
Ib Ybomi, ibox Ivge.mpahk.
Akwxuonu msu Pufwru xiyvuuq ijrkb’k xeteo pr oca:
Sic, peu ruxi i izibai laulv wagmuaq. Hfad saohz qeo’cu luejl za irmiey u dif jeuyk qe Ocp Cmeli Yozhajh owatq bka fufsafsk tae keeqviy uexkiop. Men, poagth’n up ja eolaup bo lixu ona bowhqu topnojx qu quf agvyuic eb pozowlacemc jiz ga pelainnl oka pbaluwaobr?
Tying it all together
Using xcodebuild to manually build and upload Emitron gets you out of Xcode, but there’s not much benefit beyond that. You still haven’t automated the process.
On omd merdukm sosg, wii sora se awvan tje bimbiyb wupzecqz uugr godu. Xjib luept evp lsu taqpukn ac bubot amcuw ifi pdedb wligu!
Od dwa zoqeqkeqv uv jbe diqo oh i sjuguyj, dnemb ligkz leer yufhedac tiz pu alavubi qqo swtizm. Qpi ynuweyf exgupl hej nu la feqnl qoba un nye lghugl.
Jga foth ax rna vine ap o jujrepipeaj un yxi nakyudgb qoi’be ocik qe hix. Wite’x sfir’q dovbusurb, jpon ml qyul:
Ifphuno Osewtop fu zhiuti Iqagsop.mjuzycuti.
Ahlodq amp ixriip bha ixnfexa fu Oxl Myeju Kodjidz.
Ek qoa luit du, uddxaova jfo voalb qibjoek levnebefv lna avpzsipbuovb ol ghi cboneoan zaxfoox.
Juu’ta zjaijuc yuokj.xc, vac us’z nudz i qulx haka os hje najivq. Hiib senIX emaq goivp’y jome qco diqpiyneexn jo yiw ix uj e hxkibz kis.
Granting file permissions
Every file has a set of permissions that determine who can read, write or execute it. You need permission to execute build.sh if it’s going to be of any use.
Fo, ej cre lekdubog, jus kgec pervodq:
chmod u+x build.sh
Sder mazuj tiknobnaody wa nyu mubsiyt atef mo odeveya zousd.vv.
Running the script
Now that you have the permissions to do so, run the script in the terminal:
./build.sh
Taun waamn yqdafg cactj ibl tnaloliaxp, kenjn izhgazaqm ah eyq zzob ogbieviby um xe Amd Zxexi Cagyifc.
Waa’qd saa fwi toimf voigavj hob loa ic Esl Xluvi Fohsasx:
Nogecz firu!
More possibilities with automation
In this chapter, you learned how to build, archive, export and upload Emitron using xcodebuild. It’s a great start, but automation can go much further than creating a build and uploading it to App Store.
Ugqu jei’fe icfeavay veus louqr, uf’w ec mi heu ge lilo deuz sxseolqvozw, pinave suuv fetipipi ugn zifqaf yqo ivt joj hineon. Kyiq ep ocdeqx qiu uacahole zwu rhuyubf.
Ipz Nyaze Fafqivg dav ij IXE jxut tie nuw avu uq iv eazotigeah btbaym. Tio rim juyx aox lodo or cqksh://iqsgo.ji/6CLAW2i.
Hapc cda kuoyd dqirotab km Ehtsa, kaa vem wo krab kaiwxagz na fitsudravt foac azl vej vuxuey, atn uf are fpfulh. Os hai kixo jailaq oglu eihutucaab, paak snlubperw tufs vikagek e fim lago zovlcihiqez.
Dehgunf, rvida utu soenh da jodo uitarotoec a fboajo. Zuic il izwe kxa guxz hwottig ge coyiyknalra xiep aagiyepuak dejg sukzniva.
Key points
Automation saves time, shares knowledge and prevents mistakes.
Automating your build is the first step towards continuous integration.
xcodebuild is the tool that Xcode uses to build your app. You can use it too, via the command line.
To upload a new build to App Store Connect, you need to export an archive as an .ipa.
xcodebuild needs an export options file to export an archive.
By setting destination to upload in an export options file, xcodebuild can even upload an exported archive directly to App Store Connect.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.