-
Notifications
You must be signed in to change notification settings - Fork 1
183 lines (172 loc) · 5.43 KB
/
build_docker.yaml
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: build_docker
on:
workflow_dispatch:
push:
branches:
- 'main'
tags:
- 'v*'
pull_request:
env:
REGISTRY: ghcr.io
IMAGE: ghcr.io/openmethane/openmethane-prior
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
platform:
# - linux/arm64
- linux/amd64
permissions:
contents: read
packages: write
outputs:
digest: ${{ steps.build.outputs.digest }}
# Builds and pushes the image
# Tags the image with the PR that it is linked to
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: "${{ env.IMAGE }}"
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
- name: Build and push image
uses: docker/build-push-action@v5
id: build
with:
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ steps.meta.outputs.tags }}
push: true
pull: false
cache-from: type=gha
cache-to: type=gha,mode=max
test-unit:
# Simple test suite to verify that the docker container works as expected
timeout-minutes: 10
runs-on: ubuntu-latest
needs: build
permissions:
contents: read
packages: read
container:
image: ghcr.io/openmethane/openmethane@${{ needs.build.outputs.digest }}
credentials:
username: ${{ github.actor }}
password: ${{ secrets.github_token }}
steps:
- name: Run a quick test suite
run: |
cd /opt/project
poetry run python -m pytest -r a -v tests/test_create_domain_info.py
env:
CDSAPI_KEY: ${{ secrets.CDSAPI_ADS_KEY }}
CDSAPI_URL: https://ads.atmosphere.copernicus.eu/api/v2
- name: Upload artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: docker-artifacts
path: /opt/project/data
# Tag the latest image if running on the main branch
# TODO: Handle tagged builds
tag-latest-image:
runs-on: ubuntu-latest
needs: [ test-unit ]
if: github.ref == 'refs/heads/main'
permissions:
contents: read
packages: write
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Checkout code
uses: actions/checkout@v4
- name: Login to Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: "${{ env.IMAGE }}"
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main'}}
- name: Push latest image
uses: docker/build-push-action@v5
id: build
with:
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
push: true
load: true
# Push the image to ECR as well
push-ecr:
runs-on: ubuntu-latest
needs: [test-unit, build ]
permissions:
contents: read
packages: read
env:
GHCR_IMAGE_ID: ghcr.io/openmethane/openmethane@${{ needs.build.outputs.digest }}
ECR_IMAGE: 654654509571.dkr.ecr.ap-southeast-2.amazonaws.com/github/openmethane/openmethane
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Checkout code
uses: actions/checkout@v4
- name: Login to Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
# TODO: Use the OIDC token instead of the access key
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-2
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
- name: Pull built docker image
run: |
docker pull ${{ env.GHCR_IMAGE_ID }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: "${{ env.ECR_IMAGE }}"
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main'}}
- name: Tag and push docker image to Amazon ECR
run: |
echo "${{ steps.meta.outputs.tags }}"
for tag in "${{ steps.meta.outputs.tags }}"; do
docker tag "${{ env.GHCR_IMAGE_ID }}" "$tag"
docker push "$tag"
done