Refactor Docker setup and update start script in Dockerfile and start.sh #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Monitor Suibase Repository and Rebuild Docker Image | |
on: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: "0 0 * * 0" # Runs every Sunday at midnight | |
jobs: | |
build-suibase-docker-image: | |
permissions: | |
actions: read | |
contents: read | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v4 | |
- name: Check Suibase Repository | |
id: check_suibase_repo | |
run: | | |
git fetch --depth=1 | |
EXTERNAL_REPO="ChainMovers/suibase" # Repository to monitor | |
API_URL="https://api.github.com/repos/$EXTERNAL_REPO/commits" | |
LATEST_COMMIT=$(curl -s $API_URL | jq -r '.[0].sha') | |
SHORT_COMMIT=$(echo $LATEST_COMMIT | head -c7) | |
echo "last_commit=$SHORT_COMMIT" >> $GITHUB_OUTPUT | |
- name: Check if Docker Image Already Exists | |
id: check_image_exists | |
run: | | |
IMAGE_TAG=${{secrets.DOCKERHUB_USERNAME}}/suibase:${{ steps.check_suibase_repo.outputs.last_commit }} | |
IMAGE_EXISTS=$(curl -s https://hub.docker.com/v2/repositories/${{ secrets.DOCKERHUB_USERNAME }}/suibase/tags/${{ steps.check_suibase_repo.outputs.last_commit }}/) | |
if [ "$IMAGE_EXISTS" == "" ]; then | |
echo "Image does not exist" | |
echo "image_exists=false" >> $GITHUB_OUTPUT | |
else | |
echo "Image already exists" | |
echo "image_exists=true" >> $GITHUB_OUTPUT | |
fi | |
- name: Check if Dockerfile has changed | |
id: check_dockerfile_change | |
run: | | |
# Fetch the last two commits and check if Dockerfile is in the list of changed files | |
git fetch --depth=2 | |
FILES_CHANGED=$(git diff --name-only HEAD^ HEAD) | |
if echo "$FILES_CHANGED" | grep -q "Dockerfile"; then | |
echo "Dockerfile has changed." | |
echo "dockerfile_changed=true" >> $GITHUB_OUTPUT | |
else | |
echo "Dockerfile has not changed." | |
echo "dockerfile_changed=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Set up Docker Buildx | |
if: steps.check_image_exists.outputs.image_exists != 'true' || steps.check_dockerfile_change.outputs.dockerfile_changed == 'true' | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to DockerHub | |
if: steps.check_image_exists.outputs.image_exists != 'true' || steps.check_dockerfile_change.outputs.dockerfile_changed == 'true' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and Push Docker Image | |
if: steps.check_image_exists.outputs.image_exists != 'true' || steps.check_dockerfile_change.outputs.dockerfile_changed == 'true' | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
platforms: linux/amd64 | |
push: true | |
tags: noras/suibase:latest,noras/suibase:${{ steps.check_suibase_repo.outputs.last_commit }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |