|
| 1 | +name: Build, Test, and Publish Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - master |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + SERVICE_NAME: "rslearn_projects" |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + outputs: |
| 26 | + ghcr_docker_image: ${{ steps.image-names.outputs.ghcr_image_name }} |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Log in to the Container registry |
| 32 | + uses: docker/login-action@v3 |
| 33 | + with: |
| 34 | + registry: ${{ env.REGISTRY }} |
| 35 | + username: ${{ github.actor }} |
| 36 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + |
| 38 | + - name: Extract metadata (tags, labels) for Docker |
| 39 | + id: meta |
| 40 | + uses: docker/metadata-action@v5 |
| 41 | + with: |
| 42 | + images: | |
| 43 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 44 | + tags: | |
| 45 | + type=sha,format=long |
| 46 | + type=sha,format=short |
| 47 | + type=raw,value=latest,enable={{is_default_branch}} |
| 48 | +
|
| 49 | + - name: Build and push Docker image |
| 50 | + id: build-push |
| 51 | + uses: docker/build-push-action@v6 |
| 52 | + with: |
| 53 | + context: . |
| 54 | + push: true |
| 55 | + tags: ${{ steps.meta.outputs.tags }} |
| 56 | + labels: ${{ steps.meta.outputs.labels }} |
| 57 | + build-args: | |
| 58 | + GIT_USERNAME=${{ secrets.GIT_USERNAME }} |
| 59 | + GIT_TOKEN=${{ secrets.GIT_TOKEN }} |
| 60 | +
|
| 61 | + - name: Store Image Names |
| 62 | + # We need the docker image name downstream in test & deploy. This saves the full docker image names to outputs |
| 63 | + id: image-names |
| 64 | + run: |- |
| 65 | + GHCR_IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-push.outputs.digest }}" |
| 66 | + GHCR_IMAGE=`echo ${GHCR_IMAGE} | tr '[:upper:]' '[:lower:]'` # docker requires that all image names be lowercase |
| 67 | + echo "ghcr.io Docker image name is ${GHCR_IMAGE}" |
| 68 | + echo "ghcr_image_name=\"${GHCR_IMAGE}\"" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + # TODO: Make sure skylight can grab the image tag and deploy |
| 71 | + |
| 72 | + test: |
| 73 | + runs-on: ubuntu-latest |
| 74 | + steps: |
| 75 | + - name: Checkout repository |
| 76 | + uses: actions/checkout@v4 |
| 77 | + - name: Log in to the Container registry |
| 78 | + uses: docker/login-action@v3 |
| 79 | + with: |
| 80 | + registry: ${{ env.REGISTRY }} |
| 81 | + username: ${{ github.actor }} |
| 82 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + |
| 84 | + - name: Log in to the Container registry |
| 85 | + uses: docker/login-action@v3 |
| 86 | + with: |
| 87 | + registry: ${{ env.REGISTRY }} |
| 88 | + username: ${{ github.actor }} |
| 89 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + - name: Build docker images |
| 91 | + run: | |
| 92 | + COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose -f docker-compose.yaml build |
| 93 | +
|
| 94 | +
|
| 95 | + - name: Run tests with Docker Compose |
| 96 | + run: | |
| 97 | + docker compose -f docker-compose.yaml run test pytest tests/ |
| 98 | +
|
| 99 | + - name: Clean up |
| 100 | + if: always() |
| 101 | + run: | |
| 102 | + docker compose -f docker-compose.yaml down |
0 commit comments