From 50bb8eee81ea11f185f7c4ee5ba935f514d9bf6f Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Wed, 4 Dec 2024 14:29:22 -0800 Subject: [PATCH] Updates the build process --- .github/workflows/build.yml | 165 +++++++++--------------------------- docker-compose.yml | 50 ++++++----- solr/Dockerfile | 3 + 3 files changed, 72 insertions(+), 146 deletions(-) create mode 100644 solr/Dockerfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c762776..ba261ca 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,151 +1,66 @@ name: Build -on: [ push, pull_request, workflow_dispatch ] -env: - REGISTRY: ghcr.io + +on: [ push, workflow_dispatch ] jobs: - # TODO: DRY w/release.yml setup: runs-on: ubuntu-latest - steps: - # See https://github.com/docker/build-push-action/blob/v2.10.0/TROUBLESHOOTING.md#repository-name-must-be-lowercase - - name: Sanitize image name - uses: actions/github-script@v6 - id: image-name - with: - result-encoding: string - script: return '${{ env.REGISTRY }}/${{ github.repository }}'.toLowerCase() - - - name: Get short SHA + - name: Add variables to the GitHub environment run: | - echo SHORT_SHA="${GITHUB_SHA:0:7}" >> $GITHUB_ENV - - outputs: - base_image_name: ${{ steps.image-name.outputs.result }} - build_image: ${{ steps.image-name.outputs.result }}:${{ env.SHORT_SHA }} + JOB_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" + echo "$JOB_URL" >> "$GITHUB_ENV" build: if: github.event_name != 'release' - needs: setup - env: - BUILD_IMAGE: ${{ needs.setup.outputs.build_image }} - - runs-on: ubuntu-latest permissions: packages: write contents: read + runs-on: ubuntu-latest + + strategy: + matrix: + platform: + - linux/amd64 + - linux/arm64 + fail-fast: false + steps: - name: Checkout repository uses: actions/checkout@v3 - - name: Log in to the Container registry - uses: docker/login-action@v2 + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Get build start time - run: | - echo BUILD_TIMESTAMP="$(date --utc --iso-8601=seconds)" >> $GITHUB_ENV + images: | + ghcr.io/berkeleylibrary/geodata + labels: | + edu.berkeley.lib.build-timestamp=${{ github.event.repository.updated_at }} + edu.berkeley.lib.build-url=${{ env.JOB_URL }} + edu.berkeley.lib.git-ref-name=${{ github.ref_name }} + edu.berkeley.lib.git-repository-url=${{ github.repositoryUrl }} + edu.berkeley.lib.git-sha=${{ github.sha }} + tags: | + type=sha + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} - name: Build and push Docker image uses: docker/build-push-action@v3 with: context: . - push: true - tags: ${{ env.BUILD_IMAGE }} + push: false build-args: | - BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }} - BUILD_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} - DOCKER_TAG=${{ env.BUILD_IMAGE }} - GIT_BRANCH=${{ github.ref_name }} - GIT_COMMIT=${{ github.sha }} - GIT_URL=${{ github.repositoryUrl }} - - outputs: - build_image: ${{ env.BUILD_IMAGE }} - - test: - if: github.event_name != 'release' - needs: build - - runs-on: ubuntu-latest - - container: - options: --init --user=root - image: ${{ needs.build.outputs.build_image }} - - defaults: - run: - working-directory: /opt/app - - services: - db: - image: postgres - env: - POSTGRES_USER: root - POSTGRES_PASSWORD: root - - steps: - - name: Run tests - env: - RAILS_ENV: test - run: bundle exec rake check -t - - - name: Run style checks - run: bundle exec rubocop - - - name: Validate database migrations - env: - RAILS_ENV: test - DISABLE_DATABASE_ENVIRONMENT_CHECK: 1 - run: rails --trace db:drop db:create db:migrate - - - name: Upload artifacts - if: ${{ always() }} - uses: actions/upload-artifact@v4 - with: - name: artifacts - path: | - /opt/app/artifacts/** - /tmp/solr*/server/logs - - - # TODO: DRY w/release.yml - push: - if: github.event_name != 'release' - - needs: [ setup, build, test ] - env: - BASE_IMAGE_NAME: ${{ needs.setup.outputs.base_image_name }} - BUILD_IMAGE: ${{ needs.build.outputs.build_image }} - - runs-on: ubuntu-latest - - permissions: - packages: write - - steps: - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v4 - with: - images: ${{ env.BASE_IMAGE_NAME }} - - - name: Log in to the Container registry - uses: docker/login-action@v2 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Tag and push image - uses: akhilerm/tag-push-action@v2.1.0 - with: - src: ${{ env.BUILD_IMAGE }} - dst: | - ${{ steps.meta.outputs.tags }} + BUILD_TIMESTAMP=${{ github.event.repository.updated_at }} + BUILD_URL=${{ env.JOB_URL }} + GIT_REF_NAME=${{ github.ref_name }} + GIT_REPOSITORY_URL=${{ github.repositoryUrl }} + GIT_SHA=${{ github.sha }} + labels: ${{ steps.meta.outputs.labels }} + tags: ${{ steps.meta.outputs.tags }} diff --git a/docker-compose.yml b/docker-compose.yml index 79bafdb..f00b03b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,31 @@ -version: "3.8" +--- services: + app: + build: + context: . + target: ${DOCKER_APP_BUILD_TARGET:-development} + args: + - BUILD_TIMESTAMP=${BUILD_TIMESTAMP} + - BUILD_URL=https://github.com/${GITHUB_REPOSITORY:-berkeleylibrary/geodata}/actions/runs/${GITHUB_RUN_ID:-development} + - DOCKER_TAG=${BUILD_IMAGE} + - GIT_BRANCH=${GIT_REF_NAME} + - GIT_COMMIT=${GIT_SHA} + - GIT_URL=${GIT_REPOSITORY_URL} + tags: + - ghcr.io/berkeleylibrary/geodata:${DOCKER_APP_TAG} + depends_on: + - db + - solr + ports: + - 3000:3000 + restart: always + environment: + - DATABASE_URL=postgres://root:root@db/geodata-dev?pool=5 + - SOLR_URL=http://solr:8983/solr/geodata-test + volumes: + - ./:/opt/app:delegated + adminer: image: adminer restart: always @@ -11,30 +36,13 @@ services: image: postgres:12.2 restart: always environment: - POSTGRES_USER: root - POSTGRES_PASSWORD: root + - POSTGRES_USER=root + - POSTGRES_PASSWORD=root ports: - 5432:5432 solr: - image: solr:${SOLR_VERSION:-8.11.1} + build: solr restart: always ports: - 8983:8983 - volumes: - - ./solr:/var/solr/data/configsets/solr:delegated - - app: - build: - context: . - target: development - depends_on: - - db - ports: - - 3000:3000 - restart: always - environment: - DATABASE_URL: postgres://root:root@db/geodata-dev?pool=5 - SOLR_URL: http://solr:8983/solr/geodata-test - volumes: - - ./:/opt/app:delegated diff --git a/solr/Dockerfile b/solr/Dockerfile new file mode 100644 index 0000000..4c63a3c --- /dev/null +++ b/solr/Dockerfile @@ -0,0 +1,3 @@ +FROM solr:8 + +COPY --chown=solr . /var/solr/data/