|
| 1 | +--- |
| 2 | +name: Integration Tests |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - "action.yaml" |
| 7 | + - ".github/workflows/integration-tests.yaml" |
| 8 | + push: |
| 9 | + paths: |
| 10 | + - "action.yaml" |
| 11 | + - ".github/workflows/integration-tests.yaml" |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: integration-tests-${{ github.event_name == 'pull_request' && 'pr' || 'push' }}-${{ github.event.pull_request.head.sha || github.sha }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + filter-matrix: |
| 19 | + name: Filter Matrix |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + test-json: ${{ steps.filter.outputs.test-json }} |
| 23 | + cleanup-json: ${{ steps.filter.outputs.cleanup-json }} |
| 24 | + steps: |
| 25 | + - name: Filter Matrix |
| 26 | + id: filter |
| 27 | + shell: bash |
| 28 | + run: | |
| 29 | + # Remove any entries with keys containing `null` values. |
| 30 | + test_yaml="$(yq 'map(select(to_entries | map(.value != null) | all))' <<<"${matrix:?}")" |
| 31 | +
|
| 32 | + # Validate we do not accidentally test against the same package and commit SHA. |
| 33 | + yq -o=json <<<"$test_yaml" | jq -e '(map({package, "commit-sha"}) | unique | length) == length' || exit 1 |
| 34 | +
|
| 35 | + # Automatically cleanup the `cache-sha-*` tags for the specific test commits. |
| 36 | + cleanup_yaml="$(yq 'group_by(.package) | map({"package": .[0].package, "tags" : map(.commit-sha) | unique | map("cache-sha-" + .) | join(",")})' <<<"$test_yaml")" |
| 37 | +
|
| 38 | + # Output our multiline YAML document using GH action flavored heredoc |
| 39 | + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings |
| 40 | + { |
| 41 | + echo "test-json<<EOF" |
| 42 | + yq -o json <<<"$test_yaml" |
| 43 | + echo "EOF" |
| 44 | +
|
| 45 | + echo "cleanup-json<<EOF" |
| 46 | + yq -o json <<<"$cleanup_yaml" |
| 47 | + echo "EOF" |
| 48 | + } | tee -a "$GITHUB_OUTPUT" |
| 49 | + env: |
| 50 | + # We need to avoid running concurrent tests using the same commit SHA and |
| 51 | + # writing to the same image-repository. If we do not then we could see false |
| 52 | + # positive test results if one of them doesn't actually push cache layers. We |
| 53 | + # address this problem by: |
| 54 | + # |
| 55 | + # 1. Ensuring tests which run in parallel either use separate image repositories |
| 56 | + # or different Git commit SHAs. |
| 57 | + # 2. Utilizing concurrency groups to avoid having multiple instances of this |
| 58 | + # workflow run in parallel when triggered on the same commit SHA. |
| 59 | + # 3. Deleting the `cache-sha-*` tags to ensure our running workflow produced |
| 60 | + # those images. Ideally, we'd delete these before the tests run but attempting |
| 61 | + # to delete images from non-existing packages causes failures so this works |
| 62 | + # well enough. |
| 63 | + # |
| 64 | + # I also considered revising the action to avoid pushing images entirely. |
| 65 | + # Doing this may be challenging in otherways as pushing the image is a |
| 66 | + # requirement for getting the digests in some contexts: |
| 67 | + # https://github.com/docker/build-push-action/issues/906#issuecomment-1674567311 |
| 68 | + matrix: | |
| 69 | + - title: ${{ github.event_name == 'pull_request' && 'Merge Commit' || '' }} |
| 70 | + package : temporary/whalesay-pr |
| 71 | + commit-sha: ${{ github.sha }} |
| 72 | + from-scratch: false |
| 73 | + - title: Head Commit |
| 74 | + package: temporary/whalesay-${{ github.event_name == 'pull_request' && 'pr' || 'push' }} |
| 75 | + commit-sha: ${{ github.event.pull_request.head.sha || github.sha }} |
| 76 | + from-scratch: false |
| 77 | + - title: ${{ github.event_name == 'pull_request' && 'Merge Commit From Scratch' || '' }} |
| 78 | + package: temporary/whalesay-pr-from-scratch |
| 79 | + commit-sha: ${{ github.sha }} |
| 80 | + from-scratch: true |
| 81 | + - title: Head Commit From Scratch |
| 82 | + package: temporary/whalesay-${{ github.event_name == 'pull_request' && 'pr' || 'push' }}-from-scratch |
| 83 | + commit-sha: ${{ github.event.pull_request.head.sha || github.sha }} |
| 84 | + from-scratch: true |
| 85 | +
|
| 86 | + test: |
| 87 | + name: Test ${{ matrix.test.title }} |
| 88 | + needs: filter-matrix |
| 89 | + # These permissions are needed to: |
| 90 | + # - Checkout the repo |
| 91 | + permissions: |
| 92 | + contents: read |
| 93 | + packages: write |
| 94 | + runs-on: ubuntu-latest |
| 95 | + strategy: |
| 96 | + fail-fast: false |
| 97 | + matrix: |
| 98 | + test: ${{ fromJSON(needs.filter-matrix.outputs.test-json) }} |
| 99 | + steps: |
| 100 | + - name: Job started at |
| 101 | + id: job-started |
| 102 | + run: | |
| 103 | + job_started_at="$(date --utc --iso-8601=seconds)" |
| 104 | + echo "at=$job_started_at" | tee -a "$GITHUB_OUTPUT" |
| 105 | + - uses: actions/checkout@v4 |
| 106 | + with: |
| 107 | + ref: ${{ matrix.test.commit-sha }} |
| 108 | + - name: Log in to the Container registry |
| 109 | + uses: docker/login-action@v3 |
| 110 | + with: |
| 111 | + registry: ghcr.io |
| 112 | + username: ${{ github.actor }} |
| 113 | + password: ${{ github.token }} |
| 114 | + - uses: ./ |
| 115 | + id: build |
| 116 | + with: |
| 117 | + image-repository: ghcr.io/beacon-biosignals/${{ matrix.test.package }} |
| 118 | + context: test |
| 119 | + build-args: | |
| 120 | + DEBIAN_VERSION=12.9 |
| 121 | + from-scratch: ${{ matrix.test.from-scratch || 'false' }} |
| 122 | + - name: Validate image works |
| 123 | + run: | |
| 124 | + docker pull "${{ steps.build.outputs.image }}" |
| 125 | + output="$(docker run "${{ steps.build.outputs.image }}")" |
| 126 | + if [[ "$(wc -l <<<"$output")" -lt 14 ]]; then |
| 127 | + echo "$output" |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + debian_version="$(docker run --entrypoint=/bin/cat "${{ steps.build.outputs.image }}" /etc/debian_version)" |
| 131 | + [[ "$debian_version" == "12.9" ]] || exit 2 |
| 132 | + - name: Layer created at |
| 133 | + id: layer-created |
| 134 | + run: | |
| 135 | + layer_created_at="$(docker run --entrypoint=/bin/cat "${{ steps.build.outputs.image }}" /etc/layer-created-at)" |
| 136 | + echo "at=$layer_created_at" | tee -a "$GITHUB_OUTPUT" |
| 137 | + # Test will fail if this is the first time the image was build in the image-repository |
| 138 | + - name: Validate layer caching |
| 139 | + if: ${{ matrix.test.from-scratch == false }} |
| 140 | + run: | |
| 141 | + [[ "$(date -d "$layer_created_at" +%s)" -lt "$(date -d "$job_started_at" +%s)" ]] || exit 1 |
| 142 | + env: |
| 143 | + job_started_at: ${{ steps.job-started.outputs.at }} |
| 144 | + layer_created_at: ${{ steps.layer-created.outputs.at }} |
| 145 | + - name: Validate no layer caching |
| 146 | + if: ${{ matrix.test.from-scratch == true }} |
| 147 | + run: | |
| 148 | + [[ "$(date -d "$layer_created_at" +%s)" -gt "$(date -d "$job_started_at" +%s)" ]] || exit 1 |
| 149 | + env: |
| 150 | + job_started_at: ${{ steps.job-started.outputs.at }} |
| 151 | + layer_created_at: ${{ steps.layer-created.outputs.at }} |
| 152 | + - name: Validate cache images |
| 153 | + run: | |
| 154 | + docker manifest inspect "${{ steps.build.outputs.image-repository }}:cache-sha-${{ matrix.test.commit-sha }}" |
| 155 | +
|
| 156 | + # Should only be skipped when workflow is triggered by a tag push |
| 157 | + if [[ -n "$branch" ]]; then |
| 158 | + docker manifest inspect "${{ steps.build.outputs.image-repository }}:cache-branch-${branch//[^[:alnum:]]/-}" |
| 159 | + fi |
| 160 | + env: |
| 161 | + branch: ${{ github.head_ref || (github.ref_type == 'branch' && github.ref_name) }} |
| 162 | + - name: Validate annotations |
| 163 | + run: | |
| 164 | + set -x |
| 165 | + json="$(docker manifest inspect "${{ steps.build.outputs.image }}")" |
| 166 | + [[ "$(jq -r '.annotations."org.opencontainers.image.revision"' <<<"$json")" == "${{ matrix.test.commit-sha }}" ]] || exit 1 |
| 167 | + - name: Validate docker/metadata-output environment variables are overwritten |
| 168 | + shell: bash |
| 169 | + run: | |
| 170 | + if [[ "$(printenv | grep '^DOCKER_METADATA_OUTPUT_' | grep -c '[^=]$')" -ne 0 ]]; then |
| 171 | + printenv | grep '^DOCKER_METADATA_OUTPUT_' |
| 172 | + exit 1 |
| 173 | + fi |
| 174 | +
|
| 175 | + cleanup: |
| 176 | + name: Cleanup (${{ matrix.cleanup.package }}) |
| 177 | + needs: |
| 178 | + - filter-matrix |
| 179 | + - test |
| 180 | + if: ${{ !cancelled() }} |
| 181 | + runs-on: ubuntu-latest |
| 182 | + strategy: |
| 183 | + fail-fast: false |
| 184 | + matrix: |
| 185 | + cleanup: ${{ fromJSON(needs.filter-matrix.outputs.cleanup-json || '[]') }} |
| 186 | + steps: |
| 187 | + - uses: dataaxiom/ghcr-cleanup-action@cd0cdb900b5dbf3a6f2cc869f0dbb0b8211f50c4 # v1.0.16 |
| 188 | + with: |
| 189 | + package: ${{ matrix.cleanup.package }} |
| 190 | + delete-tags: ${{ matrix.cleanup.tags }} |
| 191 | + - uses: dataaxiom/ghcr-cleanup-action@cd0cdb900b5dbf3a6f2cc869f0dbb0b8211f50c4 # v1.0.16 |
| 192 | + with: |
| 193 | + package: ${{ matrix.cleanup.package }} |
| 194 | + older-than: 1 day |
| 195 | + keep-n-tagged: 0 |
| 196 | + exclude-tags: branch-main,cache-branch-main |
0 commit comments