8000 ci: move docker container creation to an action · russell/libgit2@d15be59 · GitHub
[go: up one dir, main page]

Skip to content

Commit d15be59

Browse files
committed
ci: move docker container creation to an action
1 parent ee1d9a0 commit d15be59

File tree

4 files changed

+143
-99
lines changed

4 files changed

+143
-99
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Run a build step in a container or directly on the Actions runner
2+
name: Download or Build Container
3+
description: Download a container from the package registry, or build it if it's not found
4+
5+
inputs:
6+
container:
7+
description: Container name
8+
type: string
9+
required: true
10+
dockerfile:
11+
description: Dockerfile
12+
type: string
13+
base:
14+
description: Container base
15+
type: string
16+
registry:
17+
description: Docker registry to read and publish to
18+
type: string
19+
default: ghcr.io
20+
config-path:
21+
description: Path to Dockerfiles
22+
type: string
23+
github_token:
24+
description: GitHub Token
25+
type: string
26+
27+
runs:
28+
using: 'composite'
29+
steps:
30+
- name: Download container
31+
run: |
32+
IMAGE_NAME="${{ inputs.container }}"
33+
DOCKERFILE_PATH="${{ inputs.dockerfile }}"
34+
DOCKER_REGISTRY="${{ inputs.registry }}"
35+
DOCKERFILE_ROOT="${{ inputs.config-path }}"
36+
37+
if [ "${DOCKERFILE_PATH}" = "" ]; then
38+
DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${IMAGE_NAME}"
39+
else
40+
DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${DOCKERFILE_PATH}"
41+
fi
42+
43+
GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
44+
echo "::: git worktree is ${GIT_WORKTREE}"
45+
cd "${GIT_WORKTREE}"
46+
47+
DOCKER_CONTAINER="${GITHUB_REPOSITORY}/${IMAGE_NAME}"
48+
DOCKER_REGISTRY_CONTAINER="${DOCKER_REGISTRY}/${DOCKER_CONTAINER}"
49+
50+
echo "dockerfile=${DOCKERFILE_PATH}" >> $GITHUB_ENV
51+
echo "docker-container=${DOCKER_CONTAINER}" >> $GITHUB_ENV
52+
echo "docker-registry-container=${DOCKER_REGISTRY_CONTAINER}" >> $GITHUB_ENV
53+
54+
# Identify the last git commit that touched the Dockerfiles
55+
# Use this as a hash to identify the resulting docker containers
56+
echo "::: dockerfile path is ${DOCKERFILE_PATH}"
57+
58+
DOCKER_SHA=$(git log -1 --pretty=format:"%h" -- "${DOCKERFILE_PATH}")
59+
echo "docker-sha=${DOCKER_SHA}" >> $GITHUB_ENV
60+
61+
echo "::: docker sha is ${DOCKER_SHA}"
62+
63+
DOCKER_REGISTRY_CONTAINER_SHA="${DOCKER_REGISTRY_CONTAINER}:${DOCKER_SHA}"
64+
65+
echo "docker-registry-container-sha=${DOCKER_REGISTRY_CONTAINER_SHA}" >> $GITHUB_ENV
66+
echo "docker-registry-container-latest=${DOCKER_REGISTRY_CONTAINER}:latest" >> $GITHUB_ENV
67+
68+
echo "::: logging in to ${DOCKER_REGISTRY} as ${GITHUB_ACTOR}"
69+
70+
exists="true"
71+
docker login https://${DOCKER_REGISTRY} -u ${GITHUB_ACTOR} -p ${GITHUB_TOKEN} || exists="false"
72+
73+
echo "::: pulling ${DOCKER_REGISTRY_CONTAINER_SHA}"
74+
75+
if [ "${exists}" != "false" ]; then
76+
docker pull ${DOCKER_REGISTRY_CONTAINER_SHA} || exists="false"
77+
fi
78+
79+
if [ "${exists}" = "true" ]; then
80+
echo "::: docker container exists in registry"
81+
echo "docker-container-exists=true" >> $GITHUB_ENV
82+
else
83+
echo "::: docker container does not exist in registry"
84+
echo "docker-container-exists=false" >> $GITHUB_ENV
85+
fi
86+
shell: bash
87+
env:
88+
GITHUB_TOKEN: ${{ inputs.github_token }}
89+
- name: Create container
90+
run: |
91+
if [ "${{ inputs.base }}" != "" ]; then
92+
BASE_ARG="--build-arg BASE=${{ inputs.base }}"
93+
fi
94+
95+
GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
96+
echo "::: git worktree is ${GIT_WORKTREE}"
97+
cd "${GIT_WORKTREE}"
98+
99+
docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} .
100+
docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }}
101+
shell: bash
102+
working-directory: source/${{ inputs.config-path }}
103+
if: env.docker-container-exists != 'true'
104+
- name: Publish container
105+
run: |
106+
docker push ${{ env.docker-registry-container-sha }}
107+
docker push ${{ env.docker-registry-container-latest }}
108+
shell: bash
109+
if: env.docker-container-exists != 'true' && github.event_name != 'pull_request'

