-
Notifications
You must be signed in to change notification settings - Fork 5
144 lines (120 loc) · 4.26 KB
/
ci.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
name: ci
on:
# Triggers the workflow on push or pull request on the main or dev branch
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
env:
IMAGE_NAME: plant-map-digital-logbook
jobs:
create-ids:
runs-on: ubuntu-latest
outputs:
image_id: ${{ steps.imageTags.outputs.image_id }}
version: ${{ steps.imageTags.outputs.version }}
steps:
- id: imageTags
name: Generate tags
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[[ "${{ github.ref }}" != "refs/tags/"* ]] && VERSION=latest
# Save to output for later steps
echo "::set-output name=image_id::$IMAGE_ID"
echo "::set-output name=version::$VERSION"
# Builds the Docker image if the corresponding files changed
build-docker:
needs: create-ids
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
# check if Dockerfile has changed
- name: Get changed files
id: changed-files-specific-docker
uses: tj-actions/changed-files@v8.1
with:
files: |
.devcontainer/Dockerfile
- name: Set up Docker Buildx
if: github.ref_type == 'tag' || steps.changed-files-specific-docker.outputs.any_changed == 'true'
uses: docker/setup-buildx-action@v1
- name: Login to Container Registry
if: github.ref_type == 'tag' || steps.changed-files-specific-docker.outputs.any_changed == 'true'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push base image
if: github.ref_type == 'tag' || steps.changed-files-specific-docker.outputs.any_changed == 'true'
uses: docker/build-push-action@v2
with:
file: .devcontainer/Dockerfile
# build on dev branch, only push on main branch
push: ${{ (GitHub.event_name != 'pull_request') && (github.ref == 'refs/heads/main') }}
tags: ${{ needs.create-ids.outputs.image_id }}:${{ needs.create-ids.outputs.version }}
cache-from: type=registry,ref=${{ needs.create-ids.outputs.image_id }}:${{ needs.create-ids.outputs.version }}
cache-to: type=inline
# Builds the code
build-code:
needs: [build-docker, create-ids]
runs-on: ubuntu-latest
container:
image: ${{ needs.create-ids.outputs.image_id }}:${{ needs.create-ids.outputs.version }}
options: --user docker --workdir /workdir
services:
postgres:
image: postgres:14
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: github_actions
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v2
# Build python classes from protobuf files
- name: Build the proto files
run: |
mkdir -p build/gRPC
python3 -m grpc_tools.protoc -I=protobuf-msgs --python_out=build/gRPC --grpc_python_out=build/gRPC protobuf-msgs/*
shell: bash
# Run migrations
- name: Run migrations
env:
PYTHONPATH: ../build/gRPC
run: |
cd django
python3 manage.py migrate
shell: bash
# Build vue frontend
- name: Build vue
run: |
cd vue
npm ci
npm run build
shell: bash
# Run pre-commit checks
- name: run pre-commit tests
run: |
pre-commit run -a
shell: bash