|
| 1 | +name: argus-docker-build-prep |
| 2 | +description: Prepare for building a Docker Image for Argus |
| 3 | + |
| 4 | +inputs: |
| 5 | + path_filters: |
| 6 | + description: 'Glob patterns to match against changed files in the repository, comma delimited' |
| 7 | + required: false |
| 8 | + default: '**/*' |
| 9 | + path_filters_base: |
| 10 | + description: | |
| 11 | + Git reference (e.g. branch name) against which the changes will be detected. Defaults to the current branch. |
| 12 | + If it references same branch it was pushed to, changes are detected against the most recent commit before the push. |
| 13 | + This option is ignored if action is triggered by pull_request event. |
| 14 | + required: false |
| 15 | + default: ${{ github.ref }} |
| 16 | + branches_include: |
| 17 | + description: 'Branch names to run this job on, supports wildcards, comma delimited' |
| 18 | + required: false |
| 19 | + default: '*' |
| 20 | + branches_ignore: |
| 21 | + description: 'Branch names to run this job on, supports wildcards, comma delimited' |
| 22 | + required: false |
| 23 | + default: '' |
| 24 | + |
| 25 | +outputs: |
| 26 | + image_tag: |
| 27 | + description: A custom tag to apply to the images that are built |
| 28 | + value: ${{ steps.build_tags.outputs.image_tag }} |
| 29 | + should_build: |
| 30 | + description: Whether the job should run |
| 31 | + value: ${{ steps.final_check.outputs.should_build }} |
| 32 | + |
| 33 | +runs: |
| 34 | + using: composite |
| 35 | + steps: |
| 36 | + - name: Check for matching branch |
| 37 | + id: branch_filter |
| 38 | + uses: actions/github-script@v7 |
| 39 | + with: |
| 40 | + script: | |
| 41 | + function wildcardMatch(text, pattern) { |
| 42 | + const regexPattern = |
| 43 | + new RegExp('^' + pattern.replace(/\?/g, '.').replace(/\*/g, '.*') + '$'); |
| 44 | + return regexPattern.test(text); |
| 45 | + } |
| 46 | + const branchesInclude = `${{ inputs.branches_include }}`.split(',').map(b => b.trim()).filter(b => b.length > 0); |
| 47 | + console.log('Branches to run against:', branchesInclude); |
| 48 | + const branchesIgnore = `${{ inputs.branches_ignore }}`.split(',').map(b => b.trim()).filter(b => b.length > 0); |
| 49 | + console.log('Branches to ignore:', branchesIgnore); |
| 50 | + const branch = `${{ github.ref }}`.replace('refs/heads/', ''); |
| 51 | + const shouldRun = branchesInclude.some(b => wildcardMatch(branch, b)) && !branchesIgnore.some(b => wildcardMatch(branch, b)); |
| 52 | + if (shouldRun) { |
| 53 | + console.log('Job will run'); |
| 54 | + } else { |
| 55 | + console.log(`Job will be skipped because branch name "${branch}" does not match the filters`); |
| 56 | + } |
| 57 | + core.setOutput('match', shouldRun); |
| 58 | +
|
| 59 | + - uses: actions/checkout@v4 |
| 60 | + with: |
| 61 | + fetch-depth: 0 |
| 62 | + |
| 63 | + - name: Get build tag |
| 64 | + id: build_tags |
| 65 | + uses: actions/github-script@v7 |
| 66 | + with: |
| 67 | + script: | |
| 68 | + let sha; |
| 69 | + const eventName = context.eventName; |
| 70 | + if (eventName === "pull_request") { |
| 71 | + console.log('pull_request:', context.payload.pull_request.head.sha); |
| 72 | + sha = context.payload.pull_request.head.sha; |
| 73 | + } else if (eventName === "push") { |
| 74 | + console.log('push:', context.sha); |
| 75 | + sha = context.sha; |
| 76 | + } else { |
| 77 | + core.setFailed(`EventName ${eventName} not supported`); |
| 78 | + return; |
| 79 | + } |
| 80 | +
|
| 81 | + const imageTag = `sha-${sha.slice(0, 7)}`; |
| 82 | + if (imageTag === 'sha-') { |
| 83 | + core.setFailed('The image tag [${imageTag}] is invalid.'); |
| 84 | + } |
| 85 | +
|
| 86 | + console.log('imageTag:', imageTag); |
| 87 | + core.setOutput('image_tag', imageTag); |
| 88 | +
|
| 89 | + - name: Parse inputs |
| 90 | + id: parse_filters |
| 91 | + uses: actions/github-script@v7 |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const filters = `${{ inputs.path_filters }}`.split(',').map(f => f.trim()).filter(b => b.length > 0); |
| 95 | + const filtersStr = "run_on:\n" + filters.map(f => ` - '${f}'`).join('\n'); |
| 96 | + core.setOutput('filters', filtersStr); |
| 97 | +
|
| 98 | + - name: Check for force push |
| 99 | + id: force_push |
| 100 | + uses: actions/github-script@v7 |
| 101 | + with: |
| 102 | + # if the push was forced, use the default branch as the base -- otherwise, use the most recent commit before the push |
| 103 | + # this is necessary because when you force push the previous commit is not available in the repo, thus no changes can be detected |
| 104 | + script: | |
| 105 | + if (`${{ github.event_name }}` === 'push' && ${{ github.event.forced || false }}) { |
| 106 | + core.info(`Force push detected, using the repo's default branch (${{ github.event.repository.default_branch }}) as the base`) |
| 107 | + core.setOutput('base', `${{ github.event.repository.default_branch }}`); |
| 108 | + } else { |
| 109 | + core.info(`Push was not forced, using the most recent commit before the push as the base`) |
| 110 | + core.setOutput('base', `${{ inputs.path_filters_base }}`); |
| 111 | + } |
| 112 | +
|
| 113 | + - name: Check for matching file changes |
| 114 | + uses: dorny/paths-filter@v3 |
| 115 | + id: file_filter |
| 116 | + with: |
| 117 | + filters: | |
| 118 | + ${{ steps.parse_filters.outputs.filters }} |
| 119 | + base: ${{ steps.force_push.outputs.base }} |
| 120 | + list-files: json |
| 121 | + |
| 122 | + - name: Check if build should run |
| 123 | + id: final_check |
| 124 | + uses: actions/github-script@v7 |
| 125 | + with: |
| 126 | + script: | |
| 127 | + const branchMatched = `${{ steps.branch_filter.outputs.match }}` === 'true'; |
| 128 | + const filesMatched = `${{ steps.file_filter.outputs.run_on }}` === 'true'; |
| 129 | + core.setOutput('should_build', filesMatched && branchMatched); |
0 commit comments