Add cross-repo CI: trigger downstream tests on PR success #1161
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CommCare Core Build | |
| on: | |
| push: | |
| branches: | |
| - 'master' | |
| - 'formplayer' | |
| pull_request: | |
| branches: | |
| - 'master' | |
| - 'formplayer' | |
| - 'dmr/cross-repo-ci' | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| env: | |
| JAVA_VERSION: '17' | |
| JOB_GRADLE_VERSION: 8.1 | |
| FORMPLAYER_GRADLE_VERSION: 8.1 | |
| FORMPLAYER_JAVA_VERSION: '17' | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Set environment for Formplayer | |
| if: ${{ github.ref_name == 'formplayer' || github.base_ref == 'formplayer' }} | |
| run: | | |
| echo "JOB_GRADLE_VERSION=${{ env.FORMPLAYER_GRADLE_VERSION }}" >> "$GITHUB_ENV" | |
| echo "JAVA_VERSION=${{ env.FORMPLAYER_JAVA_VERSION }}" >> "$GITHUB_ENV" | |
| - name: Set up Java | |
| uses: actions/setup-java@v5 | |
| with: | |
| java-version: ${{ env.JAVA_VERSION }} | |
| distribution: 'adopt' | |
| - name: Set up Gradle | |
| uses: gradle/actions/setup-gradle@v5 | |
| with: | |
| gradle-version: ${{ env.JOB_GRADLE_VERSION }} | |
| - name: Build with Gradle | |
| run: gradle build | |
| trigger-downstream: | |
| needs: build | |
| if: github.event_name == 'pull_request' | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - repo: dimagi/commcare-android | |
| check_name: "cross-repo / commcare-android" | |
| workflow_file: commcare-android-pr-workflow.yml | |
| - repo: dimagi/formplayer | |
| check_name: "cross-repo / formplayer" | |
| workflow_file: build.yml | |
| steps: | |
| - name: Generate GitHub App token | |
| id: app-token | |
| uses: actions/create-github-app-token@v1 | |
| with: | |
| app-id: ${{ secrets.CROSS_REPO_CI_APP_ID }} | |
| private-key: ${{ secrets.CROSS_REPO_CI_PRIVATE_KEY }} | |
| owner: dimagi | |
| repositories: commcare-core,commcare-android,formplayer | |
| - name: Set pending status | |
| uses: actions/github-script@v7 | |
| env: | |
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
| CHECK_NAME: ${{ matrix.check_name }} | |
| REPO: ${{ matrix.repo }} | |
| WORKFLOW_FILE: ${{ matrix.workflow_file }} | |
| with: | |
| github-token: ${{ steps.app-token.outputs.token }} | |
| script: | | |
| await github.rest.repos.createCommitStatus({ | |
| owner: 'dimagi', | |
| repo: 'commcare-core', | |
| sha: process.env.HEAD_SHA, | |
| state: 'pending', | |
| context: process.env.CHECK_NAME, | |
| description: 'Waiting for downstream tests...', | |
| target_url: `https://github.com/${process.env.REPO}/actions/workflows/${process.env.WORKFLOW_FILE}` | |
| }); | |
| - name: Trigger downstream workflow | |
| uses: actions/github-script@v7 | |
| env: | |
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
| MERGE_SHA: ${{ github.sha }} | |
| COMMCARE_CORE_PR: ${{ github.event.pull_request.number }} | |
| CHECK_NAME: ${{ matrix.check_name }} | |
| REPO: ${{ matrix.repo }} | |
| WORKFLOW_FILE: ${{ matrix.workflow_file }} | |
| with: | |
| github-token: ${{ steps.app-token.outputs.token }} | |
| script: | | |
| try { | |
| const [owner, repo] = process.env.REPO.split('/'); | |
| await github.rest.actions.createWorkflowDispatch({ | |
| owner, | |
| repo, | |
| workflow_id: process.env.WORKFLOW_FILE, | |
| ref: 'dmr/cross-repo-ci-merge-sha', | |
| inputs: { | |
| commcare_core_sha: process.env.MERGE_SHA, | |
| commcare_core_pr: process.env.COMMCARE_CORE_PR, | |
| commcare_core_check_name: process.env.CHECK_NAME, | |
| commcare_core_status_sha: process.env.HEAD_SHA | |
| } | |
| }); | |
| } catch (error) { | |
| // If dispatch fails, set error status so it doesn't stay pending forever | |
| await github.rest.repos.createCommitStatus({ | |
| owner: 'dimagi', | |
| repo: 'commcare-core', | |
| sha: process.env.HEAD_SHA, | |
| state: 'error', | |
| context: process.env.CHECK_NAME, | |
| description: `Failed to trigger: ${error.message}`.substring(0, 140) | |
| }); | |
| throw error; | |
| } |