Unity CI/CD Demystified: Part 3: Build with Linux

Published on

Last Updated on

Estimated Reading Time: 2 min

In Part 1, we finished the setup to start creating our CI/CD pipeline.

In Part 2, we created a workflow to run our automated tests based on some trigger events.

In Part 3, we will create a reusable workflow to build the Unity project for various platforms.

The jobs in this workflow will run on a Linux VM.

Why Linux?

Its the cheapest box. As we will see in later posts, different platforms need a different OS for deployment. I am looking at you iOS.

Creating a new workflow definition .github/workflows/buildWithLinux.yml:

Defining the Workflow

name: Build with Linux  
on:  
  workflow_call:   
    inputs:   
      platform:  
        required: true  
        type: string  
      secrets:  
        UNITY_EMAIL:  
          required: true  
        UNITY_PASSWORD:  
          required: true  
        UNITY_LICENSE:  
          required: true  
    outputs:  
      buildVersion:  
        value: ${{ jobs.buildWithLinux.outputs.buildVersion }} 
  • on: ISpecify the events that trigger the workflow.
    • workflow_call: Set the workflow to be called from other workflows.
    • inputs: Values passed in from the caller workflow .
      • platform: Platform for which the Unity project should be built.
    • secrets: Secrets passed in from the caller workflow.
    • outputs: Data to pass back to the caller workflow.
      • buildVersion: The version generated by the job.

The Linux Build Job

jobs:  
  
  buildWithLinux:  
    name: Build in Linux for ${{ inputs.platform }}  
    runs-on: ubuntu-latest  
    outputs:  
      buildVersion: ${{ steps.build.outputs.buildVersion }}  
      steps:  
      - name: Checkout Repository  
        uses: actions/checkout@v4  
        with:  
          fetch-depth: 0  
          lfs: true  
  
      - name: Cache Library  
        uses: actions/cache@v4 
        with:  
          path: Library  
          key: Library-build-${{ inputs.platform }}  
          restore-keys: |  
            Library-build-${{ inputs.platform }}
            Library-    
      - name: Build Unity Project  
        id: build  
        uses: game-ci/unity-builder@v4  
        env:  
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}  
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}  
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}  
        with:  
          targetPlatform: ${{ inputs.platform }}  
  
      - name: Upload Build  
        uses: actions/upload-artifact@v4  
        with:  
          name: build-${{ inputs.platform }}  
          path: build/${{ inputs.platform }}

Here's a breakdown of what's happening in this job:

  • outputs: Data we want to return back to the caller workflow.
    • buildVersion: Version generated by the Builder in the build step below.
  • steps: This section contains a series of steps to be executed for this job.
    • Checkout Repository: Check out the code
      • fetch-depth: Get only the last commit of the branch.
      • lfs: Download files tracked by LFS.
    • Cache Library: Cache the "Library" directory, to make subsequent runs faster. The key is unique to each platform.
    • Build Unity Project: Build the Unity project for the target platform.
    • Upload Build: Upload the built Unity project as an artifact so that it can be downloaded in later jobs.

Conclusion

In this part, we created a reusable workflow to build the Unity project for different platforms. Next, we will use this workflow to build and deploy a WebGL build to GitHub pages.

Happy building!

References