|
| 1 | +name: 'Setup Maven with JFrog or Cache' |
| 2 | +description: | |
| 3 | + Configures Maven for builds. For same-repo PRs and main branch pushes, |
| 4 | + authenticates to JFrog Artifactory via OIDC token exchange. For forked PRs, |
| 5 | + restores the Maven dependency cache from the default branch (no credentials). |
| 6 | +
|
| 7 | +inputs: |
| 8 | + is-fork: |
| 9 | + description: 'Whether this is a forked PR (true/false). Caller should compute this.' |
| 10 | + required: true |
| 11 | + |
| 12 | +runs: |
| 13 | + using: 'composite' |
| 14 | + steps: |
| 15 | + # --- Forked PR path: restore cache only, no JFrog credentials --- |
| 16 | + - name: Restore Maven cache (fork) |
| 17 | + if: inputs.is-fork == 'true' |
| 18 | + uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 |
| 19 | + with: |
| 20 | + path: ~/.m2/repository |
| 21 | + key: ${{ runner.os }}-maven-deps-${{ hashFiles('**/pom.xml') }} |
| 22 | + restore-keys: ${{ runner.os }}-maven-deps- |
| 23 | + |
| 24 | + - name: Configure Maven without credentials (fork) |
| 25 | + if: inputs.is-fork == 'true' |
| 26 | + shell: bash |
| 27 | + run: | |
| 28 | + mkdir -p ~/.m2 |
| 29 | + cat > ~/.m2/settings.xml << 'SETTINGS_EOF' |
| 30 | + <settings> |
| 31 | + <mirrors> |
| 32 | + <mirror> |
| 33 | + <id>jfrog-central</id> |
| 34 | + <mirrorOf>*</mirrorOf> |
| 35 | + <url>https://databricks.jfrog.io/artifactory/db-maven/</url> |
| 36 | + </mirror> |
| 37 | + </mirrors> |
| 38 | + <!-- No <servers> block: forked PRs use cached dependencies only. |
| 39 | + Any cache miss will produce a clear HTTP 401 error indicating |
| 40 | + the dependency is not in cache. Ask a maintainer to run the |
| 41 | + "Warm Maven Dependency Cache" workflow with your PR number. --> |
| 42 | + </settings> |
| 43 | + SETTINGS_EOF |
| 44 | +
|
| 45 | + echo "Maven configured for forked PR (cache-only, no JFrog credentials)" |
| 46 | +
|
| 47 | + # --- Same-repo path: full JFrog OIDC authentication --- |
| 48 | + - name: Get JFrog OIDC token |
| 49 | + if: inputs.is-fork == 'false' |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + set -euo pipefail |
| 53 | +
|
| 54 | + # Get GitHub OIDC ID token |
| 55 | + ID_TOKEN=$(curl -sLS \ |
| 56 | + -H "User-Agent: actions/oidc-client" \ |
| 57 | + -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ |
| 58 | + "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=jfrog-github" | jq .value | tr -d '"') |
| 59 | + echo "::add-mask::${ID_TOKEN}" |
| 60 | +
|
| 61 | + # Exchange for JFrog access token |
| 62 | + ACCESS_TOKEN=$(curl -sLS -XPOST -H "Content-Type: application/json" \ |
| 63 | + "https://databricks.jfrog.io/access/api/v1/oidc/token" \ |
| 64 | + -d "{\"grant_type\": \"urn:ietf:params:oauth:grant-type:token-exchange\", \"subject_token_type\":\"urn:ietf:params:oauth:token-type:id_token\", \"subject_token\": \"${ID_TOKEN}\", \"provider_name\": \"github-actions\"}" | jq .access_token | tr -d '"') |
| 65 | + echo "::add-mask::${ACCESS_TOKEN}" |
| 66 | +
|
| 67 | + if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" = "null" ]; then |
| 68 | + echo "FAIL: Could not extract JFrog access token" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "JFROG_ACCESS_TOKEN=${ACCESS_TOKEN}" >> "$GITHUB_ENV" |
| 73 | +
|
| 74 | + echo "JFrog OIDC token obtained successfully" |
| 75 | +
|
| 76 | + - name: Configure Maven with JFrog credentials |
| 77 | + if: inputs.is-fork == 'false' |
| 78 | + shell: bash |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | +
|
| 82 | + mkdir -p ~/.m2 |
| 83 | + cat > ~/.m2/settings.xml << EOF |
| 84 | + <settings> |
| 85 | + <mirrors> |
| 86 | + <mirror> |
| 87 | + <id>jfrog-central</id> |
| 88 | + <mirrorOf>*</mirrorOf> |
| 89 | + <url>https://databricks.jfrog.io/artifactory/db-maven/</url> |
| 90 | + </mirror> |
| 91 | + </mirrors> |
| 92 | + <servers> |
| 93 | + <server> |
| 94 | + <id>jfrog-central</id> |
| 95 | + <username>gha-service-account</username> |
| 96 | + <password>${JFROG_ACCESS_TOKEN}</password> |
| 97 | + </server> |
| 98 | + </servers> |
| 99 | + </settings> |
| 100 | + EOF |
| 101 | +
|
| 102 | + echo "Maven configured to use JFrog registry" |
| 103 | +
|
| 104 | + - name: Cache Maven packages |
| 105 | + if: inputs.is-fork == 'false' |
| 106 | + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 |
| 107 | + with: |
| 108 | + path: ~/.m2 |
| 109 | + key: ${{ runner.os }}-maven-deps-${{ hashFiles('**/pom.xml') }} |
| 110 | + restore-keys: ${{ runner.os }}-maven-deps- |
0 commit comments