UNPKG

4.44 kBPlain TextView Raw
1// This script is called from travis to perform codepush builds and deployments
2import {
3 execSync
4} from 'child_process';
5import path from 'path';
6import fs from 'fs';
7import request from 'request';
8
9const appConfig = require(path.join(process.cwd(), 'env' , 'env'));
10const packageJson = require(path.join(process.cwd(), 'package.json'));
11const github = new (require('github'))({
12 version: '3.0.0',
13 timeout: 5000
14});
15
16// if codepush is not configured, exit
17if (!appConfig.codepush) {
18 process.exit(0);
19}
20
21// if travis is building a pull, exit
22if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
23 console.log('Codepush building ignored for pull request');
24 process.exit(0);
25}
26
27// ensure env vars exist
28if (!process.env.GITHUB_TOKEN) {
29 console.error('GITHUB_TOKEN env var is missing.');
30 process.exit(1);
31}
32
33if (!process.env.APPCENTER_TOKEN) {
34 console.error('APPCENTER_TOKEN env var is missing.');
35 process.exit(1);
36}
37
38github.authenticate({
39 type: 'basic',
40 username: process.env.GITHUB_TOKEN,
41 password: 'x-oauth-basic'
42});
43
44const USER = appConfig.codepush.user;
45const REPO = appConfig.codepush.repo;
46const currentHEADSha = execSync('git rev-parse HEAD').toString();
47
48if (!USER) {
49 console.error('Repo user not defined for codepush config');
50 process.exit(1);
51}
52
53if (!REPO) {
54 console.error('Repo key not defined for codepush config');
55 process.exit(1);
56}
57
58if (!currentHEADSha) {
59 console.error('Unable to get current SHA for repo');
60 process.exit(1);
61}
62
63// add react-native to dependencies list because the appcenter cli uses that to detect if
64// the project is react native
65packageJson.dependencies['react-native'] = 'meh';
66fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
67
68// build and release for each app property
69if (appConfig.codepush.android) {
70 try {
71 execSync(`appcenter codepush release-react -a ${appConfig.codepush.android.name} \
72-d Latest --development false --description ${currentHEADSha}`, {stdio: [0, 1, 2]});
73
74 tagDeploymentLabel(appConfig.codepush.android.name, 'AndroidBuild');
75
76 updateTag('LatestAndroidBuild', currentHEADSha, () =>
77 console.log(`Updated latest android build tag`));
78 } catch (e) {
79 console.error('Android codepush deployment failed to build');
80 console.error(e);
81 }
82}
83
84if (appConfig.codepush.ios) {
85 try {
86 execSync(`appcenter codepush release-react -a ${appConfig.codepush.ios.name} \
87--plist-file ./ios/${appConfig.name}/Info.plist -d Latest --development false \
88--description ${currentHEADSha}`, {stdio: [0, 1, 2]});
89
90 tagDeploymentLabel(appConfig.codepush.ios.name, 'iOSBuild');
91
92 updateTag('LatestIOSBuild', currentHEADSha, () => console.log(`Updated latest ios build tag`));
93 } catch (e) {
94 console.error('iOS codepush deployment failed to build');
95 console.error(e);
96 }
97}
98
99function tagDeploymentLabel(app: string, tagPrefix: string): void {
100 // this is async and its fine for script to continue
101 request({
102 url: `https://api.appcenter.ms/v0.1/apps/${app}/deployments/Latest`,
103 json: true,
104 headers: {
105 'X-API-Token': process.env.APPCENTER_TOKEN
106 }
107 }, (error, response, body) => {
108 if (!error && body && body.latest_release &&
109 body.latest_release.description === currentHEADSha
110 ) {
111 createTag(tagPrefix + body.latest_release.label, currentHEADSha, () =>
112 console.log('Successfully tagged deployment label'));
113 }
114 });
115}
116
117interface GithubError {
118 code: number;
119}
120
121function updateTag(tag: string, sha: string, cb: Function): void {
122 github.gitdata.getReference({
123 user: USER,
124 repo: REPO,
125 ref: 'tags/' + tag
126 }, (e: GithubError, d: string) => {
127 if (e && e.code === 404) {
128 // create tag
129 return createTag(tag, sha, cb);
130 }
131
132 if (d) {
133 github.gitdata.updateReference({
134 user: USER,
135 repo: REPO,
136 ref: 'tags/' + tag,
137 sha,
138 force: true
139 }, (e: GithubError, d: string) => {
140 if (e) {
141 console.log('tag update error', tag, sha);
142 console.log(e);
143 process.exit(1);
144 }
145 cb();
146 });
147 }
148 });
149}
150
151function createTag(tag: string, sha: string, cb: Function): void {
152 github.gitdata.createReference({
153 user: USER,
154 repo: REPO,
155 ref: 'refs/tags/' + tag,
156 sha
157 }, (e: GithubError, d: string) => {
158 if (e) {
159 console.log('tag create error', tag, sha);
160 console.log(e);
161 process.exit(1);
162 }
163 cb();
164 });
165}