Skip to content

Commit 705ad86

Browse files
feat(docker-build): features and fixes (#72)
feat(docker-build): make image-tag and dockerfile optional feat(docker-build): allow dry-runs by always running build (even if push is disabled) docs(docker-build): fix bad workflow reference fix(docker-build): fix sarif security-report docs(docker-build): fix documentation
1 parent 773b9cb commit 705ad86

File tree

2 files changed

+63
-83
lines changed

2 files changed

+63
-83
lines changed

.github/workflows/docker-build.yml

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@ name: Build, Test and Push Docker Image
33
on:
44
workflow_call:
55
inputs:
6-
dockerfile:
7-
description: 'Path to Dockerfile'
8-
default: 'Dockerfile'
9-
type: string
106
image-name:
11-
description: 'Name of Docker Image'
7+
description: "Name of Docker Image"
128
type: string
139
required: true
1410
image-tag:
15-
description: 'Tag of Docker Image'
11+
description: "Tag of Docker Image"
12+
default: "latest"
1613
type: string
17-
required: true
14+
dockerfile:
15+
description: "Path to Dockerfile"
16+
default: "Dockerfile"
17+
type: string
18+
context:
19+
description: "Path to Docker Build Context"
20+
default: "."
21+
type: string
22+
registry:
23+
description: "Docker Registry"
24+
default: "docker.io"
25+
type: string
26+
push:
27+
description: "Push Docker Image to Registry"
28+
default: false
29+
type: boolean
1830
security-scan:
19-
description: 'Enable Security Scan'
31+
description: "Enable Trivy Security Scan"
2032
default: true
2133
type: boolean
2234
security-report:
23-
description: 'Enable Security Report'
24-
default: 'sarif'
35+
description: 'Security Report Mode (`"sarif"` | `"comment"`; ignored if `security-scan: false`)'
36+
default: "sarif"
2537
type: string
2638
hadolint:
27-
description: 'Enable Hadolint'
39+
description: "Enable Hadolint"
2840
default: true
2941
type: boolean
30-
push:
31-
description: 'Push Docker Image to Registry'
32-
default: false
33-
type: boolean
34-
context:
35-
description: 'Path to Docker Build Context'
36-
default: '.'
37-
type: string
38-
registry:
39-
description: 'Docker Registry'
40-
default: 'docker.io'
41-
type: string
4242
secrets:
4343
username:
4444
required: false
@@ -67,7 +67,6 @@ jobs:
6767
password: ${{ secrets.password }}
6868

6969
- name: Build Docker Image
70-
if: ${{ inputs.push }}
7170
uses: docker/build-push-action@v6
7271
with:
7372
context: ${{ inputs.context }}
@@ -88,12 +87,12 @@ jobs:
8887
uses: aquasecurity/[email protected]
8988
with:
9089
input: vuln-image.tar
91-
format: 'table'
90+
format: ${{ (inputs.security-report == 'sarif' && 'sarif') || 'table' }}
9291
ignore-unfixed: true
93-
vuln-type: 'os,library'
94-
severity: 'CRITICAL,HIGH'
92+
vuln-type: "os,library"
93+
severity: "CRITICAL,HIGH"
9594
hide-progress: true
96-
output: trivy.txt
95+
output: ${{ (inputs.security-report == 'sarif' && 'trivy-results.sarif') || 'trivy.txt' }}
9796

9897
- name: Read Trivy report file
9998
id: read_trivy
@@ -109,8 +108,8 @@ jobs:
109108
uses: peter-evans/find-comment@v3
110109
with:
111110
issue-number: ${{ github.event.pull_request.number }}
112-
comment-author: 'github-actions[bot]'
113-
body-includes: 'Trivy Security Scan Results'
111+
comment-author: "github-actions[bot]"
112+
body-includes: "Trivy Security Scan Results"
114113

115114
- name: Create or update Trivy comment
116115
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'comment'
@@ -134,7 +133,7 @@ jobs:
134133
if: github.event_name == 'pull_request' && inputs.security-scan && inputs.security-report == 'sarif'
135134
uses: github/codeql-action/upload-sarif@v3
136135
with:
137-
sarif_file: 'trivy-results.sarif'
136+
sarif_file: "trivy-results.sarif"
138137

139138
- name: Run Hadolint Dockerfile linter
140139
id: hadolint
@@ -159,8 +158,8 @@ jobs:
159158
uses: peter-evans/find-comment@v3
160159
with:
161160
issue-number: ${{ github.event.pull_request.number }}
162-
comment-author: 'github-actions[bot]'
163-
body-includes: 'Hadolint Dockerfile Lint Results'
161+
comment-author: "github-actions[bot]"
162+
body-includes: "Hadolint Dockerfile Lint Results"
164163

165164
- name: Create or update Hadolint comment
166165
if: ${{ inputs.hadolint && steps.read_hadolint.outputs.report != '' }}

docker-build/README.md

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
# 🐳 Docker Build Workflow
22

33
## 🔍 Overview
4+
45
This reusable GitHub Actions workflow automates the process of building and pushing Docker images to Docker Hub. It simplifies the Docker build process in your CI/CD pipeline by handling authentication, building, and tagging in a standardized way. Perfect for teams looking to streamline their containerization workflow with minimal configuration.
56

67
## ✨ Features
8+
79
- 🔐 Securely authenticates with Docker Hub using best practices
810
- 🏗️ Builds optimized Docker images from a specified Dockerfile
911
- 🏷️ Intelligently tags and pushes images to Docker Hub
12+
- 🔎 Scan for vulnerabilities
13+
- 👍 Lint dockerfile
1014
- 🛡️ Handles authentication securely using GitHub Secrets
1115
- 🚀 Optimizes build performance with layer caching
1216
- 📦 Supports multi-platform builds (AMD64, ARM64)
1317

1418
## ⚙️ Inputs
1519

16-
| Name | Description | Required | Default |
17-
|------|-------------|----------|---------|
18-
| `dockerfile` | Path to the Dockerfile to build (e.g., './Dockerfile', './docker/Dockerfile') | Yes | - |
19-
| `tag` | Tag to apply to the built image (e.g., 'myimage:latest', 'myorg/myimage:v1.2.3') | Yes | - |
20+
| Name | Description | Required | Default |
21+
| ----------------- | ---------------------------------------------------------------------------------- | -------- | -------------- |
22+
| `image-name` | Name of Docker Image (e.g., 'myimage', 'myorg/myimage') | true | - |
23+
| `image-tag` | Tag to apply to the built image (e.g., 'latest', 'v1.2.3') | No | `"latest"` |
24+
| `dockerfile` | Path to the Dockerfile to build (e.g., './Dockerfile', './docker/Dockerfile') | No | `"Dockerfile"` |
25+
| `context` | Path to Docker Build Context | No | `"."` |
26+
| `registry` | Docker Registry | No | `"docker.io"` |
27+
| `push` | Push Docker Image to Registry | No | `false` |
28+
| `security-scan` | Enable Trivy Security Scan | No | `true` |
29+
| `security-report` | Security Report Mode (`"sarif"` \| `"comment"`; ignored if `security-scan: false`) | No | `"sarif"` |
30+
| `hadolint` | Enable Hadolint | No | `true` |
2031

2132
## 🔐 Secrets
2233

23-
| Name | Description | Required |
24-
|------|-------------|----------|
25-
| `dockerhub_username` | Username for Docker Hub authentication | Yes |
26-
| `dockerhub_pat` | Personal Access Token for Docker Hub authentication (with appropriate permissions) | Yes |
34+
| Name | Description | Required |
35+
| ---------- | --------------------------------------------------------------------------------------------------- | -------- |
36+
| `username` | Username for Docker Registry authentication | Yes |
37+
| `password` | Password or Personal Access Token for Docker registry authentication (with appropriate permissions) | Yes |
2738

2839
## 💻 Example Usage
2940

@@ -32,69 +43,39 @@ name: Build and Push Docker Image
3243

3344
on:
3445
push:
35-
branches: [ main ]
46+
branches: [main]
3647
# Also trigger on tag creation for release versioning
3748
tags:
38-
- 'v*.*.*'
49+
- "v*.*.*"
3950

4051
jobs:
4152
build:
4253
runs-on: ubuntu-latest
4354
steps:
44-
- uses: actions/checkout@v3
45-
with:
46-
fetch-depth: 0 # Fetch all history for proper versioning
47-
48-
- name: Build and Push Docker Image
49-
uses: iExecBlockchainComputing/github-actions-workflows/[email protected]
55+
- uses: actions/checkout@v4
5056
with:
51-
dockerfile: 'Dockerfile'
52-
tag: 'my-image:latest'
53-
secrets:
54-
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
55-
dockerhub_pat: ${{ secrets.DOCKERHUB_PAT }}
56-
```
57-
58-
## 🔍 Advanced Usage
59-
60-
### Multi-Platform Build Example
61-
```yaml
62-
name: Build Multi-Platform Docker Image
63-
64-
on:
65-
release:
66-
types: [published]
67-
68-
jobs:
69-
build:
70-
runs-on: ubuntu-latest
71-
steps:
72-
- uses: actions/checkout@v3
73-
74-
- name: Set up QEMU
75-
uses: docker/setup-qemu-action@v2
76-
77-
- name: Set up Docker Buildx
78-
uses: docker/setup-buildx-action@v2
57+
fetch-depth: 0 # Fetch all history for proper versioning
7958

8059
- name: Build and Push Docker Image
81-
uses: iExecBlockchainComputing/github-actions-workflows/docker-build@docker-build-v1.1.1
60+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/docker-build.yml@main # ⚠️ use tagged version here
8261
with:
83-
dockerfile: 'Dockerfile'
84-
tag: 'myorg/myapp:${{ github.event.release.tag_name }}'
85-
secrets:
86-
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
87-
dockerhub_pat: ${{ secrets.DOCKERHUB_PAT }}
62+
image-name: "username/my-image"
63+
dockerfile: "Dockerfile"
64+
secrets:
65+
username: ${{ secrets.DOCKERHUB_USERNAME }}
66+
password: ${{ secrets.DOCKERHUB_PAT }}
8867
```
8968
9069
## 📝 Notes
70+
9171
- 🔒 Ensure your Docker Hub credentials are stored securely as GitHub Secrets
9272
- 🔄 The workflow will automatically handle the Docker build and push process
9373
- 🏷️ You can specify any valid Docker tag format in the `tag` input
9474
- 📅 Consider using dynamic tags based on git tags, commit SHAs, or dates
9575
- 🧪 For testing purposes, you can use the `--dry-run` flag in your own implementation
9676

9777
## 🛠️ Troubleshooting
78+
9879
- If you encounter authentication issues, verify your Docker Hub credentials are correct and have appropriate permissions
9980
- For build failures, check your Dockerfile syntax and ensure all referenced files exist
10081
- Large images may take longer to push - consider optimizing your Dockerfile with multi-stage builds

0 commit comments

Comments
 (0)