Skip to content

Commit

Permalink
feat: change docker.yml to use docker cloud registry
Browse files Browse the repository at this point in the history
  • Loading branch information
rustcandy committed Nov 12, 2024
1 parent 86916b3 commit 4a00d62
Showing 1 changed file with 71 additions and 22 deletions.
93 changes: 71 additions & 22 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:
- "go.sum"

env:
REGISTRY: ghcr.io
DOCKER_IMAGE: milkywaylabs/opinit-bots

jobs:
opinitd:
Expand All @@ -31,33 +31,82 @@ jobs:
contents: read
packages: write

outputs:
tags: ${{ steps.meta.outputs.tags }}

env:
IMAGE_NAME: opinitd

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v2
- name: Compute diff 📜
uses: technote-space/[email protected]
id: git_diff
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
PATTERNS: |
**/**.go
go.mod
go.sum
- name: Extract metadata (tags, labels) for Docker
id: meta
- 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.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
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 and push
uses: docker/build-push-action@v4
- name: Build 🔧
if: env.GIT_DIFF
uses: docker/build-push-action@v5
with:
file: Dockerfile
push: ${{ startsWith(github.ref, 'refs/tags') }} # push image only for tags
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
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

0 comments on commit 4a00d62

Please sign in to comment.