forked from Audionut/Upload-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (123 loc) · 4.79 KB
/
docker-image.yml
File metadata and controls
139 lines (123 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
name: Create and publish Docker images
on:
release:
types:
- published
workflow_dispatch:
inputs:
pr_number:
description: 'PR number (for PR builds)'
required: false
type: string
pr_ref:
description: 'PR branch ref (for PR builds)'
required: false
type: string
pr_sha:
description: 'PR SHA (for PR builds)'
required: false
type: string
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.pr_sha || github.sha }}
fetch-depth: 0
- name: Fetch PR if building for PR
if: inputs.pr_number
run: |
echo "Building for PR #${{ inputs.pr_number }}"
echo "PR SHA: ${{ inputs.pr_sha }}"
echo "PR Ref: ${{ inputs.pr_ref }}"
# Try to fetch the PR head directly (works for both forks and same-repo PRs)
if git fetch origin pull/${{ inputs.pr_number }}/head:pr-${{ inputs.pr_number }}; then
echo "✅ Successfully fetched PR via pull/${{ inputs.pr_number }}/head"
git checkout pr-${{ inputs.pr_number }}
else
echo "⚠️ Failed to fetch via pull/ ref, trying to checkout SHA directly"
# Fallback: try to checkout the SHA if it exists
if git checkout ${{ inputs.pr_sha }}; then
echo "✅ Successfully checked out SHA ${{ inputs.pr_sha }}"
else
echo "❌ Failed to checkout PR. Using current ref."
exit 1
fi
fi
# Verify we're on the right commit
CURRENT_SHA=$(git rev-parse HEAD)
echo "Current SHA: $CURRENT_SHA"
echo "Expected SHA: ${{ inputs.pr_sha }}"
if [ "$CURRENT_SHA" = "${{ inputs.pr_sha }}" ]; then
echo "✅ Successfully checked out correct commit"
else
echo "⚠️ Warning: Current SHA doesn't match expected SHA"
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get lowercase repo name
run: |
REPO_NAME=${{ env.IMAGE_NAME }}
echo "LOWER_CASE_REPO_NAME=${REPO_NAME,,}" >> $GITHUB_ENV
- name: Get version for tagging
run: |
if [ "${{ github.event_name }}" == "release" ]; then
RELEASE_VERSION="${{ github.event.release.tag_name }}"
echo "VERSION=${RELEASE_VERSION}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ -n "${{ inputs.pr_number }}" ]; then
PR_NUMBER="${{ inputs.pr_number }}"
echo "VERSION=pr-${PR_NUMBER}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
BRANCH_NAME="${{ github.ref_name }}"
echo "VERSION=${BRANCH_NAME}" >> $GITHUB_ENV
fi
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable=${{ github.event_name == 'release' }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Output build information
run: |
echo "✅ Docker images built and pushed successfully!"
echo "🐋 Images:"
echo "${{ steps.meta.outputs.tags }}" | sed 's/^/ - /'
echo "📝 Event: ${{ github.event_name }}"
if [ "${{ github.event_name }}" == "release" ]; then
echo "📝 Triggered by release: ${{ github.event.release.tag_name }}"
elif [ -n "${{ inputs.pr_number }}" ]; then
echo "📝 Triggered by comment on PR #${{ inputs.pr_number }}"
echo "📝 PR branch: ${{ inputs.pr_ref }}"
echo "📝 PR SHA: ${{ inputs.pr_sha }}"
else
echo "📝 Triggered by manual workflow dispatch on branch: ${{ github.ref_name }}"
fi