Skip to content

Commit

Permalink
Merge pull request #8 from ditkrg/test-action
Browse files Browse the repository at this point in the history
Create a Custom GitHub Action
  • Loading branch information
5h4k4r authored Mar 3, 2024
2 parents 0c5f089 + 95ecdf2 commit 041cc2e
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 1 deletion.
105 changes: 104 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,104 @@
# build-image-action
# Build Image Workflow

This GitHub Action automates the process of building and pushing a Docker image to a self-hosted registry. The workflow includes setting up Docker Buildx, extracting metadata, logging in to the registry, and building and pushing the Docker image.

## Inputs

### `image` (required)
- Description: Image Name
- Required: true

### `build-args` (optional)
- Description: Build Arguments
- Required: false

### `file` (optional)
- Description: Dockerfile Path
- Required: false

### `registry` (required)
- Description: Registry URL
- Required: true
- Default: reg.dev.krd

### `username` (required)
- Description: Username for the registry
- Required: true

### `password` (required)
- Description: Password for the registry
- Required: true

### `build-secrets` (optional)
- Description: Build Secrets
- Required: false

## Outputs

### `tag`
- Description: Image Tag
- Value: ${{ steps.meta.outputs.tags[0] }}

### `tags`
- Description: Image Tags
- Value: ${{ steps.meta.outputs.tags }}

## Workflow Steps

1. **Set up Docker Buildx:**
- Uses: docker/setup-buildx-action@v3

2. **Extract Metadata:**
- Uses: docker/metadata-action@v5
- Inputs:
- `images`: ${{ inputs.registry }}/${{ inputs.image }}
- `flavor`: latest=false
- `tags`:
- Cache: `type=raw,value=${{ github.ref_name }}-cache`
- Branches: `type=ref,event=branch`, `type=ref,event=branch,suffix=-{{sha}},priority=8888`
- Releases: `type=semver,pattern={{major}}`, `type=semver,pattern={{major}}.{{minor}}`, `type=semver,pattern={{version}},priority=9999`

3. **Login to Registry:**
- Uses: docker/login-action@v3
- Inputs:
- `registry`: ${{ inputs.registry }}
- `username`: ${{ inputs.username }}
- `password`: ${{ inputs.password }}

4. **Build Docker images:**
- Uses: docker/build-push-action@v5
- Inputs:
- `push`: true
- `file`: ${{ inputs.file }}
- `tags`: ${{ steps.meta.outputs.tags }}
- `cache-to`: `type=inline`
- `cache-from`: `type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:${{ github.ref_name }}-cache`
- `build-args`: ${{ inputs.build-args }}
- `secrets`: ${{ inputs.build-secrets }}

## Example Usage

```yaml
name: Build Image Workflow
on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Build and Push Image
uses: ditkrg/build-image-workflow@v1
with:
image: "my-docker-image"
registry: "my-registry.example.com"
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
build-args: "EXAMPLE=123"
build-secrets: "EXAMPLE=****"
file: "path/to/Dockerfile"
```
Feel free to customize the inputs and adjust the workflow based on your specific requirements.
77 changes: 77 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: "Build, Scan and Push Image"
description: "Build, Scan and Push Image to Self Hosted Registry"
inputs:
image:
description: "Image Name"
required: true
build-args:
description: "Build Arguments"
required: false
file:
description: "Dockerfile Path"
required: false
registry:
description: "Registry URL"
required: true
default: reg.dev.krd

username:
required: true
description: "Username for registry"
password:
required: true
description: "Password for registry"
build-secrets:
required: false
description: "Build Secrets"

outputs:
tag:
description: "Image Tag"
value: ${{ steps.meta.outputs.tags[0] }}
tags:
description: "Image Tags"
value: ${{ steps.meta.outputs.tags }}

runs:
using: "composite"
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- id: meta
name: Extract Metadata
uses: docker/metadata-action@v5
with:
images: ${{ inputs.registry }}/${{ inputs.image }}
flavor: latest=false
tags: |
# Cache
type=raw,value=${{ github.ref_name }}-cache
# Branches
type=ref,event=branch
type=ref,event=branch,suffix=-{{sha}},priority=8888 # 2
# Releases
type=semver,pattern={{major}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{version}},priority=9999 #1
- name: Login to Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ inputs.username }}
password: ${{ inputs.password }}

- name: Build Docker images
uses: docker/build-push-action@v5
with:
push: true
file: ${{ inputs.file }}
tags: ${{ steps.meta.outputs.tags }}
cache-to: type=inline
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:${{ github.ref_name }}-cache
build-args: ${{ inputs.build-args }}
secrets: ${{ inputs.build-secrets }}

0 comments on commit 041cc2e

Please sign in to comment.