-
Notifications
You must be signed in to change notification settings - Fork 0
143 lines (134 loc) · 4.57 KB
/
build-docker.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
name: Build the Docker Image
on:
workflow_call:
inputs:
package_name:
description: "The name of the python package"
required: true
type: string
image_name:
description: "Name of the docker image to build"
required: false
type: string
default: ${{ github.event.repository.name }}
test_image:
description: "Runs the image test if true"
required: false
type: boolean
default: false
outputs:
image_artifact_name:
description: "Keyword of the image artifact."
value: ${{ jobs.build.outputs.artifact_name }}
image_artifact_file:
description: "Path of the image artifact."
value: ${{ jobs.build.outputs.artifact_file }}
image_name:
description: "Full name of the docker image."
value: ${{ jobs.build.outputs.image_name }}
permissions:
contents: read
jobs:
check_file:
runs-on: ubuntu-latest
outputs:
dockerfile_exists: ${{ steps.check-file.outputs.file_exists}}
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check file existence
id: check-file
run: |
if [ -f "Dockerfile" ]; then
file_exists="true"
else
file_exists="false"
fi
echo "file_exists=${file_exists}" >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
needs: [check_file]
outputs:
image_name: ${{ steps.build-docker-image.outputs.image_name }}
artifact_file: ${{ steps.build-docker-image.outputs.artifact_file }}
artifact_name: ${{ steps.build-docker-image.outputs.artifact_name }}
if: ${{ needs.check_file.outputs.dockerfile_exists }} == "true"
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install .
- name: Get package version
run: |
version=$(python -c "import ${{ inputs.package_name}}; print(${{ inputs.package_name}}.__version__)")
echo "PACKAGE_VERSION=$version" >> $GITHUB_ENV
- name: Build image name
id: build-docker-image
run: |
IMAGE_NAME=${{ inputs.image_name }}:${PACKAGE_VERSION}
ARTIFACT_FILE=image.tar
ARTIFACT_NAME=docker-image
echo "IMAGE_NAME=${IMAGE_NAME}" > /tmp/image-name.txt
echo "IMAGE_NAME: ${IMAGE_NAME}"
docker build -t $IMAGE_NAME .
docker save --output /tmp/${ARTIFACT_FILE} $IMAGE_NAME
echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT
echo "artifact_file=${ARTIFACT_FILE}" >> $GITHUB_OUTPUT
echo "artifact_name=${ARTIFACT_NAME}" >> $GITHUB_OUTPUT
- name: Upload docker image as artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.build-docker-image.outputs.artifact_name}}
path: /tmp/${{ steps.build-docker-image.outputs.artifact_file}}
test:
runs-on: ubuntu-latest
needs: [check_file, build]
if: ${{ (inputs.test_image == true) && (needs.check_file.outputs.dockerfile_exists == 'true') }}
env:
image_name: ${{ needs.build.outputs.image_name }}
artifact_file: ${{ needs.build.outputs.artifact_file }}
artifact_name: ${{ needs.build.outputs.artifact_name }}
steps:
- name: Download docker image
uses: actions/download-artifact@v4
with:
name: ${{ env.artifact_name }}
path: /tmp
- name: Load image
run: |
docker load --input /tmp/${{ env.artifact_file }}
docker image ls -a
- name: Run Docker Image
run: docker run --name test-container -d ${{ env.image_name }}
- name: Check container is running
run: sleep 5 && docker ps -a
- name: Check if container has exited already
run: |
if docker ps -a | grep -q "Exited"; then
echo "Container exited. probably broken"
exit 1
else
echo "Container still running, should be okay."
fi
- name: Stop and remove container
run: |
docker stop test-container
docker rm test-container
teardown:
if: failure()
needs: [build, test]
runs-on: ubuntu-latest
steps:
- uses: geekyeggo/delete-artifact@v5
with:
name: ${{ env.artifact_name }}