diff --git a/.docker/Dockerfile.seed b/.docker/Dockerfile.seed index 381d99f3..08c09357 100644 --- a/.docker/Dockerfile.seed +++ b/.docker/Dockerfile.seed @@ -1,4 +1,5 @@ -FROM ghcr.io/dpc-sdp/bay/mariadb:6.x +ARG BASE_IMAGE=ghcr.io/dpc-sdp/bay/mariadb:6.x +FROM $BASE_IMAGE ARG DATA_DIR=/tmp/data @@ -7,8 +8,7 @@ COPY $DATA_DIR /var/lib/mysql USER root RUN chown -R mysql /var/lib/mysql \ - && chgrp -R mysql /var/lib/mysql \ - && /bin/fix-permissions /var/lib/mysql + && chgrp -R mysql /var/lib/mysql USER mysql ENV MARIADB_INIT_WAIT_SECONDS=80 diff --git a/.github/workflows/run_db_trigger.yml b/.github/workflows/run_db_trigger.yml index c6086b4f..a411a842 100644 --- a/.github/workflows/run_db_trigger.yml +++ b/.github/workflows/run_db_trigger.yml @@ -146,16 +146,120 @@ jobs: annotations: ${{ steps.meta.outputs.annotations }} push: true + export-db-mysql: + runs-on: ${{ inputs.runner }} + permissions: + contents: read + packages: write + id-token: write + env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }}/mysql + TMP_DATA_DIR: data + DB_FILE: db.sql + steps: + - name: Start the database container + id: container + run: | + CONTAINER_ID=$(docker run -d -e "MYSQL_ROOT_PASSWORD=${{ secrets.DB_IMAGE_MARIADB_ROOT_PASSWORD }}" ghcr.io/dpc-sdp/bay/mysql:6.x) + sleep 30 + echo "id=$CONTAINER_ID" >> $GITHUB_OUTPUT + + - name: Configure AWS actions + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ap-southeast-2 + + - name: Download the database + run: aws s3 cp ${{ inputs.s3_uri }} "${{ env.DB_FILE }}" + + - name: Import the database into the running container + run: | + echo 'create database if not exists drupal;' | docker exec -i "${{ steps.container.outputs.id }}" /usr/bin/mysql -u root -p${{ secrets.DB_IMAGE_MARIADB_ROOT_PASSWORD }} + cat "${{ env.DB_FILE }}" | docker exec -i "${{ steps.container.outputs.id }}" /usr/bin/mysql -u root -p${{ secrets.DB_IMAGE_MARIADB_ROOT_PASSWORD }} drupal + + - name: Verify database is available + run: | + if docker exec -i "${{ steps.container.outputs.id }}" /usr/bin/mysql -u root -p${{ secrets.DB_IMAGE_MARIADB_ROOT_PASSWORD }} -e "show tables;" drupal | grep -q users; then + echo "Database import successfully" + else + echo "Database was not imported successfully" + exit 1 + fi + + - name: Gracefully shutdown the intermediary container + run: docker exec -i "${{ steps.container.outputs.id }}" mysqladmin -u root -p${{ secrets.DB_IMAGE_MARIADB_ROOT_PASSWORD }} shutdown + continue-on-error: true + + - name: Copy database files out of the intermediary container + run: | + mkdir -p "${{ env.TMP_DATA_DIR }}" + docker cp "${{ steps.container.outputs.id }}":/var/lib/mysql/. "${{ env.TMP_DATA_DIR }}/" + ls -la "${{ env.TMP_DATA_DIR }}" + + - name: Verify the data directory + run: | + if [ -d "${{ env.TMP_DATA_DIR }}/mysql" ]; then + echo "Files exist on the host" + else + ls -la "${{ env.TMP_DATA_DIR }}" + exit 1 + fi + + - name: Download the DB image dockerfile + run: curl -o Dockerfile.seed https://raw.githubusercontent.com/dpc-sdp/github-actions/feature/mysql-db-image/.docker/Dockerfile.seed + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ghcr.io + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build and push the database image + uses: docker/build-push-action@v5 + with: + file: Dockerfile.seed + context: . + build-args: | + DATA_DIR=${{ env.TMP_DATA_DIR }} + BASE_IMAGE=ghcr.io/dpc-sdp/bay/mysql:6.x + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + platforms: linux/amd64,linux/arm64 + labels: ${{ steps.meta.outputs.labels }} + annotations: ${{ steps.meta.outputs.annotations }} + push: true + notify_slack: runs-on: ubuntu-latest - needs: [export-db] + needs: [export-db, export-db-mysql] if: ${{ always() }} steps: - name: Send Notification to Slack on Failure - if: ${{ needs.export-db.result == 'failure' }} + if: ${{ needs.export-db.result == 'failure' || needs.export-db-mysql.result == 'failure' }} uses: 8398a7/action-slack@v3 with: - status: ${{ needs.export-db.result }} + status: failure text: 'Attention: The db-trigger workflow has FAILED in project "${{ inputs.project }}" for repo "${{ github.repository }}"!' fields: 'project: ${{ inputs.project }}, repo: ${{ github.repository }}' mention: 'here'