Skip to content

Commit

Permalink
Extract docker job into reusable workflow (#938)
Browse files Browse the repository at this point in the history
* Extractor docker job into reusable workflow

* Update visualizer workflow to reuse docker workflow
  • Loading branch information
rimrakhimov authored Jun 26, 2024
1 parent a774737 commit 8c78035
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 54 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Docker build and docker push (reusable)

on:
workflow_call:
inputs:
registry:
description: 'A container registry where the image is stored'
default: 'ghcr.io'
required: false
type: string
organization:
description: 'An organization the image is associated with'
default: 'blockscout'
required: false
type: string
service-name:
required: true
type: string

jobs:
build-and-push:
runs-on: ubuntu-latest
timeout-minutes: 300
env:
IMAGE_NAME: '${{ inputs.registry }}/${{ inputs.organization }}/${{ inputs.service-name }}'
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions-ecosystem/action-regex-match@v2
id: regex
with:
text: ${{ github.ref }}
regex: '^(refs\/tags\/${{ inputs.service-name }}\/(v\d+\.\d+\.\d+))|(refs\/heads\/(main))$'

- name: Extract tag name
id: tags_extractor
run: |
t=${{ steps.regex.outputs.group2 }}
m=${{ steps.regex.outputs.group4 }}
(if ! [[ "$t" == "" ]]; then echo tags=${{ env.IMAGE_NAME }}:$t, ${{ env.IMAGE_NAME }}:latest; elif ! [[ "$m" == "" ]]; then echo tags=${{ env.IMAGE_NAME }}:$m; else echo tags=; fi) >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v6
with:
context: ${{ inputs.service-name }}
file: '${{ inputs.service-name }}/Dockerfile'
build-contexts: |
proto=proto
push: ${{ steps.tags_extractor.outputs.tags != '' }}
tags: ${{ steps.tags_extractor.outputs.tags }}
# platforms: |
# linux/amd64
# linux/arm64/v8
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:build-cache
cache-to: ${{ github.ref == 'refs/heads/main' && format('type=registry,ref={0}:build-cache,mode=max', env.IMAGE_NAME) || '' }}
60 changes: 6 additions & 54 deletions .github/workflows/visualizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ on:
- visualizer/**
- .github/workflows/visualizer.yml
- .github/actions/deps/**
- .github/workflows/docker-build-push.yml
pull_request:
paths:
- visualizer/**
- .github/workflows/visualizer.yml
- .github/actions/deps/**
- .github/workflows/docker-build-push.yml

name: Test, lint and docker (visualizer)

env:
REGISTRY: ghcr.io
IMAGE_NAME: blockscout/visualizer

defaults:
run:
working-directory: visualizer
Expand Down Expand Up @@ -98,7 +96,7 @@ jobs:
- name: cargo clippy
run: cargo clippy --all --all-targets --all-features -- -D warnings

push:
docker:
name: Docker build and docker push
needs:
- test
Expand All @@ -107,52 +105,6 @@ jobs:
always() &&
(needs.test.result == 'success' || needs.test.result == 'cancelled') &&
(needs.lint.result == 'success' || needs.lint.result == 'cancelled')
timeout-minutes: 300
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions-ecosystem/action-regex-match@v2
id: regex
with:
text: ${{ github.ref }}
regex: '^(refs\/tags\/visualizer\/(v\d+\.\d+\.\d+))|(refs\/heads\/(main))$'

- name: Extract tag name
id: tags_extractor
run: |
t=${{ steps.regex.outputs.group2 }}
m=${{ steps.regex.outputs.group4 }}
(if ! [[ "$t" == "" ]]; then echo tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$t, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest; elif ! [[ "$m" == "" ]]; then echo tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$m; else echo tags=; fi) >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v5
with:
context: "visualizer"
file: "visualizer/Dockerfile"
build-contexts: |
proto=proto
push: ${{ steps.tags_extractor.outputs.tags != '' }}
tags: ${{ steps.tags_extractor.outputs.tags }}
# platforms: |
# linux/amd64
# linux/arm64/v8
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:build-cache
cache-to: ${{ github.ref == 'refs/heads/main' && format('type=registry,ref={0}/{1}:build-cache,mode=max', env.REGISTRY, env.IMAGE_NAME) || '' }}
uses: ./.github/workflows/docker-build-push.yml
with:
service-name: visualizer

0 comments on commit 8c78035

Please sign in to comment.