Debian Codegraph @

.github/actions/download-or-build-container/action.yml

raw
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'