Skip to content

Commit 76d2883

Browse files
committed
Add CI/CD
1 parent 33631e9 commit 76d2883

File tree

1 file changed

+121
-24
lines changed

1 file changed

+121
-24
lines changed

.github/workflows/branch.yml

Lines changed: 121 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,121 @@
1-
- name: Setup Python
2-
uses: actions/[email protected]
3-
with:
4-
# Version range or exact version of Python or PyPy to use, using SemVer's version range syntax. Reads from .python-version if unset.
5-
python-version: # optional
6-
# File containing the Python version to use. Example: .python-version
7-
python-version-file: # optional
8-
# Used to specify a package manager for caching in the default directory. Supported values: pip, pipenv, poetry.
9-
cache: # optional
10-
# The target architecture (x86, x64, arm64) of the Python or PyPy interpreter.
11-
architecture: # optional
12-
# Set this option if you want the action to check for the latest available version that satisfies the version spec.
13-
check-latest: # optional
14-
# The token used to authenticate when fetching Python distributions from https://github.com/actions/python-versions. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting.
15-
token: # optional, default is ${{ github.server_url == 'https://github.com' && github.token || '' }}
16-
# Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies.
17-
cache-dependency-path: # optional
18-
# Set this option if you want the action to update environment variables.
19-
update-environment: # optional, default is true
20-
# When 'true', a version range passed to 'python-version' input will match prerelease versions if no GA versions are found. Only 'x.y' version range is supported for CPython.
21-
allow-prereleases: # optional
22-
# When 'true', use the freethreaded version of Python.
23-
freethreaded: # optional
24-
1+
name: Test and deploy to branch
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
validate:
10+
name: Validate branch
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: '3.9'
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: '18'
24+
25+
- name: Install Python dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install -r requirements.txt
29+
30+
- name: Install frontend dependencies
31+
run: |
32+
cd UI
33+
npm install
34+
35+
- name: Build frontend
36+
run: |
37+
cd UI
38+
npm run build
39+
40+
# Optionnel : Ajouter des tests ici si disponibles
41+
# - name: Run backend tests
42+
# run: pytest
43+
44+
deploy:
45+
name: Deploy to branch
46+
runs-on: ubuntu-latest
47+
needs: validate
48+
steps:
49+
- uses: actions/checkout@v4
50+
51+
- name: Login to registry
52+
uses: docker/login-action@v3
53+
with:
54+
registry: ${{ secrets.DOCKER_REGISTRY }}
55+
username: ${{ secrets.DOCKER_LOGIN }}
56+
password: ${{ secrets.DOCKER_PASSWD }}
57+
58+
- name: Build and push API image
59+
uses: docker/build-push-action@v5
60+
with:
61+
context: .
62+
file: Dockerfile-api
63+
push: true
64+
tags: ${{ secrets.DOCKER_REGISTRY }}/yam-report-api:${{ github.ref_name }}
65+
66+
- name: Build and push UI image
67+
uses: docker/build-push-action@v5
68+
with:
69+
context: .
70+
file: Dockerfile-ui
71+
push: true
72+
tags: ${{ secrets.DOCKER_REGISTRY }}/yam-report-ui:${{ github.ref_name }}
73+
74+
- name: Configure SSH
75+
run: |
76+
mkdir -p ~/.ssh/
77+
echo "$SSH_KEY" > ~/.ssh/staging.key
78+
chmod 600 ~/.ssh/staging.key
79+
cat >>~/.ssh/config <<END
80+
Host staging
81+
HostName $SSH_HOST
82+
User $SSH_USER
83+
Port $SSH_PORT
84+
IdentityFile ~/.ssh/staging.key
85+
StrictHostKeyChecking no
86+
END
87+
env:
88+
SSH_USER: ${{ secrets.SSH_USER }}
89+
SSH_KEY: ${{ secrets.SSH_KEY }}
90+
SSH_HOST: ${{ secrets.SSH_HOST }}
91+
SSH_PORT: ${{ secrets.SSH_PORT }}
92+
93+
- name: Update branch environment
94+
env:
95+
DOCKER_BRANCH: ${{ github.ref_name }}
96+
DOCKER_REGISTRY: ${{ secrets.DOCKER_REGISTRY }}
97+
DOCKER_LOGIN: ${{ secrets.DOCKER_LOGIN }}
98+
DOCKER_PASSWD: ${{ secrets.DOCKER_PASSWD }}
99+
DOMAIN_URL: ${{ secrets.DOMAIN_URL }}
100+
run: |
101+
if [[ "$DOCKER_BRANCH" == "main" ]]; then
102+
export HOSTNAME="yam-report.$DOMAIN_URL"
103+
elif [[ "$DOCKER_BRANCH" == "staging" ]]; then
104+
export HOSTNAME="yam-report.$DOCKER_BRANCH.$DOMAIN_URL"
105+
else
106+
export HOSTNAME="yam-report.$DOCKER_BRANCH.$DOMAIN_URL"
107+
fi
108+
109+
ssh staging <<EOF
110+
set -euxo pipefail
111+
export DOCKER_BRANCH="$DOCKER_BRANCH"
112+
export DOCKER_REGISTRY="$DOCKER_REGISTRY"
113+
114+
cd /var/docker/yam-report/$DOCKER_BRANCH
115+
git pull origin $DOCKER_BRANCH
116+
docker compose --file docker-compose-branch.yml pull
117+
docker login -u "$DOCKER_LOGIN" -p "$DOCKER_PASSWD" $DOCKER_REGISTRY
118+
119+
HOSTNAME="$HOSTNAME" \
120+
docker compose --project-name $DOCKER_BRANCH-yam-report --file docker-compose-branch.yml up -d
121+
EOF

0 commit comments

Comments
 (0)