-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (152 loc) · 6.32 KB
/
dev.yml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
name: Build and Deploy for Dev, QA, and Stable
on:
workflow_dispatch:
inputs:
RELEASE:
required: false
type: string
description: "The name of the release you want to deploy to stable."
pull_request:
types: [opened, reopened, synchronize, closed]
branches:
- dev
push:
tags:
- "*"
release:
types: [created]
env:
ARTIFACT_NAME: actions-demo
jobs:
test:
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Preparing job
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
# Validate and sanitize the tag name
if [[ ! "$TAG_NAME" =~ ^[a-z0-9]+([._-][a-z0-9]+)*$ ]]; then
echo "# ❌ Invalid tag name: $TAG_NAME. Tag names must be lowercase and can include dashes, dots, or underscores." >> $GITHUB_STEP_SUMMARY
exit 1
fi
# Ensure tag name is a valid Docker tag
if [ ${#TAG_NAME} -gt 128 ]; then
echo "# ❌ Tag name is too long. Must be less than 128 characters." >> $GITHUB_STEP_SUMMARY
exit 1
fi
TAG_COMMIT_SHA=$(git rev-list -n 1 $TAG_NAME)
SHA_SHORT=$(echo $TAG_COMMIT_SHA | cut -c1-7)
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
echo "TAG_COMMIT_SHA=$TAG_COMMIT_SHA" >> $GITHUB_ENV
echo "SHA_SHORT=$SHA_SHORT" >> $GITHUB_ENV
IMAGE_DEV="docker.artifactory.d3d.io/${{ env.ARTIFACT_NAME }}:$SHA_SHORT"
IMAGE_QA="docker.artifactory.d3d.io/${{ env.ARTIFACT_NAME }}:$TAG_NAME"
echo "Tag Name: $TAG_NAME"
echo "Full SHA: $TAG_COMMIT_SHA"
echo "Short SHA: $SHA_SHORT"
echo "DEV: $IMAGE_DEV"
echo "QA: $IMAGE_QA"
build_test_and_deploy_dev:
runs-on: ubuntu-latest
if: |
github.event_name == 'pull_request' && github.base_ref == 'dev' ||
(github.event_name == 'pull_request_target' && github.event.action == 'closed' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev')
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Preparing job
run: |
echo "IMAGE_DEV=kennyd3d/${{ env.ARTIFACT_NAME }}:${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Run tests
run: |
echo "Fake running some test"
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: |
${{ env.IMAGE_DEV }}
- name: Push Docker Image if merged
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev' }}
run: |
docker push ${{ env.IMAGE_DEV }}
- name: Deploy to Dev
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev' }}
run: |
echo "Pretending to deploy to k8"
echo "## Successfully deployed ${{ env.IMAGE_DEV }} to Dev ✅" >> $GITHUB_STEP_SUMMARY
# Build test and deploy to qa when a release tag is created
build_test_and_deploy_qa:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'created'
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Preparing job
run: |
echo "IMAGE_DEV=kennyd3d/${{ env.ARTIFACT_NAME }}:${GITHUB_SHA::7}" >> $GITHUB_ENV
echo "IMAGE_QA=kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.release.name }}" >> $GITHUB_ENV
- name: Retag and push Docker Image with Release Name
run: |
docker pull ${{ env.IMAGE_DEV }}
docker tag ${{ env.IMAGE_DEV }} ${{ env.IMAGE_QA }}
docker push ${{ env.IMAGE_QA }}
- name: Deploy to Stable k8
run: |
echo "Test to deploy to Stable k8 using image ${{ env.IMAGE_QA }}"
- name: Give possible failure hint
if: failure()
run: |
echo "# ❌ QA Failed to Deploy: Did you make sure you tagged the same commit that merged to Dev? 🤔" >> $GITHUB_STEP_SUMMARY
echo "## DEV deployment: ${{ env.IMAGE_DEV }}" >> $GITHUB_STEP_SUMMARY
echo "## QA Deployment: ${{ env.IMAGE_QA }}" >> $GITHUB_STEP_SUMMARY
- name: Run success message on successful deployment
if: success()
run: |
echo "# 🎊 Successfully deployed ${{ env.IMAGE_QA }} to QA ✅" >> $GITHUB_STEP_SUMMARY
# Manually deploy the release given the release name
deploy_stable:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' }}
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check if release exists in remote repo
run: |
IMAGE_STABLE="kennyd3d/${{ env.ARTIFACT_NAME }}:${{ github.event.inputs.RELEASE }}"
echo "IMAGE_STABLE=$IMAGE_STABLE" >> $GITHUB_ENV
if ! docker manifest inspect $IMAGE_STABLE > /dev/null 2>&1; then
echo "# ❌ Failed Stable Deployment: $IMAGE_STABLE not found in artifactory" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Deploy release to stable
run: |
echo "Test to deploy to Stable k8 using image ${{ env.IMAGE_STABLE }}"
- name: Give possible failure hint
if: failure()
run: |
echo "## Stable Failed to Deploy ${{ env.IMAGE_STABLE }}: Did you type the correct release name? 🤔" >> $GITHUB_STEP_SUMMARY
- name: Run success message on successful deployment
if: success()
run: |
echo "# 🎊 Successfully deployed ${{ env.IMAGE_STABLE }} to Stable ✅" >> $GITHUB_STEP_SUMMARY