|
| 1 | +name: Build |
| 2 | +on: [ push, pull_request, workflow_dispatch ] |
| 3 | +env: |
| 4 | + REGISTRY: ghcr.io |
| 5 | + |
| 6 | +jobs: |
| 7 | + # TODO: DRY w/release.yml |
| 8 | + setup: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + 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 |
| 16 | + with: |
| 17 | + result-encoding: string |
| 18 | + script: return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase() |
| 19 | + |
| 20 | + - name: Get short SHA |
| 21 | + run: | |
| 22 | + echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV |
| 23 | +
|
| 24 | + - name: Get build start time |
| 25 | + run: | |
| 26 | + echo BUILD_TIMESTAMP="$(date --utc --iso-8601=seconds)" >> $GITHUB_ENV |
| 27 | +
|
| 28 | + outputs: |
| 29 | + base_image_name: ${{ steps.image-name.outputs.result }} |
| 30 | + build_image: ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }} |
| 31 | + |
| 32 | + test: |
| 33 | + if: github.event_name != 'release' |
| 34 | + needs: [ setup ] |
| 35 | + runs-on: ubuntu-latest |
| 36 | + permissions: |
| 37 | + packages: write |
| 38 | + env: |
| 39 | + BUILD_IMAGE: ${{ needs.setup.outputs.build_image }} |
| 40 | + steps: |
| 41 | + - name: Checkout repository |
| 42 | + uses: actions/checkout@v3 |
| 43 | + |
| 44 | + - name: Run tests |
| 45 | + run: ./test.sh |
| 46 | + |
| 47 | + - name: Log in to the Container registry |
| 48 | + uses: docker/login-action@v2 |
| 49 | + with: |
| 50 | + registry: ${{ env.REGISTRY }} |
| 51 | + username: ${{ github.actor }} |
| 52 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 53 | + |
| 54 | + - name: Build and push Docker image |
| 55 | + uses: docker/build-push-action@v3 |
| 56 | + 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 }} |
| 67 | +
|
| 68 | + outputs: |
| 69 | + build_image: ${{ env.BUILD_IMAGE }} |
0 commit comments