Jenkins integration
How to integrate Codemagic into your Jenkins workflows using the Codemagic REST API
Jenkins is a popular open source automation server used to set up CI/CD pipelines for mobile and web apps. If your development team uses Jenkins, you can trigger Codemagic builds from Jenkins jobs using the Codemagic REST API.
Create a Codemagic API key
You need a Codemagic API key to authenticate REST API requests from Jenkins. Every Codemagic account has an API key available.
- Log into Codemagic.
- Go to Teams → Personal account → Integrations → Codemagic API
- Click Show and copy the
codemagic-api-key.
Find your Application ID
The Application ID uniquely identifies your app in Codemagic API calls.
- Navigate to your app in Codemagic
- Look at the app URL: https://codemagic.io/app/xxxxxxxxxxxxxxxxxxxxxxxx
- Copy the UUID at the end - this is your
AppID.
Find you Workflow name
The Workflow name specifies which workflow to trigger.
- Navigate to your codemagic.yaml file (in Codemagic or in your repository)
- Copy the name of the workflow (e.g.,
sample-workflow) - Use this name as WORKFLOW_NAME in API calls
workflows:
sample-workflow:
name: Codemagic Sample WorkflowConfiguring access to Codemagic in Jenkins
One credential needs to be added to Jenkins for the Codemagic integration: codemagic-api-key.
- Go to Manage Jenkins → Credentials → System → Global credentials.
- Click Add Credentials.
- Set Kind to Secret text
- Set Scope to Global
- Set Secret to
your-codemagic-api-key - Set ID to codemagic-api-key.
- Click OK.
Trigger a Codemagic build
A Pipeline job can be used to send a request to Codemagic with information about which app, workflow, and branch to build.
Learn more about required parameters: Builds API documentation
The following is an example of how to perform a request that triggers a Codemagic build:
pipeline {
agent any
environment {
CODEMAGIC_APP_ID = 'YOUR_APP_ID' // change to your AppID
CODEMAGIC_WORKFLOW = 'YOUR_WORKFLOW_NAME' // change to your Workflow Name
CODEMAGIC_BRANCH = 'YOUR_BRANCH_NAME' // change to your branch name
}
stages {
stage('Trigger Codemagic build') {
steps {
withCredentials([string(credentialsId: 'codemagic-api-key', variable: 'CODEMAGIC_API_KEY')]) {
script {
def response = sh(
script: """
curl -s -o /dev/null -w "%{http_code}" \
-X POST \
-H "Content-Type: application/json" \
-H "x-auth-token: \$CODEMAGIC_API_KEY" \
-d '{
\"appId\": \"${CODEMAGIC_APP_ID}\",
\"branch\": \"${CODEMAGIC_BRANCH}\",
\"workflowId\": \"${CODEMAGIC_WORKFLOW}\"
}' \
https://api.codemagic.io/builds
""",
returnStdout: true
).trim()
if (response == '200') {
echo "Codemagic build triggered for branch ${CODEMAGIC_BRANCH}."
} else {
error "Codemagic API returned HTTP ${response}"
}
}
}
}
}
}
}Replace:
YOUR_APP_IDwith your Codemagic app ID.YOUR_WORKFLOW_NAMEwith your workflow nameYOUR_BRANCH_NAMEwith your branch name
Security best practices
Store API keys as Jenkins credentials and use withCredentials to mask them in logs.
Rotate your Codemagic API key regularly.