A Gitlab CI file for React Native Apps

Cover Image

CI/CD

Git

Gitlab CI

Example

https://docs.gitlab.com/ee/ci/introduction

.gitlab-ci.yml

setup

image: node:8

stages:
- pre
- test
- publish
- post

cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/

variables:
DOCKER_DRIVER: overlay2

before_script:
- yarn generate-dotenv
- yarn
// package.json

{
"name": "stegappasaurus",
"scripts": {
"generate-dotenv": "sh util/generate-dotenv.sh > .env",
}
}
# util/generate-dotenv.sh

#!/usr/bin/env bash

cat << EOF
BUGSNAG_API_KEY=${BUGSNAG_API_KEY}
CAT_API_KEY=${CAT_API_KEY}
EOF
BUGSNAG_API_KEY=1232541
CAT_API_KEY=abxc-71379991

jobs

pre

close:issue:
image: registry.gitlab.com/gitlab-automation-toolkit/gitlab-auto-close-issue
stage: pre
before_script: []
only:
- master
script:
- apk add --no-cache --upgrade grep
- ISSUE=$(echo $CI_COMMIT_MESSAGE | grep -oP "(?<=Fixes \#)[0-9]+" || echo '1')
- gitlab_auto_close_issue --issue $ISSUE --remove-label "Doing" --remove-label "To Do"
create:merge-request:
image: registry.gitlab.com/gitlab-automation-toolkit/gitlab-auto-mr
stage: pre
before_script: []
except:
- production
- master
- tags
script:
- gitlab_auto_mr -t master -c WIP -d .gitlab/merge_request_templates/merge_request.md -r -s --use-issue-name
<!-- merge_request.md -->

# Description

<!-- please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. -->

## Type

- [ ] Bug Fix
- [ ] Improvement
- [ ] New Feature

Fixes #<!-- Issue Number -->

test

lint:
stage: test
only:
- merge_requests
except:
variables:
- $CI_COMMIT_REF_NAME =~ /^master/
script:
- yarn lint
lint:code-formatter:
stage: test
only:
- merge_requests
except:
variables:
- $CI_COMMIT_REF_NAME =~ /^master/
script:
- yarn code-formatter-check
lint:types:
stage: test
only:
- merge_requests
except:
variables:
- $CI_COMMIT_REF_NAME =~ /^master/
script:
- yarn types-check
tests:unit:
stage: test
only:
- merge_requests
except:
variables:
- $CI_COMMIT_REF_NAME =~ /^master/
script:
- yarn tests --silent
tests:coverage:
stage: test
only:
- master
script:
- yarn coverage --silent
coverage: /All\sfiles.*?\s+(\d+.\d+)/

publish

#!/usr/bin/env bash

cat << EOF
android.useAndroidX=true
android.enableJetifier=true
org.gradle.jvmargs=-Xms1g
MYAPP_RELEASE_STORE_FILE=stegappasaurus.keystore
MYAPP_RELEASE_STORE_PASSWORD=${ANDROID_KEYSTORE_PASSWORD}
MYAPP_RELEASE_KEY_ALIAS=${ANDROID_KEYSTORE_ALIAS}
MYAPP_RELEASE_KEY_PASSWORD=${ANDROID_KEYSTORE_KEY_PASSWORD}
EOF
android {
signingConfigs {
release {
if (project.hasProperty("MYAPP_RELEASE_STORE_FILE")) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
}
#!/usr/bin/env bash

echo $CI_COMMIT_TAG

if [[ $CI_COMMIT_TAG == *"alpha"* ]]; then
echo "Publishing Package: Alpha"
cd android && ./gradlew publish --track alpha
elif [[ $CI_COMMIT_TAG == *"beta"* ]]; then
echo "Publishing Package: Beta"
cd android && ./gradlew publish --track beta
elif [[ $CI_COMMIT_TAG == *"release"* ]]; then
echo "Publishing Package: Production"
cd android && ./gradlew publish --track production
else
echo "Publishing Package: Internal"
cd android && ./gradlew publish --track internal
fi
echo "Publishing Package: Production" 
cd android && ./gradlew publish --track production
publish:android:package:
stage: publish
image: reactnativecommunity/react-native-android
only:
- /^release/.*$/
script:
- echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf && sysctl -p
- cd android
- base64 -d $ANDROID_KEYSTORE > app/stegappasaurus.keystore
- export VERSION=$(cat app.json | jq -r .version)
- cat $PLAY_STORE_JSON > app/play-store.json
- cat $FIREBASE_GOOGLE_SERVICES_JSON > app/google-services.json
- yarn generate-licenses
- yarn generate-gradle-properties
- yarn publish-package --no-daemon
artifacts:
paths:
- ./android/app/build/
- ./android/app/src/main/assets/

post

create:gitlab:release:
image: registry.gitlab.com/gitlab-automation-toolkit/gitlab-auto-release
stage: post
only:
- /^release/.*$/
except:
variables:
- $CI_COMMIT_TAG =~ /beta/
- $CI_COMMIT_TAG =~ /alpha/
before_script: []
script:
- gitlab_auto_release -c CHANGELOG.md -d "This was auto-generated by the gitlab-auto-release tool, https://gitlab.com/gitlab-automation-toolkit/gitlab-auto-release." --artifacts "publish:android:package"
publish:bugsnag:soucemaps:
stage: post
only:
- /^release/.*$/
script:
- apt update && apt install -y jq
- export VERSION=$(cat app.json | jq -r .version)
- curl https://upload.bugsnag.com/react-native-source-map -F apiKey=${BUGSNAG_API_KEY} -F appVersion=${VERSION} -F dev=false -F platform=android -F sourceMap=@android/app/src/main/assets/index.map -F bundle=@android/app/src/main/assets/index.bundle -F projectRoot=`pwd`
- yarn run bugsnag-sourcemaps upload --api-key=${BUGSNAG_API_KEY} --app-version=${VERSION} --minifiedFile=android/app/build/generated/assets/react/release/index.android.bundle --source-map=android/app/build/generated/sourcemaps/react/release/index.android.bundle.map --minified-url=index.android.bundle --upload-sources
dependencies:
- publish:android:package

other

pages:
only:
- master
before_script:
- yarn
script:
- yarn docs-build
- mv .docz/dist/* public/
artifacts:
paths:
- public

Appendix

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Haseeb Majid

Haseeb Majid

Software Engineer | Pythonista | Typescripter | Docker Advocate | https://haseebmajid.dev