name: Build k8s-mgmt-pod image on: schedule: - cron: '0 3 * * 6' push: branches: ["main"] tags: ["v*.*.*"] pull_request: branches: ["main"] workflow_dispatch: env: IMAGE_BASE_NAME: k8s-mgmt-pod jobs: build: name: Build & Push runs-on: ubuntu-latest permissions: contents: read packages: write id-token: write steps: - name: Checkout uses: actions/checkout@v4 - name: Detect platform & registry id: platform run: | if [[ "${{ github.server_url }}" == "https://github.com" ]]; then echo "registry=ghcr.io" >> $GITHUB_OUTPUT echo "name=GitHub" >> $GITHUB_OUTPUT else DOMAIN=$(echo "${{ github.server_url }}" | sed 's|https://||') echo "registry=${DOMAIN}" >> $GITHUB_OUTPUT echo "name=Forgejo (${DOMAIN})" >> $GITHUB_OUTPUT fi - name: Resolve image name id: image run: | REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') echo "name=${{ steps.platform.outputs.registry }}/${REPO}/${{ env.IMAGE_BASE_NAME }}" >> $GITHUB_OUTPUT - name: Generate build metadata id: meta run: | BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') BUILD_DATE_SHORT=$(date -u +'%Y%m%d') SHORT_SHA=$(git rev-parse --short HEAD) BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" BRANCH_CLEAN=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9._-]/-/g') echo "build_date=${BUILD_DATE}" >> $GITHUB_OUTPUT echo "build_date_short=${BUILD_DATE_SHORT}" >> $GITHUB_OUTPUT echo "short_sha=${SHORT_SHA}" >> $GITHUB_OUTPUT echo "branch=${BRANCH}" >> $GITHUB_OUTPUT echo "branch_clean=${BRANCH_CLEAN}" >> $GITHUB_OUTPUT echo "build_version=${BUILD_DATE_SHORT}-${SHORT_SHA}" >> $GITHUB_OUTPUT - name: Generate Docker tags id: tags run: | IMAGE="${{ steps.image.outputs.name }}" BRANCH="${{ steps.meta.outputs.branch }}" VERSION="${{ steps.meta.outputs.build_version }}" DATE="${{ steps.meta.outputs.build_date_short }}" BRANCH_CLEAN="${{ steps.meta.outputs.branch_clean }}" if [[ "${{ github.ref }}" == refs/tags/* ]]; then TAG="${{ github.ref_name }}" TAGS="${IMAGE}:${TAG} ${IMAGE}:latest" elif [[ "$BRANCH" == "main" ]]; then TAGS="${IMAGE}:latest ${IMAGE}:${VERSION} ${IMAGE}:${DATE}" else TAGS="${IMAGE}:dev-${BRANCH_CLEAN}-latest ${IMAGE}:dev-${BRANCH_CLEAN}-${VERSION}" fi echo "tags<> $GITHUB_OUTPUT echo "$TAGS" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT - name: Log in to GitHub Container Registry if: github.event_name != 'pull_request' && github.server_url == 'https://github.com' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Log in to Forgejo Container Registry if: github.event_name != 'pull_request' && github.server_url != 'https://github.com' uses: docker/login-action@v3 with: registry: ${{ steps.platform.outputs.registry }} username: ${{ github.actor }} password: ${{ secrets.FORGEJOTOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build and push id: build uses: docker/build-push-action@v7 with: context: . file: ./Dockerfile platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.tags.outputs.tags }} labels: | org.opencontainers.image.title=k8s-mgmt-pod org.opencontainers.image.description=Kubernetes management container with lfk and SSH access org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }} org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.created=${{ steps.meta.outputs.build_date }} org.opencontainers.image.version=${{ steps.meta.outputs.build_version }} cache-from: type=gha cache-to: type=gha,mode=max - name: Job summary if: always() run: | STATUS="${{ job.status }}" if [[ "$STATUS" == "success" ]]; then ICON="✅" else ICON="❌" fi echo "# ${ICON} Build ${STATUS^}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "| | |" >> $GITHUB_STEP_SUMMARY echo "|---|---|" >> $GITHUB_STEP_SUMMARY echo "| **Platform** | ${{ steps.platform.outputs.name }} |" >> $GITHUB_STEP_SUMMARY echo "| **Registry** | \`${{ steps.platform.outputs.registry }}\` |" >> $GITHUB_STEP_SUMMARY echo "| **Image** | \`${{ steps.image.outputs.name }}\` |" >> $GITHUB_STEP_SUMMARY echo "| **Branch** | \`${{ steps.meta.outputs.branch }}\` |" >> $GITHUB_STEP_SUMMARY echo "| **Commit** | \`${{ steps.meta.outputs.short_sha }}\` |" >> $GITHUB_STEP_SUMMARY echo "| **Version** | \`${{ steps.meta.outputs.build_version }}\` |" >> $GITHUB_STEP_SUMMARY echo "| **Built at** | \`${{ steps.meta.outputs.build_date }}\` |" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [[ "${{ github.event_name }}" != "pull_request" ]]; then echo "### Published tags" >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY echo "${{ steps.tags.outputs.tags }}" >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY else echo "> Built but **not pushed** (pull request)" >> $GITHUB_STEP_SUMMARY fi