Skip to content

Build and Push to Dockerhub #73

Build and Push to Dockerhub

Build and Push to Dockerhub #73

name: Build and Push to Dockerhub
on:
workflow_dispatch:
workflow_run:
workflows: ["Test and Lint", "Code Scanning"]
types:
- completed
branches:
- main
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/smr-website
jobs:
build-and-push:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Verify all prerequisite workflows passed
if: ${{ github.event_name != 'workflow_dispatch' }}
uses: actions/github-script@v7
with:
script: |
const sha = context.payload.workflow_run.head_sha;
const requiredWorkflows = ['Test and Lint', 'Code Scanning'];
const runs = await github.rest.actions.listWorkflowRunsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: sha,
status: 'success',
});
for (const name of requiredWorkflows) {
const passed = runs.data.workflow_runs.some(r => r.name === name);
if (!passed) {
core.info(`Workflow "${name}" has not succeeded yet for SHA ${sha}. Skipping build.`);
process.exit(1);
}
}
core.info(`All prerequisite workflows passed for SHA ${sha}.`);
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix=
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max