Skip to content

Docker

Docker #4

Workflow file for this run

name: Docker
on:
workflow_dispatch:
push:
branches:
- "main"
tags:
- "v*"
paths:
- "**.go"
- "go.mod"
- "go.sum"
pull_request:
branches:
- "main"
paths:
- "**.go"
- "go.mod"
- "go.sum"
env:
DOCKER_IMAGE: milkywaylabs/opinit-bots
jobs:
opinitd:
name: opinitd
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Compute diff 📜
uses: technote-space/[email protected]
id: git_diff
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- name: Login to Docker registry 🔑
if: env.GIT_DIFF
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Set up Docker Buildx 🛠️
if: env.GIT_DIFF
uses: docker/setup-buildx-action@v3
with:
version: "lab:latest"
driver: cloud
endpoint: "milkywaylabs/default"
install: true
- name: Prepare tags 🏷️
if: env.GIT_DIFF
id: prep-indexer
run: |
VERSION=edge
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
fi
TAGS="${{ env.DOCKER_IMAGE }}:${VERSION},${{ env.DOCKER_IMAGE }}:latest"
if [ "${{ github.event_name }}" = "push" ]; then
TAGS="$TAGS,${{ env.DOCKER_IMAGE }}:sha-${GITHUB_SHA::8}"
fi
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
echo "TAGS=${TAGS}" >> $GITHUB_OUTPUT
echo "CREATED=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
- name: Extract metadata (tags, labels) for Docker image 🏷️
if: env.GIT_DIFF
id: meta-indexer
uses: docker/metadata-action@v4
with:
images: ${{ env.DOCKER_IMAGE }}
- name: Cache Docker layers 💾
id: cache
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build 🔧
if: env.GIT_DIFF
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.TAGS }}
labels: |
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.created=${{ steps.prep.outputs.CREATED }}
org.opencontainers.image.revision=${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache