Skip to content

Commit 9ffb727

Browse files
add action (#1)
1 parent aca86eb commit 9ffb727

8 files changed

Lines changed: 490 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Trigger Codemagic workflow
2+
==========================
3+
4+
GitHub Action to trigger a workflow on Codemagic.
5+
6+
Quick start
7+
-----------
8+
9+
Add the following configuration to `.github/workflows/main.yml` to trigger Codemagic build on any push event.
10+
11+
on: push
12+
13+
jobs:
14+
trigger-codemagic-build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Trigger Codemagic build
18+
uses: codemagic-ci-cd/trigger-codemagic-workflow-action@v1.0.0
19+
with:
20+
app-id: <MY-APPLICATION-ID>
21+
workflow-id: <MY-WORKFLOW-ID>
22+
token: ${{ secrets.CODEMAGIC_API_TOKEN }}
23+
24+
Arguments
25+
---------
26+
27+
| Argument | Description |
28+
| ------------- | ------------------------------------------------------------------------------------------------- |
29+
| `app-id` | [Application ID](https://docs.codemagic.io/rest-api/applications/) [**required**] |
30+
| `workflow-id` | [Workflow ID](https://docs.codemagic.io/rest-api/builds/) [**required**] |
31+
| `token` | [API token](https://docs.codemagic.io/rest-api/codemagic-rest-api/#authentication) [**required**] |
32+
| `branch` | GitHub event branch override |
33+
| `tag` | GitHub event tag override |
34+
| `labels` | Build labels, one label per line |
35+
| `xcode` | Xcode version e.g. 14.1 |
36+
| `flutter` | Flutter version e.g. 3.3.3 |
37+
| `cocoapods` | CocoaPods version e.g. 0.29.0 |
38+
| `node` | Node version e.g. 16 |
39+
| `npm` | NPM version e.g. 8.19.2 |
40+
| `ndk` | NDK version e.g. 25.1.8937393 |
41+
| `java` | Java version e.g. 18 |
42+
| `ruby` | Ruby version e.g. 3.1.2 |
43+
44+
Note that branch and tag names are inferred from the event that triggered the action. `branch` or `tag` arguments will override the values from the event. `tag` argument takes precedence over `branch` if both arguments are provided.
45+
46+
Environment variables
47+
---------------------
48+
49+
Use the `CM_` prefix to pass environment variables to Codemagic builds.
50+
51+
For example, define the `CM_FOO` variable in the GitHub Action step configuration:
52+
53+
env:
54+
CM_FOO: bar
55+
56+
The corresponding variable `FOO` (without the `CM_` prefix) will be available during the Codemagic build.
57+
58+
Output variables
59+
----------------
60+
61+
Output variables can be used later in the action steps:
62+
63+
- name: Build ID
64+
run: echo "${{ steps.build.outputs.build-id }}"
65+
66+
| Output variable | Description |
67+
| ------------------ | ------------------------------------------------- |
68+
| `build-id` | Codemagic build ID |
69+
| `build-status-url` | Build status API endpoint |
70+
| `build-html-url` | Build page, requires Codemagic account for access |
71+

action.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Trigger Codemagic workflow
2+
description: GitHub Action to trigger a workflow on Codemagic
3+
branding:
4+
icon: star
5+
color: purple
6+
7+
inputs:
8+
app-id:
9+
description: Codemagic application ID
10+
required: true
11+
workflow-id:
12+
description: Codemagic workflow ID
13+
required: true
14+
token:
15+
description: Codemagic API token
16+
required: true
17+
branch:
18+
description: GitHub event branch override
19+
tag:
20+
description: GitHub event tag override
21+
labels:
22+
description: Build labels
23+
xcode:
24+
description: Xcode version
25+
flutter:
26+
description: Flutter version
27+
cocoapods:
28+
description: CocoaPods version
29+
node:
30+
description: Node version
31+
npm:
32+
description: NPM version
33+
ndk:
34+
description: NDK version
35+
java:
36+
description: Java version
37+
ruby:
38+
description: Ruby version
39+
outputs:
40+
build-id:
41+
description: Codemagic build ID
42+
build-api-url:
43+
description: Build details API endpoint
44+
build-url:
45+
description: Build page on Codemagic
46+
runs:
47+
using: node16
48+
main: dist/index.js
49+

dist/index.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import axios from 'axios'
2+
3+
import {
4+
getInput,
5+
setFailed,
6+
getMultilineInput,
7+
setOutput,
8+
} from '@actions/core'
9+
import { context } from '@actions/github'
10+
11+
const appId = getInput('app-id')
12+
const workflowId = getInput('workflow-id')
13+
const token = getInput('token')
14+
15+
const labels = getMultilineInput('labels')
16+
17+
let branch = getInput('branch')
18+
let tag = getInput('tag')
19+
20+
if (!branch && !tag) {
21+
const { ref } = context
22+
branch = ref.split('refs/heads/')[1] || process.env.GITHUB_HEAD_REF
23+
tag = ref.split('refs/tags/')[1]
24+
}
25+
26+
const variables = Object.keys(process.env).reduce((variables, variable) => {
27+
if (variable.startsWith('CM_')) {
28+
variables[variable.substring(3)] = process.env[variable]
29+
}
30+
return variables
31+
}, {})
32+
33+
const softwareVersions = [
34+
'xcode',
35+
'flutter',
36+
'cocoapods',
37+
'node',
38+
'npm',
39+
'ndk',
40+
'java',
41+
'ruby',
42+
].reduce((softwareVersions, software) => {
43+
const version = getInput(software)
44+
if (version) {
45+
softwareVersions[software] = version
46+
}
47+
return softwareVersions
48+
}, {})
49+
50+
const url = 'https://api.codemagic.io/builds'
51+
52+
const payload = {
53+
appId,
54+
workflowId,
55+
labels,
56+
branch,
57+
tag,
58+
environment: { variables, softwareVersions },
59+
}
60+
61+
const headers = {
62+
'x-auth-token': token,
63+
}
64+
65+
try {
66+
const response = await axios.post(url, payload, { headers })
67+
68+
const { buildId } = await response.data
69+
setOutput('build-id', buildId)
70+
setOutput('build-status-url', `https://api.codemagic.io/builds/${buildId}`)
71+
setOutput('build-html-url', `https://codemagic.io/app/${appId}/build/${buildId}`)
72+
} catch (error) {
73+
let details = null
74+
if (error.response.status === 403) {
75+
details = 'Either app ID or token is incorrect'
76+
} else {
77+
details = error.response.data.error
78+
}
79+
setFailed(`${error.message}: ${details}`)
80+
}
81+

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "trigger-codemagic-workflow-action",
3+
"version": "1.0.0",
4+
"description": "GitHub Action to trigger a workflow on Codemagic",
5+
"main": "index.js",
6+
"repository": "git@github.com:codemagic-ci-cd/trigger-codemagic-workflow-action.git",
7+
"author": "Artemii Yanushevskyi <artemii@nevercode.io>",
8+
"license": "MIT",
9+
"private": false,
10+
"type": "module",
11+
"dependencies": {
12+
"@actions/core": "^1.10.0",
13+
"@actions/github": "^5.1.1",
14+
"axios": "^1.1.3"
15+
},
16+
"devDependencies": {
17+
"@vercel/ncc": "^0.34.0"
18+
}
19+
}
20+

0 commit comments

Comments
 (0)