|
1 | | -# Docker build |
| 1 | +# π³ Docker Build Workflow |
2 | 2 |
|
3 | | -## Inputs |
| 3 | +## π Overview |
| 4 | +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. |
4 | 5 |
|
5 | | -### `dockerfile` |
| 6 | +## β¨ Features |
| 7 | +- π Securely authenticates with Docker Hub using best practices |
| 8 | +- ποΈ Builds optimized Docker images from a specified Dockerfile |
| 9 | +- π·οΈ Intelligently tags and pushes images to Docker Hub |
| 10 | +- π‘οΈ Handles authentication securely using GitHub Secrets |
| 11 | +- π Optimizes build performance with layer caching |
| 12 | +- π¦ Supports multi-platform builds (AMD64, ARM64) |
6 | 13 |
|
7 | | -**Required** The path to the Dockerfile to build. |
| 14 | +## βοΈ Inputs |
8 | 15 |
|
9 | | -### `tag` |
| 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 | - | |
10 | 20 |
|
11 | | -**Required** The tag to apply to the built image. |
| 21 | +## π Secrets |
12 | 22 |
|
13 | | -## Secrets |
| 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 | |
14 | 27 |
|
15 | | -### `dockerhub_username` |
| 28 | +## π» Example Usage |
16 | 29 |
|
17 | | -**Required** The username to use to log in to Docker Hub. |
| 30 | +```yaml |
| 31 | +name: Build and Push Docker Image |
| 32 | + |
| 33 | +on: |
| 34 | + push: |
| 35 | + branches: [ main ] |
| 36 | + # Also trigger on tag creation for release versioning |
| 37 | + tags: |
| 38 | + - 'v*.*.*' |
18 | 39 |
|
19 | | -### `dockerhub_pat` |
| 40 | +jobs: |
| 41 | + build: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + steps: |
| 44 | + - uses: actions/checkout@v3 |
| 45 | + with: |
| 46 | + fetch-depth: 0 # Fetch all history for proper versioning |
20 | 47 |
|
21 | | -**Required** The personal access token to use to log in to Docker Hub. |
| 48 | + - name: Build and Push Docker Image |
| 49 | + uses: iExecBlockchainComputing/github-actions-workflows/[email protected] |
| 50 | + with: |
| 51 | + dockerfile: 'Dockerfile' |
| 52 | + tag: 'my-image:latest' |
| 53 | + secrets: |
| 54 | + dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 55 | + dockerhub_pat: ${{ secrets.DOCKERHUB_PAT }} |
| 56 | +``` |
22 | 57 |
|
23 | | -## Example usage |
| 58 | +## π Advanced Usage |
24 | 59 |
|
| 60 | +### Multi-Platform Build Example |
25 | 61 | ```yaml |
26 | | -uses: iExecBlockchainComputing/github-actions-workflows/[email protected] |
27 | | -with: |
28 | | - dockerfile: 'Dockerfile' |
29 | | - tag: 'my-image:latest' |
30 | | -secrets: |
31 | | - dockerhub_username: ${{ secrets.dockerhub_username }} |
32 | | - dockerhub_pat: ${{ secrets.dokerhub_pat }} |
| 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 |
| 79 | + |
| 80 | + - name: Build and Push Docker Image |
| 81 | + uses: iExecBlockchainComputing/github-actions-workflows/[email protected] |
| 82 | + 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 }} |
33 | 88 | ``` |
| 89 | +
|
| 90 | +## π Notes |
| 91 | +- π Ensure your Docker Hub credentials are stored securely as GitHub Secrets |
| 92 | +- π The workflow will automatically handle the Docker build and push process |
| 93 | +- π·οΈ You can specify any valid Docker tag format in the `tag` input |
| 94 | +- π
Consider using dynamic tags based on git tags, commit SHAs, or dates |
| 95 | +- π§ͺ For testing purposes, you can use the `--dry-run` flag in your own implementation |
| 96 | + |
| 97 | +## π οΈ Troubleshooting |
| 98 | +- If you encounter authentication issues, verify your Docker Hub credentials are correct and have appropriate permissions |
| 99 | +- For build failures, check your Dockerfile syntax and ensure all referenced files exist |
| 100 | +- Large images may take longer to push - consider optimizing your Dockerfile with multi-stage builds |
| 101 | +- If you need to debug the build process, you can add the `ACTIONS_STEP_DEBUG` secret set to `true` in your repository |
0 commit comments