.github/workflows/main.yml

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111

1212
env:
1313
docker-registry: ghcr.io
14-
docker-config-path: source/ci/docker
14+
docker-config-path: ci/docker
1515

1616
permissions:
1717
contents: write
@@ -214,27 +214,15 @@ jobs:
214214
- name: Setup QEMU
215215
run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
216216
if: matrix.platform.container.qemu == true
217-
- name: Download container
218-
run: |
219-
"${{ github.workspace }}/source/ci/getcontainer.sh" "${{ matrix.platform.container.name }}" "${{ matrix.platform.container.dockerfile }}"
220-
env:
221-
DOCKER_REGISTRY: ${{ env.docker-registry }}
222-
GITHUB_TOKEN: ${{ secrets.github_token }}
223-
working-directory: ${{ env.docker-config-path }}
217+
- name: Set up container
218+
uses: ./source/.github/actions/download-or-build-container
219+
with:
220+
registry: ${{ env.docker-registry }}
221+
config-path: ${{ env.docker-config-path }}
222+
container: ${{ matrix.platform.container.name }}
223+
github_token: ${{ secrets.github_token }}
224+
dockerfile: ${{ matrix.platform.container.dockerfile }}
224225
if: matrix.platform.container.name != ''
225-
- name: Create container
226-
run: |
227-
if [ "${{ matrix.container.base }}" != "" ]; then
228-
BASE_ARG="--build-arg BASE=${{ matrix.container.base }}"
229-
fi
230-
docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} .
231-
docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }}
232-
if [ "${{ github.event_name }}" != "pull_request" ]; then
233-
docker push ${{ env.docker-registry-container-sha }}
234-
docker push ${{ env.docker-registry-container-latest }}
235-
fi
236-
working-directory: ${{ env.docker-config-path }}
237-
if: matrix.platform.container.name != '' && env.docker-container-exists != 'true'
238226
- name: Prepare build
239227
run: mkdir build
240228
- name: Build
@@ -287,6 +275,14 @@ jobs:
287275
with:
288276
path: source
289277
fetch-depth: 0
278+
- name: Set up container
279+
uses: ./source/.github/actions/download-or-build-container
280+
with:
281+
registry: ${{ env.docker-registry }}
282+
config-path: ${{ env.docker-config-path }}
283+
container: docurium
284+
github_token: ${{ secrets.github_token }}
285+
dockerfile: ${{ matrix.platform.container.dockerfile }}
290286
- name: Generate documentation
291287
working-directory: source
292288
run: |

.github/workflows/nightly.yml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
env:
1010
docker-registry: ghcr.io
11-
docker-config-path: source/ci/docker
11+
docker-config-path: ci/docker
1212

1313
permissions:
1414
contents: read
@@ -355,22 +355,15 @@ jobs:
355355
- name: Setup QEMU
356356
run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
357357
if: matrix.platform.container.qemu == true
358-
- name: Download container
359-
run: |
360-
"${{ github.workspace }}/source/ci/getcontainer.sh" "${{ matrix.platform.container.name }}" "${{ matrix.platform.container.dockerfile }}"
361-
env:
362-
DOCKER_REGISTRY: ${{ env.docker-registry }}
363-
GITHUB_TOKEN: ${{ secrets.github_token }}
364-
working-directory: ${{ env.docker-config-path }}
358+
- name: Set up container
359+
uses: ./source/.github/actions/download-or-build-container
360+
with:
361+
registry: ${{ env.docker-registry }}
362+
config-path: ${{ env.docker-config-path }}
363+
container: ${{ matrix.platform.container.name }}
364+
github_token: ${{ secrets.github_token }}
365+
dockerfile: ${{ matrix.platform.container.dockerfile }}
365366
if: matrix.platform.container.name != ''
366-
- name: Create container
367-
run: |
368-
if [ "${{ matrix.container.base }}" != "" ]; then
369-
BASE_ARG="--build-arg BASE=${{ matrix.container.base }}"
370-
fi
371-
docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} .
372-
working-directory: ${{ env.docker-config-path }}
373-
if: matrix.platform.container.name != '' && env.docker-container-exists != 'true'
374367
- name: Prepare build
375368
run: mkdir build
376369
- name: Build
@@ -420,13 +413,14 @@ jobs:
420413
with:
421414
path: source
422415
fetch-depth: 0
423-
- name: Download container
424-
run: |
425-
"${{ github.workspace }}/source/ci/getcontainer.sh" xenial
426-
env:
427-
DOCKER_REGISTRY: ${{ env.docker-registry }}
428-
GITHUB_TOKEN: ${{ secrets.github_token }}
429-
working-directory: ${{ env.docker-config-path }}
416+
- name: Set up container
417+
uses: ./source/.github/actions/download-or-build-container
418+
with:
419+
registry: ${{ env.docker-registry }}
420+
config-path: ${{ env.docker-config-path }}
421+
container: xenial
422+
github_token: ${{ secrets.github_token }}
423+
if: matrix.platform.container.name != ''
430424
- name: Run Coverity
431425
run: source/ci/coverity.sh
432426
env:

ci/getcontainer.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0