Compare commits

...

74 Commits
v1.0.1 ... main

Author SHA1 Message Date
95127ca9d2
Add harbor scan report inputs and commenting functionality in action.yml
This update introduces new input parameters for enabling harbor scan report retrieval and commenting on pull requests. The action now includes logic to comment on the PR with scan results, enhancing visibility and feedback during the CI/CD process.
2025-05-09 03:10:50 +03:00
ce1914c7e6
Refactor caching mechanism in action.yml
This update modifies the caching strategy by removing the previous cache type and introducing a new registry-based cache for builds. This change enhances the efficiency of the caching process during CI/CD workflows.
2025-05-09 01:27:45 +03:00
6bc379401f
Add pull request event types to action.yml
This update introduces new event types for handling pull requests in the action.yml file, allowing for better caching and event management during CI/CD processes.
2025-05-08 23:21:34 +03:00
a126631a61
Add digest output to action.yml and set build-and-push step ID
This update introduces a new output 'digest' to the action.yml file, which captures the digest value from the build-and-push step. Additionally, the build-and-push step is now assigned an ID for better output management.
2025-05-08 20:51:26 +03:00
03f50cfd52
Add input option for pushing to registry in action.yml
Introduced a new input parameter 'push' to control whether the image should be pushed to the registry. The default value is set to true, allowing for more flexible image handling during the build process.
2025-04-28 02:46:59 +03:00
Shakar Bakr
89f03a4cdf
Merge pull request #15 from ditkrg/dependabot/github_actions/docker/build-push-action-6
Bump docker/build-push-action from 5 to 6
2024-10-17 14:36:08 +03:00
dependabot[bot]
99f10f531b
Bump docker/build-push-action from 5 to 6
Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 5 to 6.
- [Release notes](https://github.com/docker/build-push-action/releases)
- [Commits](https://github.com/docker/build-push-action/compare/v5...v6)

---
updated-dependencies:
- dependency-name: docker/build-push-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-06-18 04:46:54 +00:00
Shakar Bakr
ec4d5b9e24
Merge pull request #14 from ditkrg/dev
Fix empty tag output
2024-03-06 13:42:31 +03:00
Shakar Bakr
8282681e90
Code cleanup
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:41:09 +03:00
Shakar Bakr
4d6653c493
Refactor tag extraction in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:38:43 +03:00
Shakar Bakr
66b6967fe6
Refactor code to extract all tags from JSON
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:36:36 +03:00
Shakar Bakr
bce021de3a
Update jq command to extract the first tag from the JSON array
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:33:57 +03:00
Shakar Bakr
e00f61ebff
Fix JSON parsing issue in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:31:28 +03:00
Shakar Bakr
f4d3093cf3
Refactor JSON tag extraction in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:29:44 +03:00
Shakar Bakr
d6f88e3c21
Add debug output to action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:25:32 +03:00
Shakar Bakr
dae645a31a
Update echo statement in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:23:42 +03:00
Shakar Bakr
4bf54d5dc5
Fix array index in echo statement
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 13:22:21 +03:00
Shakar Bakr
e226c336ff
Update echo statement to include index in jsonOutput
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 12:47:49 +03:00
Shakar Bakr
70da99d3ea
Refactor JSON extraction logic in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 12:46:37 +03:00
Shakar Bakr
4a4669077d
Refactor variable name and update echo statement in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 12:44:11 +03:00
Shakar Bakr
4ddd760bcc
Commented out unnecessary code
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 12:41:56 +03:00
Shakar Bakr
ae829aefaa
Update action.yml with new tag value and add JSON parsing
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 12:38:41 +03:00
Shakar Bakr
51293f0a01
Update action.yml to use environment variable for image tag
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 11:25:43 +03:00
Shakar Bakr
3aeafad034
Fix tag extraction in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 11:23:45 +03:00
Shakar Bakr
4d412de570
Refactor variable assignment in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 11:06:23 +03:00
Shakar Bakr
042365ed11
Update tag value in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 11:04:02 +03:00
Shakar Bakr
8b912d0b2c
Refactor set_tag step in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 11:03:01 +03:00
Shakar Bakr
41b517b4ef
Update action.yml to use the 'set_tag' output
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-06 10:55:24 +03:00
Shakar Bakr
2ae135865b
Merge pull request #13 from ditkrg/update-readme
Update README.md
2024-03-04 15:41:03 +03:00
Shakar Bakr
69e4e9eb69
Update README.md 2024-03-04 15:40:49 +03:00
Shakar Bakr
76881e94db
Merge pull request #12 from ditkrg/add-major-tag-update
Update readme.md
2024-03-04 15:30:21 +03:00
Shakar Bakr
bf29bc0c88
Merge pull request #11 from ditkrg/dependabot/github_actions/actions/checkout-4
Bump actions/checkout from 3 to 4
2024-03-04 10:34:28 +03:00
Shakar Bakr
714d8939ac
Update readme.md
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-04 10:33:20 +03:00
dependabot[bot]
c65fdb5252
Bump actions/checkout from 3 to 4
Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-04 04:22:02 +00:00
Shakar Bakr
ed63a1b7e6
Merge pull request #9 from ditkrg/add-major-tag-update
Add workflow to update main version tag
2024-03-03 16:21:40 +03:00
Shakar Bakr
e30996fe96
Add workflow to update main version tag
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 16:21:18 +03:00
Shakar Bakr
041cc2e9a4
Merge pull request #8 from ditkrg/test-action
Create a Custom GitHub Action
2024-03-03 15:58:10 +03:00
Shakar Bakr
95ecdf2564
Remove unnecessary step in build workflow
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 15:44:38 +03:00
Shakar Bakr
d837188205
Add GitHub Action for building and pushing Docker images to a self-hosted registry
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 15:05:21 +03:00
Shakar Bakr
4b683d74c5
Refactor action.yml inputs and update description
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 15:04:30 +03:00
Shakar Bakr
95f45f8b52
Update outputs in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:39:50 +03:00
Shakar Bakr
49ece91376
Commented out unnecessary code and removed unused step
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:36:17 +03:00
Shakar Bakr
11452e3e2a
Remove commented out code
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:23:47 +03:00
Shakar Bakr
a0a5013f4c
Add command to save meta.json and print its contents
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:21:56 +03:00
Shakar Bakr
6fd4e7497d
Update echo command in run-command step
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:19:50 +03:00
Shakar Bakr
45da6fe554
Fix command substitution in run step
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:18:55 +03:00
Shakar Bakr
d683d2ccb2
Update action.yml to extract the first tag from JSON output
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:16:09 +03:00
Shakar Bakr
2442fd7725
Refactor Docker Buildx and add JSON output for metadata
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:12:18 +03:00
Shakar Bakr
00bb7fe36f
Refactor JSON extraction in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:08:35 +03:00
Shakar Bakr
80887ec86b
Add JSON output and extraction using jq
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:05:48 +03:00
Shakar Bakr
c14b1df2c6
Add new step to echo and cat meta.json
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:56:47 +03:00
Shakar Bakr
a77e9ff048
Update action.yml to save tags to meta.json
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:55:08 +03:00
Shakar Bakr
870e86f268
Update echo command in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:53:51 +03:00
Shakar Bakr
fa80b6522f
Update action.yml to extract the first tag from meta.json
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:51:13 +03:00
Shakar Bakr
81a3b1a470
Update echo command in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:48:21 +03:00
Shakar Bakr
cef2c97dfb
Fix echo command in action.yml
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:47:28 +03:00
Shakar Bakr
a24eedd108
Update echo command to include tags in meta.json
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:45:14 +03:00
Shakar Bakr
a4ef33dee3
Update echo command to convert JSON string to object
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:44:01 +03:00
Shakar Bakr
a58523891b
print meta
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:40:44 +03:00
Shakar Bakr
84ea6053fc
Remove unnecessary echo step and add echo step to display metadata outputs
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 13:38:50 +03:00
Shakar Bakr
8d7c95e65e
Adds more inputs
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 12:34:45 +03:00
Shakar Bakr
8ac4d29201
Adds action.yml 2024-03-03 12:22:41 +03:00
0c5f089468
Merge pull request #4 from ditkrg/dependabot/github_actions/docker/metadata-action-5
Bump docker/metadata-action from 4 to 5
2023-10-12 09:32:09 +03:00
70bc118c01
Merge pull request #5 from ditkrg/dependabot/github_actions/docker/setup-buildx-action-3
Bump docker/setup-buildx-action from 2 to 3
2023-10-12 09:31:50 +03:00
3cc7c46312
Merge pull request #6 from ditkrg/dependabot/github_actions/docker/build-push-action-5
Bump docker/build-push-action from 4 to 5
2023-10-12 09:31:25 +03:00
5d7f47d894
Merge pull request #7 from ditkrg/dependabot/github_actions/docker/login-action-3
Bump docker/login-action from 2 to 3
2023-10-12 09:30:48 +03:00
dependabot[bot]
ceb8ea4e50
Bump docker/login-action from 2 to 3
Bumps [docker/login-action](https://github.com/docker/login-action) from 2 to 3.
- [Release notes](https://github.com/docker/login-action/releases)
- [Commits](https://github.com/docker/login-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: docker/login-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 04:57:49 +00:00
dependabot[bot]
f6aad4a475
Bump docker/build-push-action from 4 to 5
Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 4 to 5.
- [Release notes](https://github.com/docker/build-push-action/releases)
- [Commits](https://github.com/docker/build-push-action/compare/v4...v5)

---
updated-dependencies:
- dependency-name: docker/build-push-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 04:57:46 +00:00
dependabot[bot]
237210ae83
Bump docker/setup-buildx-action from 2 to 3
Bumps [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) from 2 to 3.
- [Release notes](https://github.com/docker/setup-buildx-action/releases)
- [Commits](https://github.com/docker/setup-buildx-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: docker/setup-buildx-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 04:57:42 +00:00
dependabot[bot]
b6e2c2b0ce
Bump docker/metadata-action from 4 to 5
Bumps [docker/metadata-action](https://github.com/docker/metadata-action) from 4 to 5.
- [Release notes](https://github.com/docker/metadata-action/releases)
- [Upgrade guide](https://github.com/docker/metadata-action/blob/master/UPGRADE.md)
- [Commits](https://github.com/docker/metadata-action/compare/v4...v5)

---
updated-dependencies:
- dependency-name: docker/metadata-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 04:57:38 +00:00
Mohammad Tahir
feea00704f
Merge pull request #3 from ditkrg/dev
Dev
2023-03-19 16:56:48 +03:00
Mohammad Tahir
ae8a3604c4
Merge pull request #2 from 5h4k4r/patch-1
Update workflow.yaml
2023-03-19 16:56:08 +03:00
Shakar bakr
bc293af53c
Update workflow.yaml
Changes the default value of `runs-on` input variable to ubuntu-latest 

Update `docker/build-push-action` to v4
2023-03-02 12:01:20 +03:00
a8354e235b
adds timeout input 2023-02-02 11:42:04 +03:00
4 changed files with 382 additions and 7 deletions

View File

@ -0,0 +1,32 @@
name: Release
on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: version
id: version
run: |
tag=${GITHUB_REF/refs\/tags\//}
version=${tag#v}
major=${version%%.*}
echo "tag=${tag}" >> $GITHUB_OUTPUT
echo "version=${version}" >> $GITHUB_OUTPUT
echo "major=${major}" >> $GITHUB_OUTPUT
- name: force update major tag
run: |
git config user.name github-actions
git config user.email github-actions@github.com
git tag v${{ steps.version.outputs.major }} ${{ steps.version.outputs.tag }} -f
git push origin refs/tags/v${{ steps.version.outputs.major }} -f

View File

@ -25,10 +25,14 @@ on:
type: string type: string
default: reg.dev.krd default: reg.dev.krd
required: false required: false
timeout:
type: number
default: 10
required: false
runs-on: runs-on:
type: string type: string
default: "[ 'self-hosted', 'ubuntu-focal' ]" default: "[ 'ubuntu-latest' ]"
required: false required: false
secrets: secrets:
@ -43,17 +47,17 @@ jobs:
build-push: build-push:
name: Build and Push name: Build and Push
runs-on: ${{ fromJson(inputs.runs-on) }} runs-on: ${{ fromJson(inputs.runs-on) }}
timeout-minutes: 10 timeout-minutes: ${{ inputs.timeout }}
outputs: outputs:
tag: ${{ fromJson(steps.meta.outputs.json).tags[0] }} tag: ${{ fromJson(steps.meta.outputs.json).tags[0] }}
tags: ${{ steps.meta.outputs.tags }} tags: ${{ steps.meta.outputs.tags }}
steps: steps:
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2 uses: docker/setup-buildx-action@v3
- id: meta - id: meta
name: Extract Metadata name: Extract Metadata
uses: docker/metadata-action@v4 uses: docker/metadata-action@v5
with: with:
images: ${{ inputs.registry }}/${{ inputs.image }} images: ${{ inputs.registry }}/${{ inputs.image }}
flavor: latest=false flavor: latest=false
@ -71,14 +75,14 @@ jobs:
type=semver,pattern={{version}},priority=9999 #1 type=semver,pattern={{version}},priority=9999 #1
- name: Login to Registry - name: Login to Registry
uses: docker/login-action@v2 uses: docker/login-action@v3
with: with:
registry: ${{ inputs.registry }} registry: ${{ inputs.registry }}
username: ${{ secrets.username }} username: ${{ secrets.username }}
password: ${{ secrets.password }} password: ${{ secrets.password }}
- name: Build Docker images - name: Build Docker images
uses: docker/build-push-action@v3 uses: docker/build-push-action@v6
with: with:
push: true push: true
file: ${{ inputs.file }} file: ${{ inputs.file }}

160
README.md
View File

@ -1 +1,159 @@
# 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"
```
If you want to use it with our GitOps Action:
```yaml
name: Deploy
on:
push:
branches:
- dev
- main
tags:
- v[0-9]+.[0-9]+.[0-9]+
paths-ignore:
- "**.md"
- ".vscode/**"
- ".github/**"
- "!.github/workflows/tests-base.yaml"
- "!.github/workflows/deploy.yaml"
jobs:
build:
runs-on: ubuntu-latest
concurrency: build-${{ github.ref_name }}
outputs:
tag: ${{ steps.build-image.outputs.tag }}
tags: ${{ steps.build-image.outputs.tags }}
steps:
- id: build-image
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"
update-gitops:
runs-on: ubuntu-latest
concurrency: pr-${{ github.ref_name }}
needs: build
steps:
- name: Update gitops
uses: ditkrg/update-gitops-image@v1
with:
owner: ditkrg
repo: GITOPS_REPO
app-id: ${{ secrets.APP_ID }}
image-tag: ${{ needs.build.outputs.tag }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
component-name: REPO_NAME
```

181
action.yml Normal file
View File

@ -0,0 +1,181 @@
name: "Build, Scan and Push Image"
description: "Build, Scan and Push Image to Self Hosted Registry"
inputs:
push:
description: "Push to Registry"
required: false
default: "true"
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"
harbor-scan-report:
required: false
default: "true"
description: "Should try to get harbor scan report"
comment-harbor-scan-report:
required: false
default: "true"
description: "Should comment harbor scan report on PR"
harbor-scan-report-comment-marker:
required: false
default: '<!-- actions-comment-pull-request "build-and-push" -->'
description: "Comment marker for harbor scan report"
outputs:
digest:
description: "Digest"
value: ${{ steps.build-and-push.outputs.digest }}
tag:
description: "Image Tag"
value: ${{ steps.set_tag.outputs.tag }}
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: |
# Pull Request
type=ref,event=pr
type=ref,event=pr,suffix=-{{sha}},priority=8887 # 2
# 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@v6
id: build-and-push
with:
push: ${{ inputs.push }}
file: ${{ inputs.file }}
tags: ${{ steps.meta.outputs.tags }}
cache-to: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:buildcache,mode=max
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:buildcache
build-args: ${{ inputs.build-args }}
secrets: ${{ inputs.build-secrets }}
- name: Set Tag
id: set_tag
shell: bash
run: |
extracted_tag=$(echo "$json" | jq -r '.tags | .[0]')
echo "tag=$extracted_tag" >> $GITHUB_OUTPUT
env:
tags: ${{ steps.meta.outputs.tags }}
json: ${{ steps.meta.outputs.json }}
- name: Harbor Scan Results
id: harbor-scan-results
if: ${{ inputs.harbor-scan-report }} == 'true'
uses: ditkrg/harbor-scan-results-action@main
with:
image: ${{ steps.set_tag.outputs.tag }}
username: ${{ inputs.username }}
password: ${{ inputs.password }}
digest: ${{ steps.build-and-push.outputs.digest }}
- name: Comment on active branch PR
uses: actions/github-script@v7
if: ${{ inputs.comment-harbor-scan-report }} == 'true'
env:
COMMENT_MARKER: ${{ inputs.harbor-scan-report-comment-marker }}
TRIVY_SCAN_RESULTS: ${{ steps.harbor-scan-results.outputs.report-markdown }}
with:
script: |
const prs = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`
});
if (prs.data.length <= 0) {
console.log('No open PR found for the current branch');
return;
}
const pr = prs.data[0];
// Check if there's already a comment from this workflow
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number
});
const comment_marker = process.env.COMMENT_MARKER;
const buildComment = comments.data.find(comment =>
comment.body.includes(comment_marker)
);
const commentBody = `${comment_marker}
${process.env.TRIVY_SCAN_RESULTS}
`;
if (buildComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: buildComment.id,
body: commentBody
});
console.log(`Updated comment to PR #${pr.number}`);
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: commentBody
});
console.log(`Added comment to PR #${pr.number}`);
}