-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (133 loc) · 5.3 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
145
146
147
148
149
150
151
152
153
154
name: CI
on:
# Allow manual triggering of the workflow
workflow_dispatch:
# Trigger on push to main or tags matching v*
push:
branches:
- "main"
tags:
- release*
pull_request:
branches:
- main
env:
DOCKER_BUILDKIT: 1
TARGET_PLATFORMS: linux/amd64,linux/arm64
DOCKERHUB_REGISTRY: docker.io
GITHUB_REGISTRY: ghcr.io
IMAGE_NAME: kcworks
RELEASE_VERSION: unset
INVENIO_RECORD_IMPORTER_LOCAL_DATA_DIR: ${{vars.INVENIO_RECORD_IMPORTER_LOCAL_DATA_DIR}}
INVENIO_RECORD_IMPORTER_DATA_DIR: ${{vars.INVENIO_RECORD_IMPORTER_DATA_DIR}}
INVENIO_SEARCH_DOMAIN: ${{vars.INVENIO_SEARCH_DOMAIN}}
INVENIO_INSTANCE_PATH: ${{vars.INVENIO_INSTANCE_PATH}}
INVENIO_SECRET_KEY: ${{secrets.INVENIO_SECRET_KEY}}
REDIS_DOMAIN: ${{vars.REDIS_DOMAIN}}
INVENIO_SQLALCHEMY_DATABASE_URI: ${{vars.INVENIO_SQLALCHEMY_DATABASE_URI}}
POSTGRES_USER: ${{vars.POSTGRES_USER}}
POSTGRES_PASSWORD: ${{secrets.POSTGRES_PASSWORD}}
POSTGRES_DB: ${{vars.POSTGRES_DB}}
PGADMIN_DEFAULT_EMAIL: ${{secrets.PGADMIN_DEFAULT_EMAIL}}
PGADMIN_DEFAULT_PASSWORD: ${{secrets.PGADMIN_DEFAULT_PASSWORD}}
jobs:
build_and_release:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Get release version
id: get_release_version
if: startsWith(github.ref, 'refs/tags/')
run: |
# Remove 'release-' prefix if present, otherwise use the full ref name
release_version=$(echo ${{ github.ref_name }} | sed 's/^release-//')
echo "Building release version: $release_version"
echo "RELEASE_VERSION=${release_version}" >> $GITHUB_ENV
shell: bash
- name: Label latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
echo "RELEASE_VERSION=latest" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
mask-password: true
# Checks-out your repository under $GITHUB_WORKSPACE
- name: Checkout
uses: actions/checkout@v3
# Set up Python 3.9 environment
- name: Set up Python 3.9
uses: actions/setup-python@v1
with:
python-version: "3.9"
# Cache docker images so they don't rebuild every time
# - name: Cache Local Images
# id: local-images
# uses: actions/cache@v3
# with:
# path: /var/lib/docker/
# key: local-docker-directory
- name: Build Image
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
docker build . --file Dockerfile --platform=linux/amd64 --tag $DOCKERHUB_REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:$RELEASE_VERSION --tag $GITHUB_REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:$RELEASE_VERSION --tag ${{ secrets.AWS_ACCESS_KEY_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/kcworks:$RELEASE_VERSION
- name: If release version is numbered, tag it as latest as well for docker hub
if: env.RELEASE_VERSION != 'latest'
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
docker tag $DOCKERHUB_REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:$RELEASE_VERSION $DOCKERHUB_REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:latest
- name: Push Image to Docker Hub
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
docker push $DOCKERHUB_REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME --all-tags
- name: Push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
echo $ECR_REGISTRY
docker push $ECR_REGISTRY/kcworks:$RELEASE_VERSION
- name: Start containers
if: always()
run: |
touch .env
docker compose --file docker-compose.dev.yml up -d
# - name: Run unit tests
# run: cd site && pytest
- name: Destroy containers
if: always()
run: |
touch .env
docker compose --file docker-compose.dev.yml down
- name: Remove the Docker image with version number
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: docker rmi $REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:$RELEASE_VERSION
- name: Remove the Docker image with latest tag
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: docker rmi $REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:latest
- name: Remove the local image
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: docker rmi $REGISTRY/$DOCKERHUB_USERNAME/$IMAGE_NAME:$RELEASE_VERSION
- name: Prune Docker
run: docker system prune -af