summaryrefslogtreecommitdiffstats
path: root/.github/actions/download-or-build-container/action.yml
blob: 9c83a9836c3b911dc886d022226255b221e682ff (plain)
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
# Run a build step in a container or directly on the Actions runner
name: Download or Build Container
description: Download a container from the package registry, or build it if it's not found

inputs:
  container:
    description: Container name
    type: string
    required: true
  dockerfile:
    description: Dockerfile
    type: string
  base:
    description: Container base
    type: string
  registry:
    description: Docker registry to read and publish to
    type: string
    default: ghcr.io
  config-path:
    description: Path to Dockerfiles
    type: string
  github_token:
    description: GitHub Token
    type: string

runs:
  using: 'composite'
  steps:
  - name: Download container
    run: |
      IMAGE_NAME="${{ inputs.container }}"
      DOCKERFILE_PATH="${{ inputs.dockerfile }}"
      DOCKER_REGISTRY="${{ inputs.registry }}"
      DOCKERFILE_ROOT="${{ inputs.config-path }}"

      if [ "${DOCKERFILE_PATH}" = "" ]; then
        DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${IMAGE_NAME}"
      else
        DOCKERFILE_PATH="${DOCKERFILE_ROOT}/${DOCKERFILE_PATH}"
      fi

      GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
      echo "::: git worktree is ${GIT_WORKTREE}"
      cd "${GIT_WORKTREE}"

      DOCKER_CONTAINER="${GITHUB_REPOSITORY}/${IMAGE_NAME}"
      DOCKER_REGISTRY_CONTAINER="${DOCKER_REGISTRY}/${DOCKER_CONTAINER}"

      echo "dockerfile=${DOCKERFILE_PATH}" >> $GITHUB_ENV
      echo "docker-container=${DOCKER_CONTAINER}" >> $GITHUB_ENV
      echo "docker-registry-container=${DOCKER_REGISTRY_CONTAINER}" >> $GITHUB_ENV

      # Identify the last git commit that touched the Dockerfiles
      # Use this as a hash to identify the resulting docker containers
      echo "::: dockerfile path is ${DOCKERFILE_PATH}"

      DOCKER_SHA=$(git log -1 --pretty=format:"%h" -- "${DOCKERFILE_PATH}")
      echo "docker-sha=${DOCKER_SHA}" >> $GITHUB_ENV

      echo "::: docker sha is ${DOCKER_SHA}"

      DOCKER_REGISTRY_CONTAINER_SHA="${DOCKER_REGISTRY_CONTAINER}:${DOCKER_SHA}"

      echo "docker-registry-container-sha=${DOCKER_REGISTRY_CONTAINER_SHA}" >> $GITHUB_ENV
      echo "docker-registry-container-latest=${DOCKER_REGISTRY_CONTAINER}:latest" >> $GITHUB_ENV

      echo "::: logging in to ${DOCKER_REGISTRY} as ${GITHUB_ACTOR}"

      exists="true"
      docker login https://${DOCKER_REGISTRY} -u ${GITHUB_ACTOR} -p ${GITHUB_TOKEN} || exists="false"

      echo "::: pulling ${DOCKER_REGISTRY_CONTAINER_SHA}"

      if [ "${exists}" != "false" ]; then
        docker pull ${DOCKER_REGISTRY_CONTAINER_SHA} || exists="false"
      fi

      if [ "${exists}" = "true" ]; then
        echo "::: docker container exists in registry"
        echo "docker-container-exists=true" >> $GITHUB_ENV
      else
        echo "::: docker container does not exist in registry"
        echo "docker-container-exists=false" >> $GITHUB_ENV
      fi
    shell: bash
    env:
      GITHUB_TOKEN: ${{ inputs.github_token }}
  - name: Create container
    run: |
      if [ "${{ inputs.base }}" != "" ]; then
        BASE_ARG="--build-arg BASE=${{ inputs.base }}"
      fi

      GIT_WORKTREE=$(cd "${GITHUB_ACTION_PATH}" && git rev-parse --show-toplevel)
      echo "::: git worktree is ${GIT_WORKTREE}"
      cd "${GIT_WORKTREE}"

      docker build -t ${{ env.docker-registry-container-sha }} --build-arg UID=$(id -u) --build-arg GID=$(id -g) ${BASE_ARG} -f ${{ env.dockerfile }} .
      docker tag ${{ env.docker-registry-container-sha }} ${{ env.docker-registry-container-latest }}
    shell: bash
    working-directory: source/${{ inputs.config-path }}
    if: env.docker-container-exists != 'true'
  - name: Publish container
    run: |
      docker push ${{ env.docker-registry-container-sha }}
      docker push ${{ env.docker-registry-container-latest }}
    shell: bash
    if: env.docker-container-exists != 'true' && github.event_name != 'pull_request'