|
1 | | -name: Build |
2 | | -on: [ push, pull_request, workflow_dispatch ] |
| 1 | +name: Build / Test / Push |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '**' |
| 7 | + workflow_dispatch: |
| 8 | + |
3 | 9 | env: |
4 | | - REGISTRY: ghcr.io |
| 10 | + DOCKER_METADATA_SET_OUTPUT_ENV: 'true' |
5 | 11 |
|
6 | 12 | jobs: |
7 | | - # TODO: DRY w/release.yml |
8 | | - setup: |
9 | | - runs-on: ubuntu-latest |
10 | | - |
| 13 | + build: |
| 14 | + runs-on: ${{ matrix.runner }} |
| 15 | + outputs: |
| 16 | + build-image-arm: ${{ steps.gen-output.outputs.image-arm64 }} |
| 17 | + build-image-x64: ${{ steps.gen-output.outputs.image-x64 }} |
| 18 | + strategy: |
| 19 | + fail-fast: false |
| 20 | + matrix: |
| 21 | + runner: |
| 22 | + - ubuntu-24.04 |
| 23 | + - ubuntu-24.04-arm |
11 | 24 | steps: |
12 | | - # See https://github.com/docker/build-push-action/blob/v2.10.0/TROUBLESHOOTING.md#repository-name-must-be-lowercase |
13 | | - - name: Sanitize image name |
14 | | - uses: actions/github-script@v6 |
15 | | - id: image-name |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Set up Docker Buildx |
| 29 | + uses: docker/setup-buildx-action@v3 |
| 30 | + |
| 31 | + - name: Login to GitHub Container Registry |
| 32 | + uses: docker/login-action@v3 |
16 | 33 | with: |
17 | | - result-encoding: string |
18 | | - script: return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase() |
| 34 | + registry: ghcr.io |
| 35 | + username: ${{ github.actor }} |
| 36 | + password: ${{ secrets.GITHUB_TOKEN }} |
19 | 37 |
|
20 | | - - name: Get short SHA |
| 38 | + - name: Docker meta |
| 39 | + id: meta |
| 40 | + uses: docker/metadata-action@v5 |
| 41 | + with: |
| 42 | + images: ghcr.io/${{ github.repository }} |
| 43 | + # note Specifies a single tag to ensure the default doesn't add more than one. |
| 44 | + # The actual tag is not used, this is just used to sanitize the registry name |
| 45 | + # and produce labels. |
| 46 | + tags: type=sha |
| 47 | + |
| 48 | + - name: Sanitize registry repository name |
| 49 | + id: get-reg |
21 | 50 | run: | |
22 | | - echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV |
| 51 | + echo "registry=$(echo '${{ steps.meta.outputs.tags }}' | cut -f1 -d:)" | tee -a "$GITHUB_OUTPUT" |
23 | 52 |
|
24 | | - outputs: |
25 | | - base_image_name: ${{ steps.image-name.outputs.result }} |
26 | | - build_image: ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }} |
| 53 | + - name: Build/push the arch-specific image |
| 54 | + id: build |
| 55 | + uses: docker/build-push-action@v6 |
| 56 | + with: |
| 57 | + # @todo GHA caching needs tuning, these tend not to hit. Perhaps switch to type=registry? |
| 58 | + cache-from: type=gha |
| 59 | + cache-to: type=gha,mode=max |
| 60 | + labels: ${{ steps.meta.outputs.labels }} |
| 61 | + provenance: mode=max |
| 62 | + sbom: true |
| 63 | + tags: ${{ steps.get-reg.outputs.registry }} |
| 64 | + outputs: type=image,push-by-digest=true,push=true |
| 65 | + |
| 66 | + - name: Write arch-specific image digest to outputs |
| 67 | + id: gen-output |
| 68 | + run: | |
| 69 | + echo "image-${RUNNER_ARCH,,}=${{ steps.get-reg.outputs.registry }}@${{ steps.build.outputs.digest }}" | tee -a "$GITHUB_OUTPUT" |
27 | 70 |
|
28 | | - build: |
29 | | - if: github.event_name != 'release' |
30 | | - needs: setup |
| 71 | + merge: |
| 72 | + runs-on: ubuntu-24.04 |
| 73 | + needs: |
| 74 | + - build |
31 | 75 | env: |
32 | | - BUILD_IMAGE: ${{ needs.setup.outputs.build_image }} |
33 | | - |
34 | | - runs-on: ubuntu-latest |
35 | | - |
36 | | - permissions: |
37 | | - packages: write |
38 | | - |
| 76 | + DOCKER_APP_IMAGE_ARM64: ${{ needs.build.outputs.build-image-arm }} |
| 77 | + DOCKER_APP_IMAGE_X64: ${{ needs.build.outputs.build-image-x64 }} |
| 78 | + outputs: |
| 79 | + build-image: ${{ steps.meta.outputs.tags }} |
39 | 80 | steps: |
40 | | - - name: Checkout repository |
41 | | - uses: actions/checkout@v3 |
| 81 | + - name: Checkout code |
| 82 | + uses: actions/checkout@v4 |
42 | 83 |
|
43 | | - - name: Log in to the Container registry |
44 | | - uses: docker/login-action@v2 |
| 84 | + - name: Set up Docker Buildx |
| 85 | + uses: docker/setup-buildx-action@v3 |
| 86 | + |
| 87 | + - name: Login to GitHub Container Registry |
| 88 | + uses: docker/login-action@v3 |
45 | 89 | with: |
46 | | - registry: ${{ env.REGISTRY }} |
| 90 | + registry: ghcr.io |
47 | 91 | username: ${{ github.actor }} |
48 | 92 | password: ${{ secrets.GITHUB_TOKEN }} |
49 | 93 |
|
50 | | - - name: Get build start time |
51 | | - run: | |
52 | | - echo BUILD_TIMESTAMP="$(date --utc --iso-8601=seconds)" >> $GITHUB_ENV |
53 | | -
|
54 | | - - name: Build and push Docker image |
55 | | - uses: docker/build-push-action@v3 |
| 94 | + - name: Docker meta |
| 95 | + id: meta |
| 96 | + uses: docker/metadata-action@v5 |
56 | 97 | with: |
57 | | - context: . |
58 | | - push: true |
59 | | - tags: ${{ env.BUILD_IMAGE }} |
60 | | - build-args: | |
61 | | - BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }} |
62 | | - BUILD_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
63 | | - DOCKER_TAG=${{ env.BUILD_IMAGE }} |
64 | | - GIT_BRANCH=${{ github.ref_name }} |
65 | | - GIT_COMMIT=${{ github.sha }} |
66 | | - GIT_URL=${{ github.repositoryUrl }} |
| 98 | + images: ghcr.io/${{ github.repository }} |
| 99 | + tags: | |
| 100 | + type=sha,suffix=-build-${{ github.run_id }}_${{ github.run_attempt }} |
67 | 101 |
|
68 | | - outputs: |
69 | | - build_image: ${{ env.BUILD_IMAGE }} |
| 102 | + - name: Push the multi-platform image |
| 103 | + run: | |
| 104 | + docker buildx imagetools create \ |
| 105 | + --tag "$DOCKER_METADATA_OUTPUT_TAGS" \ |
| 106 | + "$DOCKER_APP_IMAGE_ARM64" "$DOCKER_APP_IMAGE_X64" |
70 | 107 |
|
71 | 108 | test: |
72 | | - if: github.event_name != 'release' |
73 | | - needs: build |
74 | | - |
75 | | - runs-on: ubuntu-latest |
76 | | - |
77 | | - container: |
78 | | - image: ${{ needs.build.outputs.build_image }} |
79 | | - |
80 | | - defaults: |
81 | | - run: |
82 | | - working-directory: /opt/app |
| 109 | + runs-on: ubuntu-24.04 |
| 110 | + needs: |
| 111 | + - merge |
| 112 | + env: |
| 113 | + COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml |
| 114 | + DOCKER_APP_IMAGE: ${{ needs.merge.outputs.build-image }} |
| 115 | + steps: |
| 116 | + - name: Checkout code |
| 117 | + uses: actions/checkout@v4 |
83 | 118 |
|
84 | | - services: |
85 | | - db: |
86 | | - image: postgres |
87 | | - env: |
88 | | - POSTGRES_USER: root |
89 | | - POSTGRES_PASSWORD: root |
| 119 | + - name: Set up Docker Compose |
| 120 | + uses: docker/setup-compose-action@v1 |
90 | 121 |
|
91 | | - steps: |
92 | | - - name: Run tests |
93 | | - env: |
94 | | - RAILS_ENV: test |
95 | | - run: bundle exec rake check -t |
96 | | - |
97 | | - - name: Run style checks |
98 | | - run: bundle exec rubocop |
99 | | - |
100 | | - - name: Validate database migrations |
101 | | - env: |
102 | | - RAILS_ENV: production |
103 | | - DISABLE_DATABASE_ENVIRONMENT_CHECK: 1 |
104 | | - run: rails --trace db:drop db:create db:migrate |
105 | | - |
106 | | - - name: Upload artifacts |
107 | | - if: ${{ always() }} |
108 | | - uses: actions/upload-artifact@v4 |
| 122 | + - name: Login to GitHub Container Registry |
| 123 | + uses: docker/login-action@v3 |
109 | 124 | with: |
110 | | - name: artifacts |
111 | | - path: artifacts/** |
| 125 | + registry: ghcr.io |
| 126 | + username: ${{ github.actor }} |
| 127 | + password: ${{ secrets.GITHUB_TOKEN }} |
112 | 128 |
|
113 | | - # TODO: DRY w/release.yml |
114 | | - push: |
115 | | - if: github.event_name != 'release' |
| 129 | + - name: Run the test script |
| 130 | + run: | |
| 131 | + docker compose run --rm --user root app chown -R avplayer:avplayer artifacts |
| 132 | + docker compose up --detach --wait |
| 133 | + docker compose exec app bin/test |
116 | 134 |
|
117 | | - needs: [ setup, build, test ] |
| 135 | + push: |
| 136 | + runs-on: ubuntu-24.04 |
| 137 | + needs: |
| 138 | + - merge |
| 139 | + - test |
118 | 140 | env: |
119 | | - BASE_IMAGE_NAME: ${{ needs.setup.outputs.base_image_name }} |
120 | | - BUILD_IMAGE: ${{ needs.build.outputs.build_image }} |
121 | | - |
122 | | - runs-on: ubuntu-latest |
123 | | - |
124 | | - permissions: |
125 | | - packages: write |
126 | | - |
| 141 | + DOCKER_APP_IMAGE: ${{ needs.merge.outputs.build-image }} |
127 | 142 | steps: |
128 | | - - name: Extract metadata (tags, labels) for Docker |
129 | | - id: meta |
130 | | - uses: docker/metadata-action@v4 |
131 | | - with: |
132 | | - images: ${{ env.BASE_IMAGE_NAME }} |
| 143 | + - name: Checkout code |
| 144 | + uses: actions/checkout@v4 |
133 | 145 |
|
134 | | - - name: Log in to the Container registry |
135 | | - uses: docker/login-action@v2 |
| 146 | + - name: Login to GitHub Container Registry |
| 147 | + uses: docker/login-action@v3 |
136 | 148 | with: |
137 | | - registry: ${{ env.REGISTRY }} |
| 149 | + registry: ghcr.io |
138 | 150 | username: ${{ github.actor }} |
139 | 151 | password: ${{ secrets.GITHUB_TOKEN }} |
140 | 152 |
|
141 | | - - name: Tag and push image |
142 | | - |
| 153 | + - name: Produce permanent image tags |
| 154 | + id: branch-meta |
| 155 | + uses: docker/metadata-action@v5 |
143 | 156 | with: |
144 | | - src: ${{ env.BUILD_IMAGE }} |
145 | | - dst: | |
146 | | - ${{ steps.meta.outputs.tags }} |
| 157 | + images: ghcr.io/${{ github.repository }} |
| 158 | + tags: | |
| 159 | + type=sha |
| 160 | + type=ref,event=branch |
| 161 | + type=raw,value=latest,enable={{is_default_branch}} |
147 | 162 |
|
| 163 | + - name: Retag and push the image |
| 164 | + run: | |
| 165 | + docker pull "$DOCKER_APP_IMAGE" |
| 166 | + echo "$DOCKER_METADATA_OUTPUT_TAGS" | tr ' ' '\n' | xargs -n1 docker tag "$DOCKER_APP_IMAGE" |
| 167 | + docker push --all-tags "$(echo "$DOCKER_APP_IMAGE" | cut -f1 -d:)" |
0 commit comments