91 lines
3.0 KiB
YAML
91 lines
3.0 KiB
YAML
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: Install Dependencies and Build Frontend
|
|
run: |
|
|
cd frontend
|
|
npm install
|
|
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
|
|
# If the tag_name input is an empty string, default to branch-name-development-latest
|
|
IMAGE_TAG="${{ github.ref_name }}-development-latest"
|
|
else
|
|
# If tag_name is provided, use it
|
|
IMAGE_TAG="${{ github.event.inputs.tag_name }}"
|
|
fi
|
|
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
|
|
|
|
- name: Build and Push Docker Image
|
|
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 }}
|
|
|
|
- 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: Cleanup Docker Images on GHCR
|
|
run: |
|
|
# Get the list of images from ghcr.io
|
|
images=$(curl -s -H "Authorization: Bearer ${{ secrets.GHCR_TOKEN }}" \
|
|
"https://api.github.com/orgs/${{ github.repository_owner }}/packages/container/ssh-project/versions")
|
|
|
|
# List image tags and delete older versions (keep the latest 10)
|
|
count=0
|
|
max_images=10
|
|
for image in $(echo "$images" | jq -r '.[].id'); do
|
|
if [ $count -ge $max_images ]; then
|
|
echo "Deleting image $image"
|
|
curl -X DELETE -H "Authorization: Bearer ${{ secrets.GHCR_TOKEN }}" \
|
|
"https://api.github.com/orgs/${{ github.repository_owner }}/packages/container/ssh-project/versions/$image"
|
|
fi
|
|
count=$((count + 1))
|
|
done
|
|
|
|
- name: Cleanup Docker Images Locally
|
|
run: |
|
|
docker image prune -af
|
|
docker system prune -af --volumes |