How to automate CodePush release/update process using Github Actions

Tharindu Ramesh Ketipearachchi
4 min readJun 10, 2022

--

In our previous articles, we have discussed what is CodePush and how to integrate CodePush pointed to multiple development environments in a single application. But when we are looking at the way we should do a CodePush release, still we have to do it from our personal machine using the CodePush CLI. The main drawback of this method is, if someone has mistakenly changed their package.json file or some of your 3rd party iOS or android dependencies has been updated, with the CodePush update those updates are going to your production application as well. Since CodePush is only supports for Js file changes, your production app is definitely going to crash. These 3rd party updates can happen with a high possibility because if we encounter any build issues on our application, we frequently change those dependencies in order to get those build issues sorted out. Those are the most common suggested fixes when you google it or look in to solutions on the stack-overflow.

The best way to avoid such a mistakes and do a safe CodePush update is to automate the CodePush update process. When we automate this process using virtual environment, we can ignore any changes being made to our podfile.lock from the git commit phase. Then VM do the rest for you and your CodePush release will going to be safer than ever.

Here, we are going to discuss about how to automated CodePush release process using Github Actions. We are going to use the same ENV_DEV, ENV_TEST and ENV_PROD AppCenter channels that we’ve been using throughout our whole article series. In this article we’ll explain how to automate your CodePush release for ENV_TEST channel. But this will be same for all other channels as well.

01. Create a workflow in Github Actions

First we need to create a workflow in Github Actions in order to add our automations script. You can do it in a same way which we have done in our previous articles. But this time name your yml file as codepush-test.yml

02. Add AppCenter API token to Github Secrets

In order to access AppCenter channels in our yml script, we need to save AppCenter API token in our Github secrets. Here we have explained how to do that. Please keep in mind, you have to do this for your all three AppCenter channels.

03. Build the workflow script

As we did in all other scripts, we are going to trigger our pipeline using a Github tag. You already know that we release CodePush updates targeting particular released version. We need to get that target version dynamically using this tag as well. To full-fill all above requirements, we are using the following tag format to trigger our CodePush pipeline.

CP/vX.Y.Z/vi

where vX.Y.Z is our targeted app version and i said to be our CodePush release version for particular targeted app version.

So let’s begin our automation script as follows

name: Codepush-TEST on:
push:
tags:
- ‘CP/v[0–9]+.[0–9]+.[0–9]+/v[0–9]+’

Then we need to declare our app names and token as environment variables. Please keep in mind your app name should be in organization_name/app_name format.

env:
APP_CENTER_TOKEN: ${{ secrets.APP_CENTER_TOKEN_MYAPP_ANDROID_TEST }}
APP_NAME: ${{ ‘MyApp-Android/ENV_TEST’ }}
APP_CENTER_TOKEN: ${{ secrets.APP_CENTER_TOKEN_MYAPP_IOS_TEST }}
APP_NAME: ${{ ‘MyApp-iOS/ENV_TEST’ }}

Here we are going to achieve our full process using 3 different jobs. The first job we’ll get our Github tag, split it and derive our targeted app version using that. The second one we’ll do the CodePush release for android and last one we’ll do the same for iOS. To pass the variable values through different jobs we are using special variable type called outputs. Read this to know more information about outputs.

First we open our jobs section. Define our first job to derive version and set defines jobs outcome as output. Remind that you need to define all of your output variables at the beginning of the particular job.

jobs:

getversion:
name: Get Version
runs-on: ubuntu-latest
outputs:
target_version: ${{ steps.setoutput.outputs.target_version }}

Then we are splitting our tag and derive the version from it. For this we are using Github action plugin called Split Actions.

steps:  - name: Get current tag
id: ctag
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
- uses: jungwinter/split@v2
id: stag
with:
msg: ${{ steps.ctag.outputs.tag }}
separator: “/v”
- name: Set Output
id: setoutput
env:
TG_VER: ${{steps.stag.outputs._1}}
run: |
echo “::set-output name=target_version::$TG_VER”

04. CodePush update for android

Now we are defining a new job for android update. We’ll be doing a npm install before the update.

update-android:
name: Update Android
needs: getversion
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Dependencies
run: npm install

Now we are going to do a CodePush update. For this we are also using specific plugin called Github Actions for CodePush. Using this we will be able to call the CodePush CLI command with all the provided arguments in a same manner.

- name: Codepush Update
uses: NishanthShankar/codepush-action@master
with:
args: release-react -a ${{ env.APP_NAME_ANDROID }} -d Production -t ${{needs.getversion.outputs.target_version}}
env:
APPCENTER_ACCESS_TOKEN: ${{ env.APP_CENTER_TOKEN_ANDROID }}

04. CodePush update for iOS

Now we are going to push the CodePush update to iOS. This is exactly similar to the way that we did it for android. Only the AppName and tokens are going to be changed.

update-ios:
name: Update iOS
needs: getversion
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Dependencies
run: npm install
- name: Deploy to Codepush
uses: NishanthShankar/codepush-action@master
with:
args: release-react -a ${{ env.APP_NAME_IOS }} -d Production -t ${{needs.getversion.outputs.target_version}}
env:
APPCENTER_ACCESS_TOKEN: ${{ env.APP_CENTER_TOKEN_IOS }}

Now we have completed the full automation process. Here is the full script.

Cheers! Now you have automated release process for CodePush as well. Go ahead and automate the things for prod and dev environments as well.

--

--

Tharindu Ramesh Ketipearachchi

Technical Lead (Swift, Objective C, Flutter, react-native) | iOS Developer | Mobile Development Lecturer |MSc in CS, BSc in CS (Col)