-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (138 loc) · 5.42 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
name: Build and Deploy for Dev, QA, and Stable
on:
pull_request:
types: [opened, reopened, synchronize]
branches:
- dev
pull_request_target:
types: [closed]
branches:
- dev
push:
tags:
- "*"
release:
types: [created]
env:
ARTIFACT_NAME: actions-demo
jobs:
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: Get Short SHA
run: |
echo "SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
- name: Print Short SHA
run: |
echo "Short SHA: ${{ env.SHA_SHORT }}"
- 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 tests"
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: |
${{ env.SHA_SHORT }}
- name: Push Docker Image if Merged
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev' }}
run: |
docker push ${{ env.SHA_SHORT }}
echo "Docker image pushed with tag ${{ env.SHA_SHORT }}"
- name: Deploy to k8
run: |
echo "Pretending to deploy to k8"
build_test_and_deploy_qa:
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Get Tag Name
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."
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."
exit 1
fi
TAG_COMMIT_SHA=$(git rev-list -n 1 $TAG_NAME)
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
echo "TAG_COMMIT_SHA=$TAG_COMMIT_SHA" >> $GITHUB_ENV
echo "SHA_SHORT=$(echo $TAG_COMMIT_SHA | cut -c1-7)" >> $GITHUB_ENV
- name: Print Tag Name and SHA
run: |
echo "Tag Name: ${{ env.TAG_NAME }}"
echo "Full SHA: ${{ env.TAG_COMMIT_SHA }}"
echo "Short SHA: ${{ env.SHA_SHORT }}"
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Pull Docker Image for QA
run: |
docker pull ${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }}
- name: Retag Docker Image with Tag Name
run: |
echo "Retagging image tag to: ${{ env.TAG_NAME }}"
docker tag ${{ env.ARTIFACT_NAME }}:${{ env.SHA_SHORT }} ${{ env.ARTIFACT_NAME }}:${{ env.TAG_NAME }}
docker images | grep ${{ env.ARTIFACT_NAME }} # List Docker images to verify retagging
- name: Push Retagged Docker Image
run: |
echo "Pushing the retagged image to Docker Hub"
docker push ${{ env.ARTIFACT_NAME }}:${{ env.TAG_NAME }}
- name: Run regression tests
run: |
echo "Fake run of regression testing"
- name: Deploy to QA k8
run: |
echo "Fake deploy to QA k8 using image ${{ env.ARTIFACT_NAME }}:${{ env.TAG_NAME }}"
build_test_and_deploy_stable:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'created'
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Get Release Tag Name
run: |
echo "RELEASE_TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
echo "RELEASE_NAME=${{ github.event.release.name }}" >> $GITHUB_ENV
- name: Print Release Tag Name and Release Name
run: |
echo "Release Tag Name: ${{ env.RELEASE_TAG_NAME }}"
echo "Release Name: ${{ env.RELEASE_NAME }}"
- name: Pull Docker Image for Stable
run: |
docker pull ${{ env.ARTIFACT_NAME }}:${{ env.RELEASE_TAG_NAME }}
- name: Retag Docker Image with Release Name
run: |
echo "Retagging image tag to: ${{ env.RELEASE_NAME }}"
docker tag ${{ env.ARTIFACT_NAME }}:${{ env.RELEASE_TAG_NAME }} ${{ env.ARTIFACT_NAME }}:${{ env.RELEASE_NAME }}
docker images # List Docker images to verify retagging
- name: Push Retagged Docker Image
run: |
echo "Pushing the retagged image to Docker Hub"
docker push ${{ env.ARTIFACT_NAME }}:${{ env.RELEASE_NAME }}
docker images # List Docker images to verify retagging
- name: Deploy to Stable k8
run: |
echo "Test to deploy to Stable k8 using image ${{ env.ARTIFACT_NAME }}:${{ env.RELEASE_NAME }}"