Join Kevin Ford for an in-depth discussion in this video Using custom build scripts, part of Visual Studio App Center for Developers.
- [Instructor] A common thing that needs to change as part of a build are elements of the iOS, info p-list, or Android manifest. We can do this with custom scripts. For this, we need a good text editor. A normal development tool like Visual Studio for Mac will work fine. Here we're going to go in and create a new file by going to file, new file and we're going to select an empty text file. And for the name, we're going to call this appcenter-pre-build because you want this to happen right before the build.
And since it's running on a Mac, we'll give it an sh extension and click new. Keep in mind if this is running on Windows, we'd have to use a .ps1 extension for PowerShell scripts. What we are going to do is change the CFBundleDisplayName to include the branch name if a QA branch is being built. This is the name that is displayed next to the icon so this is one way we can easily see that we have a QA build. Inside the script, we're going to initialize this as a bash script so we'll key in pound bang forward slash user forward slash bin env and we'll start off with bash and we're going to check if this is a QA branch so if space and we'll put a couple spaces in here and we're going to use a variable that comes as part of the app center called APPCENTER_BRANCH and this will return to us the current branch that it's being built against and we're going to check to see if that is equal to QA.
If so, what we want to do is we want to call a tool called PlistBuddy. By default, this is in the usr/libexec folder under plistbuddy and we're going to tell it to set the CFBundleDisplayName. This is the key that we're setting. And we're going to set it equal to VSACXamarin dot and then we're going to give it once again this build environment variable called APPCENTER_BRANCH.
And finally, we're going to tell it what file to change and that's in the iOS folder called info.plist and we'll close up our if statement. And I just like getting rid of this last trailing line. Now I'm just going to go in and save the file. I'll go file and save and I want to make sure this is in the same level as the solution that's being built and that is under the source folder so I'll just hit save right here.
Before using our scripts in the build, we have to make sure to upload the script to source control. Remember, the script needs to exist in the branch that needs it, in this case the QA branch. So if I look in Sourcetree, I can see over on the left-hand side you have the little round circle next to the QA branch. This indicates the active branch. So I'm going to click on uncommitted changes here and I can see that it just added in one file which is my source appcenter-pre-build. So I'll click commit, click on this one file to commit it in and I'll add a comma here, add pre-build script and press commit.
Back in Visual Studio App Center, we can see that a build is running based on the check in we just did. However, this is one of the items that we need to modify and re-save before it is picked up by the build definition. So to do that, I'm going to click over here on the right to this little gear that appears and we can see under build scripts, it picked up the fact that there is now a pre-build file in source control. So I'm just going to save this and build again and this is going to do a new build that actually goes in and uses our pre-build script.
So I'll click save and build. We're going to go over and click on the QA branch and see our builds and we'll wait a couple minutes and see if our build worked successfully. And here we can see our build was completed successfully. You may notice I have an extra build that was done. Sometimes as we go through the class, I may need to do an offline build as I've done here and so you may notice a new build will show up in the build list. So if we want to make sure our script was picked up after the build, we can actually go in and look at the build logs.
So if we click on our latest build here and click download, we can download the build or download the build logs. The build will give us in this case the IPA and the logs will give us the logs from the build. So now that the zip file has downloaded, now that we have the file downloaded, we can click on the arrow, go to show in finder, and double click on the downloaded zip file.
Then we open that up and here we have our build text that will show us our build output. So I'm going to open that up and from here we can search in the file for pre-build and here we can see that our build script is starting and running so it definitely picked up our build script and ran it.
- Adding applications to the VSAC
- Using custom build scripts
- Creating iOS and Android builds
- Creating UI tests
- Automating tests
- Distributing apps
- Examining crash logs and analytics
- Adding push notifications
- Using CodePush for deployments