Skip to content

Commit 6fa2a83

Browse files
committed
fix: Correct Docker Hub image handling and fix indentation
- Fix Docker Hub credentials check to use proper step output - Conditionally include Docker Hub image in metadata only when credentials exist - Fix indentation consistency in build-binaries.yml and docker-push.yml - Prevents empty image entries in metadata action when Docker Hub not configured
1 parent 9b70570 commit 6fa2a83

File tree

2 files changed

+101
-83
lines changed

2 files changed

+101
-83
lines changed
Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
11
name: Build Binaries
22

33
on:
4-
workflow_run:
5-
workflows: ["build"]
6-
types: [completed]
7-
branches: [main, develop]
8-
workflow_dispatch:
4+
workflow_run:
5+
workflows: ["build"]
6+
types: [completed]
7+
branches: [main, develop]
8+
workflow_dispatch:
99

1010
permissions:
1111
contents: read
1212
actions: read
1313
checks: read
1414

1515
env:
16-
GO_VERSION: "1.25.1"
16+
GO_VERSION: "1.25.1"
1717

1818
jobs:
19-
build-binaries:
20-
name: Build Multi-Platform Binaries
21-
runs-on: ubuntu-latest
22-
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'workflow_dispatch')) }}
23-
strategy:
24-
matrix:
25-
include:
26-
- os: linux
27-
arch: amd64
28-
output: github-repo-linux-amd64
29-
- os: linux
30-
arch: arm64
31-
output: github-repo-linux-arm64
32-
- os: darwin
33-
arch: amd64
34-
output: github-repo-darwin-amd64
35-
- os: darwin
36-
arch: arm64
37-
output: github-repo-darwin-arm64
38-
- os: windows
39-
arch: amd64
40-
output: github-repo-windows-amd64.exe
41-
steps:
42-
- name: Check if lint workflow passed
43-
if: github.event_name == 'workflow_run'
44-
uses: actions/github-script@v7
45-
with:
46-
script: |
47-
const { data: runs } = await github.rest.actions.listWorkflowRuns({
48-
owner: context.repo.owner,
49-
repo: context.repo.repo,
50-
workflow_id: 'lint.yaml',
51-
head_sha: '${{ github.event.workflow_run.head_sha }}',
52-
per_page: 1
53-
});
54-
if (runs.workflow_runs.length > 0 && runs.workflow_runs[0].conclusion !== 'success') {
55-
core.setFailed('Lint workflow did not pass');
56-
}
57-
- name: Checkout code
58-
uses: actions/checkout@v5
59-
with:
60-
persist-credentials: false
19+
build-binaries:
20+
name: Build Multi-Platform Binaries
21+
runs-on: ubuntu-latest
22+
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'workflow_dispatch')) }}
23+
strategy:
24+
matrix:
25+
include:
26+
- os: linux
27+
arch: amd64
28+
output: github-repo-linux-amd64
29+
- os: linux
30+
arch: arm64
31+
output: github-repo-linux-arm64
32+
- os: darwin
33+
arch: amd64
34+
output: github-repo-darwin-amd64
35+
- os: darwin
36+
arch: arm64
37+
output: github-repo-darwin-arm64
38+
- os: windows
39+
arch: amd64
40+
output: github-repo-windows-amd64.exe
41+
steps:
42+
- name: Check if lint workflow passed
43+
if: github.event_name == 'workflow_run'
44+
uses: actions/github-script@v7
45+
with:
46+
script: |
47+
const { data: runs } = await github.rest.actions.listWorkflowRuns({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
workflow_id: 'lint.yaml',
51+
head_sha: '${{ github.event.workflow_run.head_sha }}',
52+
per_page: 1
53+
});
54+
if (runs.workflow_runs.length > 0 && runs.workflow_runs[0].conclusion !== 'success') {
55+
core.setFailed('Lint workflow did not pass');
56+
}
57+
- name: Checkout code
58+
uses: actions/checkout@v5
59+
with:
60+
persist-credentials: false
6161

62-
- name: Set up Go
63-
uses: actions/setup-go@v5
64-
with:
65-
go-version: ${{ env.GO_VERSION }}
62+
- name: Set up Go
63+
uses: actions/setup-go@v5
64+
with:
65+
go-version: ${{ env.GO_VERSION }}
6666

