-
Notifications
You must be signed in to change notification settings - Fork 0
148 lines (129 loc) · 4.95 KB
/
docker-build-pr.yml
File metadata and controls
148 lines (129 loc) · 4.95 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
140
141
142
143
144
145
146
147
148
# Copyright The Linux Foundation and each contributor to LFX.
# SPDX-License-Identifier: MIT
---
name: Deploy Branch to Development
on:
pull_request:
types: [closed, labeled, unlabeled, synchronize]
permissions:
contents: read
packages: write
pull-requests: write
issues: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
# Only build a container if the PR is still open, it's labeled with
# 'deploy-preview' and does not come from a fork.
if: >
github.event.action != 'closed' &&
github.event.pull_request.state == 'open' &&
github.event.pull_request.head.repo.fork == false &&
contains(github.event.pull_request.labels.*.name, 'deploy-preview')
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=pr,prefix=changelog-pr-
labels: |
org.opencontainers.image.title=LFX Changelog
org.opencontainers.image.description=Linux Foundation LFX Changelog application
org.opencontainers.image.vendor=The Linux Foundation
org.opencontainers.image.licenses=MIT
org.opencontainers.image.documentation=https://github.com/${{ github.repository }}/blob/main/README.md
org.opencontainers.image.source=https://github.com/${{ github.repository }}
com.github.actions.run_id=${{ github.run_id }}
com.github.actions.run_number=${{ github.run_number }}
com.github.actions.workflow=${{ github.workflow }}
- name: Build and push Docker image
id: docker-build
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_ENV=dev
- name: Comment deployment URL on PR
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
with:
script: |
const prNumber = process.env.PR_NUMBER;
const deploymentUrl = `https://changelog-pr-${prNumber}.dev.v2.cluster.linuxfound.info`;
const comment = `## 🚀 Deployment Status
Your branch has been deployed to: ${deploymentUrl}
**Deployment Details:**
- Environment: Development
- Namespace: changelog-pr-${prNumber}
- ArgoCD App: changelog-pr-${prNumber}
The deployment will be automatically removed when this PR is closed.`;
// Check if we already commented
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## 🚀 Deployment Status')
);
if (botComment) {
await github.rest.issues.updateComment({
comment_id: botComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
}
cleanup-on-close:
if: >
(
(github.event.action == 'closed' && contains(github.event.pull_request.labels.*.name, 'deploy-preview')) ||
(github.event.action == 'unlabeled' && github.event.label.name == 'deploy-preview')
)
runs-on: ubuntu-latest
steps:
- name: Comment removal on PR
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
with:
script: |
const prNumber = process.env.PR_NUMBER;
const comment = `## 🧹 Deployment Removed
The deployment for PR #${prNumber} has been removed.`;
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});