-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
110 lines (100 loc) · 3.53 KB
/
action.yml
File metadata and controls
110 lines (100 loc) · 3.53 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
name: 'Docker Golden Image: Build PR Image'
description: 'Builds and pushes a Docker image for a Pull Request and comments on the PR with tags.'
inputs:
dockerfile_path:
description: 'Path to Dockerfile'
required: false
default: './Dockerfile'
docker_context:
description: 'Docker build context path'
required: false
default: '.'
registry:
description: 'Container registry URL'
required: false
default: 'ghcr.io'
image_name:
description: 'Image name (defaults to repository name)'
required: false
default: ''
build_args:
description: 'Docker build arguments (comma-separated KEY=VALUE pairs)'
required: false
default: ''
platforms:
description: 'Target platforms for build'
required: false
default: 'linux/amd64'
registry_username:
description: 'Registry username'
required: false
registry_password:
description: 'Registry password'
required: true
runs:
using: 'composite'
steps:
- name: Checkout code
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Set image name
id: image
shell: bash
run: |
if [ -z "${{ inputs.image_name }}" ]; then
echo "name=${{ github.repository }}" >> $GITHUB_OUTPUT
else
echo "name=${{ inputs.image_name }}" >> $GITHUB_OUTPUT
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
- name: Log in to Container Registry
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
with:
registry: ${{ inputs.registry }}
username: ${{ inputs.registry_username || github.actor }}
password: ${{ inputs.registry_password }}
- name: Parse build args
id: build-args
shell: bash
run: |
if [ -n "${{ inputs.build_args }}" ]; then
echo "args<<EOF" >> $GITHUB_OUTPUT
echo "${{ inputs.build_args }}" | tr ',' '\n' >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "args=" >> $GITHUB_OUTPUT
fi
- name: Extract metadata
id: meta
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5
with:
images: ${{ inputs.registry }}/${{ steps.image.outputs.name }}
tags: |
type=ref,event=pr,prefix=pr-
type=sha,prefix=pr-{{branch}}-
- name: Build and push PR image
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5
with:
context: ${{ inputs.docker_context }}
file: ${{ inputs.dockerfile_path }}
platforms: ${{ inputs.platforms }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ steps.build-args.outputs.args }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Comment PR with image tags
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const tags = `${{ steps.meta.outputs.tags }}`.split('\n');
const comment = `### 🐳 Docker Image Built Successfully\n\n` +
`**Image Tags:**\n${tags.map(tag => `- \`${tag}\``).join('\n')}\n\n` +
`**Pull command:**\n\`\`\`bash\ndocker pull ${tags[0]}\n\`\`\``;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});