67-
- name: Build binary for ${{ matrix.os }}/${{ matrix.arch }}
68-
env:
69-
GOOS: ${{ matrix.os }}
70-
GOARCH: ${{ matrix.arch }}
71-
CGO_ENABLED: 0
72-
VERSION: ${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.event.workflow_run.head_branch || 'unknown' }}
73-
COMMIT_HASH: ${{ github.event_name == 'workflow_dispatch' && github.sha || github.event.workflow_run.head_sha || github.sha }}
74-
OUTPUT_NAME: ${{ matrix.output }}
75-
run: |
76-
go build -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.GitCommitHash=${COMMIT_HASH}' -X 'main.BuiltAt=$(date -u +%Y-%m-%dT%H:%M:%SZ)'" -o "${OUTPUT_NAME}"
67+
- name: Build binary for ${{ matrix.os }}/${{ matrix.arch }}
68+
env:
69+
GOOS: ${{ matrix.os }}
70+
GOARCH: ${{ matrix.arch }}
71+
CGO_ENABLED: 0
72+
VERSION: ${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.event.workflow_run.head_branch || 'unknown' }}
73+
COMMIT_HASH: ${{ github.event_name == 'workflow_dispatch' && github.sha || github.event.workflow_run.head_sha || github.sha }}
74+
OUTPUT_NAME: ${{ matrix.output }}
75+
run: |
76+
go build -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.GitCommitHash=${COMMIT_HASH}' -X 'main.BuiltAt=$(date -u +%Y-%m-%dT%H:%M:%SZ)'" -o "${OUTPUT_NAME}"
7777
78-
- name: Upload binary artifact
79-
uses: actions/upload-artifact@v4
80-
with:
81-
name: ${{ matrix.output }}
82-
path: ${{ matrix.output }}
83-
retention-days: 30
78+
- name: Upload binary artifact
79+
uses: actions/upload-artifact@v4
80+
with:
81+
name: ${{ matrix.output }}
82+
path: ${{ matrix.output }}
83+
retention-days: 30

.github/workflows/docker-push.yml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: Docker Build and Push
22

33
on:
4-
workflow_run:
5-
workflows: ["build"]
6-
types: [completed]
7-
branches: [main, develop]
8-
workflow_dispatch:
4+
workflow_run:
5+
workflows: ["build"]
6+
types: [completed]
7+
branches: [main, develop]
8+
workflow_dispatch:
99

1010
permissions:
1111
contents: read
@@ -16,14 +16,14 @@ permissions:
1616
checks: read
1717

1818
env:
19-
REGISTRY: ghcr.io
20-
IMAGE_NAME: ${{ github.repository }}
19+
REGISTRY: ghcr.io
20+
IMAGE_NAME: ${{ github.repository }}
2121

2222
jobs:
23-
docker-build-push:
24-
name: Build and Push Docker Image
25-
runs-on: ubuntu-latest
26-
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'develop')) }}
23+
docker-build-push:
24+
name: Build and Push Docker Image
25+
runs-on: ubuntu-latest
26+
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'develop')) }}
2727
steps:
2828
- name: Check if lint workflow passed
2929
if: github.event_name == 'workflow_run'
@@ -56,20 +56,38 @@ jobs:
5656
username: ${{ github.actor }}
5757
password: ${{ secrets.GITHUB_TOKEN }}
5858

59+
- name: Prepare Docker Hub image name
60+
id: dockerhub
61+
run: |
62+
if [ -n "${{ secrets.DOCKERHUB_TOKEN }}" ] && [ -n "${{ secrets.DOCKERHUB_USERNAME }}" ]; then
63+
echo "image=${{ secrets.DOCKERHUB_USERNAME }}/pvtr-github-repo" >> $GITHUB_OUTPUT
64+
echo "has_credentials=true" >> $GITHUB_OUTPUT
65+
else
66+
echo "has_credentials=false" >> $GITHUB_OUTPUT
67+
fi
68+
5969
- name: Log in to Docker Hub
6070
uses: docker/login-action@v3
61-
if: secrets.DOCKERHUB_TOKEN != ''
71+
if: steps.dockerhub.outputs.has_credentials == 'true'
6272
with:
6373
username: ${{ secrets.DOCKERHUB_USERNAME }}
6474
password: ${{ secrets.DOCKERHUB_TOKEN }}
6575

76+
- name: Prepare images list for metadata
77+
id: prepare-images
78+
run: |
79+
echo "images<<EOF" >> $GITHUB_OUTPUT
80+
echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT
81+
if [ "${{ steps.dockerhub.outputs.has_credentials }}" = "true" ]; then
82+
echo "${{ steps.dockerhub.outputs.image }}" >> $GITHUB_OUTPUT
83+
fi
84+
echo "EOF" >> $GITHUB_OUTPUT
85+
6686
- name: Extract metadata
6787
id: meta
6888
uses: docker/metadata-action@v5
6989
with:
70-
images: |
71-
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
72-
${{ secrets.DOCKERHUB_USERNAME }}/pvtr-github-repo
90+
images: ${{ steps.prepare-images.outputs.images }}
7391
tags: |
7492
type=raw,value=latest,enable=${{ github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main' }}
7593
type=ref,event=branch

0 commit comments

Comments
 (0)