-
Notifications
You must be signed in to change notification settings - Fork 0
119 lines (93 loc) · 3.25 KB
/
main.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
name: CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.10.6
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Run tests
run: |
pytest -v app/tests
tag_version:
runs-on: ubuntu-latest
needs: test
outputs:
version: ${{ steps.fetch_version.outputs.version }}
steps:
- uses: actions/checkout@v3
# Step to fetch the version, validate, and tag the code
- name: Fetch version and create Git tag
id: fetch_version
run: |
VERSION=$(cat VERSION)
echo "Validating version format for $VERSION..."
# Check if the version follows Semantic Versioning (MAJOR.MINOR.PATCH)
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "ERROR: The version '$VERSION' does not follow Semantic Versioning (e.g., 1.0.0)."
exit 1
fi
# Prepend 'v' if not present
if [[ "$VERSION" != v* ]]; then
VERSION="v$VERSION"
echo "Prepending 'v'. New version is $VERSION"
fi
# Check if the version already has a Git tag
if git rev-parse "$VERSION" >/dev/null 2>&1; then
echo "ERROR: Version $VERSION already has a Git tag!"
exit 1
fi
# Configure Git user for tagging
git config user.name "GitHub Actions"
git config user.email "[email protected]"
# Create the Git tag
git tag "$VERSION"
# Push the tag to the repository
git push origin "$VERSION"
# Set the version as an output using GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
needs: tag_version
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and tag Docker image using the version fetched in the previous job
- name: Build and tag Docker image
run: |
docker build -t ghcr.io/inab/observatory-api:${{ needs.tag_version.outputs.version }} -t ghcr.io/inab/observatory-api:latest .
# Push Docker image to the registry
- name: Push Docker image
run: |
docker push ghcr.io/inab/observatory-api:${{ needs.tag_version.outputs.version }}
docker push ghcr.io/inab/observatory-api:latest