Skip to content

Commit

Permalink
add action (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii-yanushevskyi authored Oct 28, 2022
1 parent aca86eb commit 9ffb727
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Trigger Codemagic workflow
==========================

GitHub Action to trigger a workflow on Codemagic.

Quick start
-----------

Add the following configuration to `.github/workflows/main.yml` to trigger Codemagic build on any push event.

on: push

jobs:
trigger-codemagic-build:
runs-on: ubuntu-latest
steps:
- name: Trigger Codemagic build
uses: codemagic-ci-cd/[email protected]
with:
app-id: <MY-APPLICATION-ID>
workflow-id: <MY-WORKFLOW-ID>
token: ${{ secrets.CODEMAGIC_API_TOKEN }}

Arguments
---------

| Argument | Description |
| ------------- | ------------------------------------------------------------------------------------------------- |
| `app-id` | [Application ID](https://docs.codemagic.io/rest-api/applications/) [**required**] |
| `workflow-id` | [Workflow ID](https://docs.codemagic.io/rest-api/builds/) [**required**] |
| `token` | [API token](https://docs.codemagic.io/rest-api/codemagic-rest-api/#authentication) [**required**] |
| `branch` | GitHub event branch override |
| `tag` | GitHub event tag override |
| `labels` | Build labels, one label per line |
| `xcode` | Xcode version e.g. 14.1 |
| `flutter` | Flutter version e.g. 3.3.3 |
| `cocoapods` | CocoaPods version e.g. 0.29.0 |
| `node` | Node version e.g. 16 |
| `npm` | NPM version e.g. 8.19.2 |
| `ndk` | NDK version e.g. 25.1.8937393 |
| `java` | Java version e.g. 18 |
| `ruby` | Ruby version e.g. 3.1.2 |

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.

Environment variables
---------------------

Use the `CM_` prefix to pass environment variables to Codemagic builds.

For example, define the `CM_FOO` variable in the GitHub Action step configuration:

env:
CM_FOO: bar

The corresponding variable `FOO` (without the `CM_` prefix) will be available during the Codemagic build.

Output variables
----------------

Output variables can be used later in the action steps:

- name: Build ID
run: echo "${{ steps.build.outputs.build-id }}"

| Output variable | Description |
| ------------------ | ------------------------------------------------- |
| `build-id` | Codemagic build ID |
| `build-status-url` | Build status API endpoint |
| `build-html-url` | Build page, requires Codemagic account for access |

49 changes: 49 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Trigger Codemagic workflow
description: GitHub Action to trigger a workflow on Codemagic
branding:
icon: star
color: purple

inputs:
app-id:
description: Codemagic application ID
required: true
workflow-id:
description: Codemagic workflow ID
required: true
token:
description: Codemagic API token
required: true
branch:
description: GitHub event branch override
tag:
description: GitHub event tag override
labels:
description: Build labels
xcode:
description: Xcode version
flutter:
description: Flutter version
cocoapods:
description: CocoaPods version
node:
description: Node version
npm:
description: NPM version
ndk:
description: NDK version
java:
description: Java version
ruby:
description: Ruby version
outputs:
build-id:
description: Codemagic build ID
build-api-url:
description: Build details API endpoint
build-url:
description: Build page on Codemagic
runs:
using: node16
main: dist/index.js

21 changes: 21 additions & 0 deletions dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
81 changes: 81 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import axios from 'axios'

import {
getInput,
setFailed,
getMultilineInput,
setOutput,
} from '@actions/core'
import { context } from '@actions/github'

const appId = getInput('app-id')
const workflowId = getInput('workflow-id')
const token = getInput('token')

const labels = getMultilineInput('labels')

let branch = getInput('branch')
let tag = getInput('tag')

if (!branch && !tag) {
const { ref } = context
branch = ref.split('refs/heads/')[1] || process.env.GITHUB_HEAD_REF
tag = ref.split('refs/tags/')[1]
}

const variables = Object.keys(process.env).reduce((variables, variable) => {
if (variable.startsWith('CM_')) {
variables[variable.substring(3)] = process.env[variable]
}
return variables
}, {})

const softwareVersions = [
'xcode',
'flutter',
'cocoapods',
'node',
'npm',
'ndk',
'java',
'ruby',
].reduce((softwareVersions, software) => {
const version = getInput(software)
if (version) {
softwareVersions[software] = version
}
return softwareVersions
}, {})

const url = 'https://api.codemagic.io/builds'

const payload = {
appId,
workflowId,
labels,
branch,
tag,
environment: { variables, softwareVersions },
}

const headers = {
'x-auth-token': token,
}

try {
const response = await axios.post(url, payload, { headers })

const { buildId } = await response.data
setOutput('build-id', buildId)
setOutput('build-status-url', `https://api.codemagic.io/builds/${buildId}`)
setOutput('build-html-url', `https://codemagic.io/app/${appId}/build/${buildId}`)
} catch (error) {
let details = null
if (error.response.status === 403) {
details = 'Either app ID or token is incorrect'
} else {
details = error.response.data.error
}
setFailed(`${error.message}: ${details}`)
}

20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "trigger-codemagic-workflow-action",
"version": "1.0.0",
"description": "GitHub Action to trigger a workflow on Codemagic",
"main": "index.js",
"repository": "[email protected]:codemagic-ci-cd/trigger-codemagic-workflow-action.git",
"author": "Artemii Yanushevskyi <[email protected]>",
"license": "MIT",
"private": false,
"type": "module",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"axios": "^1.1.3"
},
"devDependencies": {
"@vercel/ncc": "^0.34.0"
}
}

Loading

0 comments on commit 9ffb727

Please sign in to comment.