name: Build and Push Docker Image on: push: branches: - development workflow_dispatch: inputs: tag_name: description: "Custom tag name for the Docker image" required: false default: "" jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Setup Node.js uses: actions/setup-node@v2 with: node-version: '18' - name: Cache Node Modules uses: actions/cache@v3 with: path: frontend/node_modules key: ${{ runner.os }}-node-modules-${{ hashFiles('frontend/package-lock.json') }} restore-keys: | ${{ runner.os }}-node-modules- - name: Install Dependencies and Build Frontend run: | cd frontend npm ci npm run build - name: Setup Docker Buildx uses: docker/setup-buildx-action@v1 - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Login to Docker Registry uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Determine Docker image tag run: | echo "REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV if [ "${{ github.event.inputs.tag_name }}" == "" ]; then IMAGE_TAG="${{ github.ref_name }}-development-latest" else IMAGE_TAG="${{ github.event.inputs.tag_name }}" fi echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV - name: Cache ghcr-cleaner uses: actions/cache@v3 with: path: /usr/local/bin/ghcr-cleaner key: ghcr-cleaner-${{ runner.os }}-${{ hashFiles('path/to/ghcr-cleaner-version-file') }} restore-keys: | ghcr-cleaner-${{ runner.os }}- - name: Download ghcr-cleaner if not cached run: | if ! command -v ghcr-cleaner &> /dev/null; then echo "ghcr-cleaner not found, downloading..." curl -L https://github.com/quartz-analytics/ghcr-cleaner/releases/download/v1.0.0/ghcr-cleaner-linux-amd64 -o /usr/local/bin/ghcr-cleaner chmod +x /usr/local/bin/ghcr-cleaner else echo "ghcr-cleaner found, skipping download" fi - name: Build and Push Docker Image with Cache uses: docker/build-push-action@v2 with: context: . file: ./docker/Dockerfile push: true tags: ghcr.io/${{ env.REPO_OWNER }}/ssh-project:${{ env.IMAGE_TAG }} labels: org.opencontainers.image.source=https://github.com/${{ github.repository }} cache-from: type=gha,ref=ghcr.io/${{ env.REPO_OWNER }}/ssh-project:cache cache-to: type=gha,mode=max,ref=ghcr.io/${{ env.REPO_OWNER }}/ssh-project:cache - name: Notify via ntfy run: | curl -d "Docker image build and push completed successfully for tag: ${{ env.IMAGE_TAG }}" \ https://ntfy.karmaashomepage.online/ssh-project-build - name: Delete all untagged image versions uses: quartx-analytics/ghcr-cleaner@v1 with: owner-type: user token: ${{ secrets.GHCR_TOKEN }} repository-owner: ${{ github.repository_owner }} delete-untagged: true keep-at-most: 0 - name: Cleanup Docker Images Locally run: | docker image prune -af docker system prune -af --volumes