Skip to content

Commit 3ac6635

Browse files
Create fix-pull-request.yml
1 parent 3436674 commit 3ac6635

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
name: Run on PR from fix/* into release
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
branches:
7+
- 'release/**'
8+
9+
env:
10+
RUN_NUM: ${{ github.run_number }}
11+
12+
jobs:
13+
run_tests:
14+
if: startsWith(github.head_ref, 'fix/**')
15+
name: Run tests
16+
runs-on: ubuntu-latest
17+
permissions:
18+
packages: write
19+
contents: read
20+
attestations: write
21+
id-token: write
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
- name: Setup Node
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: 20
29+
cache: npm
30+
- name: Install dependencies
31+
run: npm ci
32+
- name: Build project
33+
run: npm run build
34+
- name: Run unit tests
35+
run: npm run test:cover
36+
37+
docker:
38+
if: startsWith(github.head_ref, 'fix/**')
39+
name: Build and push docker image
40+
needs: run_tests
41+
runs-on: ubuntu-latest
42+
steps:
43+
- name: Check out the repo
44+
uses: actions/checkout@v4
45+
- name: "Run docker build and doker push"
46+
run: echo "Tag for image ${GITHUB_REF##*/}.${RUN_NUM}"
47+
- name: Log in to Docker Hub
48+
uses: docker/login-action@v3
49+
with:
50+
username: ${{ secrets.DOCKER_USERNAME }}
51+
password: ${{ secrets.DOCKER_PASSWORD }}
52+
- name: Extract metadata for Docker
53+
id: meta
54+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
55+
with:
56+
images: |
57+
victorycenterua/client
58+
- name: Build and push Docker images
59+
id: push
60+
uses: docker/build-push-action@v6
61+
with:
62+
context: .
63+
push: true
64+
tags: ${{ steps.meta.outputs.tags }}.${{ github.run_number }}
65+
labels: ${{ steps.meta.outputs.labels }}
66+
secret-envs: |
67+
REACT_APP_BACKEND_URL=${{ secrets.REACT_APP_BACKEND_URL }}
68+
manual_approval_deploy:
69+
if: startsWith(github.head_ref, 'fix/**')
70+
needs: docker
71+
environment: stage
72+
runs-on: ubuntu-latest
73+
outputs:
74+
comment: ${{ steps.set_env.outputs.comment }}
75+
steps:
76+
- name: "Get approval message for Stage deploy"
77+
id: set_env
78+
run: |
79+
echo "comment=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
80+
-H "Accept: application/vnd.github+json" \
81+
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/approvals \
82+
| jq '.[] | {user: .user.login, comment: .comment, environments: [.environments[].name]}' \
83+
| jq -r '.comment')" >> $GITHUB_OUTPUT
84+
stage_deploy:
85+
needs: manual_approval_deploy
86+
if: contains(needs.manual_approval_deploy.outputs.comment, 'approve') && startsWith(github.head_ref, 'fix/**')
87+
runs-on: self-hosted
88+
outputs:
89+
backend_tag: ${{ steps.backend_tag.outputs.backend_tag }}
90+
frontend_tag: ${{ steps.frontend_tag.outputs.frontend_tag }}
91+
steps:
92+
- run: echo ${{ needs.manual_approval_deploy.outputs.comment }}
93+
- name: "Set backend tag"
94+
id: backend_tag
95+
run: echo "backend_tag=$(docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep -m 1 "victorycenterua/backend:" | cut -d "-" -f2 | head -c -1)" >> $GITHUB_OUTPUT
96+
- name: "Set frontend tag"
97+
id: frontend_tag
98+
run: echo "frontend_tag=$(docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep -m 1 "victorycenterua/client:" | cut -d "-" -f2 | head -c -1)" >> $GITHUB_OUTPUT
99+
- name: Checkout
100+
uses: actions/checkout@v2
101+
with:
102+
repository: ita-social-projects/VictoryCenter-DevOps
103+
- name: Stop all containers
104+
run: docker stop nginx certbot frontend backend
105+
- name: Delete additon info
106+
run: docker system prune -f
107+
- name: Set up enviroments and Deploy
108+
env:
109+
INITIAL_ADMIN_EMAIL: ${{ secrets.INITIAL_ADMIN_EMAIL }}
110+
INITIAL_ADMIN_PASSWORD: ${{ secrets.INITIAL_ADMIN_PASSWORD }}
111+
JWTOPTIONS_SECRETKEY: ${{ secrets.JWTOPTIONS_SECRETKEY }}
112+
JWTOPTIONS_REFRESH_TOKEN_SECRETKEY: ${{ secrets.JWTOPTIONS_REFRESH_TOKEN_SECRETKEY }}
113+
DOCKER_TAG_FRONTEND: ${{ steps.frontend_tag.outputs.frontend_tag }}
114+
DOCKER_TAG_BACKEND: ${GITHUB_REF##*/}.${RUN_NUM}
115+
DB_CONNECTION_STRING: ${{ secrets.DB_CONNECTION_STRING }}
116+
run: |
117+
docker compose up -d
118+
119+
120+
manual_approval_rollback:
121+
if: startsWith(github.head_ref, 'fix/**')
122+
needs: stage_deploy
123+
environment: stage
124+
runs-on: ubuntu-latest
125+
outputs:
126+
comment: ${{ steps.set_comment_rollback.outputs.comment }}
127+
steps:
128+
- name: "Get rollback message for Stage deploy"
129+
id: set_comment_rollback
130+
run: |
131+
echo "comment=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
132+
-H "Accept: application/vnd.github+json" \
133+
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/approvals \
134+
| jq -r '.[0].comment')" >> $GITHUB_OUTPUT
135+
stage_rollback:
136+
needs: [manual_approval_rollback, stage_deploy]
137+
if: contains(needs.manual_approval_rollback.outputs.comment, 'approve') && startsWith(github.head_ref, 'fix/**')
138+
runs-on: self-hosted
139+
steps:
140+
- name: Rollback tag
141+
run: echo "test_val_2=${GITHUB_REF##*/}.$((RUN_NUM - 1))"
142+
- name: Rollback tags
143+
run: echo ${{ needs.stage_deploy.outputs.backend_tag }}
144+
- name: Checkout
145+
uses: actions/checkout@v2
146+
with:
147+
repository: ita-social-projects/VictoryCenter-DevOps
148+
- name: Stop all containers
149+
run: docker stop nginx certbot frontend backend
150+
- name: Delete additon info
151+
run: docker system prune -f
152+
- name: Set up enviroments and Deploy
153+
env:
154+
INITIAL_ADMIN_EMAIL: ${{ secrets.INITIAL_ADMIN_EMAIL }}
155+
INITIAL_ADMIN_PASSWORD: ${{ secrets.INITIAL_ADMIN_PASSWORD }}
156+
JWTOPTIONS_SECRETKEY: ${{ secrets.JWTOPTIONS_SECRETKEY }}
157+
JWTOPTIONS_REFRESH_TOKEN_SECRETKEY: ${{ secrets.JWTOPTIONS_REFRESH_TOKEN_SECRETKEY }}
158+
DOCKER_TAG_FRONTEND: ${{ needs.stage_deploy.outputs.frontend_tag }}
159+
DOCKER_TAG_BACKEND: ${{ needs.stage_deploy.outputs.backend_tag }}
160+
DB_CONNECTION_STRING: ${{ secrets.DB_CONNECTION_STRING }}
161+
run: |
162+
docker compose up -d

0 commit comments

Comments
 (0)