# Jenkins

### Prerequisites

Before setting up Jenkins integration, ensure you have:

* **Jenkins Server** - Running locally or on dedicated server
* **FinalRun API Key** - Obtain from your FinalRun dashboard
* **App Name** - The exact app name as it appears in your FinalRun dashboard (Apps section)
* **Git Repository** - Accessible by Jenkins

{% hint style="info" %}
**Note**: App names are **case-sensitive** and must match exactly as shown in the FinalRun dashboard.
{% endhint %}

{% embed url="<https://youtu.be/IqywnR-UcAo>" %}

### Add Finalrun API key to Secrets

1. Navigate to **Manage Jenkins** → **Manage Credentials**
2. Click **(global)** domain → **Add Credentials**
3. Configure:
   * **Kind**: Secret text
   * **Scope**: Global
   * **Secret**: Your FinalRun API key
   * **ID**: `finalrun-api-key`
   * **Description**: FinalRun API Key for CI/CD

### Jenkinsfile Configuration

Create a `Jenkinsfile` in your repository root:

```groovy
pipeline {
    agent any
    
    environment {
        FINALRUN_API_KEY = credentials('finalrun-api-key')
        // Use the exact app name as it appears in your FinalRun dashboard
        APP_NAME = 'My Android App'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
                script {
                    env.GIT_COMMIT_SHORT = sh(
                        script: "git rev-parse --short HEAD",
                        returnStdout: true
                    ).trim()
                }
            }
        }
        
        stage('Build APK') {
            steps {
                script {
                    sh '''
                        echo "Building Android APK..."
                        export PATH=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/platform-tools:$PATH
                        
                        # Grant execute permission to gradlew
                        chmod +x gradlew
                        
                        # Build APK (Debug)
                        ./gradlew assembleDebug
                        
                        # Verify APK was created
                        ls -la app/build/outputs/apk/debug/
                    '''
                }
            }
        }
        
        stage('Install FinalRun CLI') {
            steps {
                script {
                    sh '''
                        echo "Installing FinalRun CLI..."
                        rm -rf ~/.finalrun/bin/finalrun-cli
                        curl -Ls "https://get-cli.finalrun.app/install.sh" | bash
                        export PATH="$HOME/.finalrun/bin:$PATH"
                        
                        # Verify installation
                        which finalrun-cli
                        finalrun-cli --version
                        finalrun-cli --help
                    '''
                }
            }
        }
        
        stage('Run FinalRun Tests') {
            steps {
                script {
                    sh '''
                        echo "Running FinalRun tests..."
                        export PATH="$HOME/.finalrun/bin:$PATH"
                        
                        finalrun-cli execute \\
                            --api-key="${FINALRUN_API_KEY}" \\
                            --app="${APP_NAME}=app/build/outputs/apk/debug/app-debug.apk" \\
                            --description="Jenkins CI/CD - Build: ${BUILD_NUMBER}, Commit: ${GIT_COMMIT_SHORT}"
                    '''
                }
            }
        }
    }
    
    post {
        success {
            echo "✅ Pipeline completed successfully!"
        }
        
        failure {
            echo "❌ Pipeline failed!"
        }
        
        cleanup {
            cleanWs()
        }
    }
}
```
