Skip to content

Commit 381c996

Browse files
committed
[action] add dockerhub action
1 parent 56d635b commit 381c996

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: DockerHub Push
2+
3+
description: Build and Push images (multiplatform) to Dockerhub.
4+
5+
inputs:
6+
docker_user:
7+
description: 'DockerHub username'
8+
required: true
9+
docker_token:
10+
description: 'DockerHub access token'
11+
required: true
12+
13+
repository:
14+
description: 'DockerHub repository name (username/repository)'
15+
required: true
16+
tag:
17+
description: 'Docker image tag'
18+
required: true
19+
20+
platforms:
21+
description: 'Comma-separated list of platforms'
22+
required: false
23+
default: 'linux/amd64,linux/arm64'
24+
build_args:
25+
description: 'JSON object with build arguments'
26+
required: false
27+
default: '{}'
28+
artifact_name:
29+
description: 'Artifact name to download (if using artifacts)'
30+
required: false
31+
default: ''
32+
33+
context_path:
34+
description: 'Docker build context path'
35+
required: false
36+
default: '.'
37+
dockerfile_path:
38+
description: 'Path to Dockerfile relative to context'
39+
required: false
40+
default: 'Dockerfile'
41+
42+
runs:
43+
using: 'composite'
44+
steps:
45+
- name: Prepare context
46+
id: prepare-context
47+
shell: bash
48+
run: |
49+
if [ -n "${{ inputs.artifact_name }}" ]; then
50+
echo "using_artifact=true" >> $GITHUB_OUTPUT
51+
echo "context_path=./artifact/${{ inputs.context_path }}" >> $GITHUB_OUTPUT
52+
else
53+
echo "using_artifact=false" >> $GITHUB_OUTPUT
54+
echo "context_path=${{ inputs.context_path }}" >> $GITHUB_OUTPUT
55+
fi
56+
57+
- name: Checkout repository
58+
if: steps.prepare-context.outputs.using_artifact == 'false'
59+
uses: actions/checkout@v4
60+
61+
- name: Download artifact
62+
if: steps.prepare-context.outputs.using_artifact == 'true'
63+
uses: actions/download-artifact@v4
64+
with:
65+
name: ${{ inputs.artifact_name }}
66+
path: ./artifact
67+
68+
- name: Login to DockerHub
69+
uses: docker/login-action@v3
70+
with:
71+
username: ${{ inputs.docker_user }}
72+
password: ${{ inputs.docker_token }}
73+
74+
- name: Setup QEMU
75+
uses: docker/setup-qemu-action@v3
76+
77+
- name: Setup Docker Buildx
78+
uses: docker/setup-buildx-action@v3
79+
with:
80+
install: true
81+
82+
- name: Build and push
83+
shell: bash
84+
run: |
85+
BUILD_ARGS=""
86+
if [ "${{ inputs.build_args }}" != "{}" ]; then
87+
while IFS= read -r arg; do
88+
BUILD_ARGS="$BUILD_ARGS $arg"
89+
done < <(echo '${{ inputs.build_args }}' | jq -r 'to_entries | .[] | "--build-arg \(.key)=\(.value)"')
90+
fi
91+
92+
docker buildx build \
93+
--platform ${{ inputs.platforms }} \
94+
--file ${{ steps.prepare-context.outputs.context_path }}/${{ inputs.dockerfile_path }} \
95+
$BUILD_ARGS \
96+
-t ${{ inputs.repository }}:${{ inputs.tag }} \
97+
--push \
98+
${{ steps.prepare-context.outputs.context_path }}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 🧬 DockerHub Push · GitHub Composite Action
2+
3+
This composite GitHub Action builds and pushes multi-platform Docker images to DockerHub with support for artifacts and custom build arguments.
4+
5+
## βœ… Features
6+
7+
Multi-platform image building (linux/amd64, linux/arm64)
8+
Support for build arguments via JSON
9+
Works with artifacts or direct repository checkout
10+
Automatic Docker Buildx setup with QEMU emulation
11+
Custom Dockerfile and context path support
12+
13+
## πŸ”§ Usage Example
14+
```yaml
15+
name: Build and Push to DockerHub
16+
17+
on:
18+
push:
19+
branches: [main]
20+
21+
jobs:
22+
build-and-push:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Push to DockerHub
26+
uses: "Mad-Pixels/github-workflows/.github/actions/dockerhub-push@main"
27+
with:
28+
docker_user: ${{ secrets.DOCKER_USER }}
29+
docker_token: ${{ secrets.DOCKER_TOKEN }}
30+
repository: "myuser/myapp"
31+
tag: "v1.0.0"
32+
platforms: "linux/amd64,linux/arm64"
33+
build_args: '{"NODE_ENV": "production", "VERSION": "1.0.0"}'
34+
```
35+
36+
## πŸ“₯ Inputs
37+
| **Name** | **Required** | **Description** |
38+
|-------------------|--------------|---------------------------------------------------|
39+
| `docker_user` | βœ… Yes | DockerHub username |
40+
| `docker_token` | βœ… Yes | DockerHub access token |
41+
| `repository` | βœ… Yes | DockerHub repository name (username/repository) |
42+
| `tag` | βœ… Yes | Docker image tag |
43+
| `platforms` | ❌ No | Comma-separated list of platforms |
44+
| `build_args` | ❌ No | JSON object with build arguments |
45+
| `artifact_name` | ❌ No | Artifact name to download (if using artifacts) |
46+
| `context_path` | ❌ No | Docker build context path |
47+
| `dockerfile_path` | ❌ No | Path to Dockerfile relative to context |

0 commit comments

Comments
Β (0)