|
| 1 | +name: Build and push docker image |
| 2 | +'on': |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + tag_latest: |
| 6 | + required: true |
| 7 | + type: boolean |
| 8 | + docker_tag: |
| 9 | + required: true |
| 10 | + type: string |
| 11 | + container_name: |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + base_folder: |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + |
| 18 | +jobs: |
| 19 | + build_and_push_image: |
| 20 | + name: Build image for ${{ inputs.container_name }} on ${{ matrix.arch }} |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + packages: write |
| 24 | + attestations: write |
| 25 | + id-token: write |
| 26 | + runs-on: '${{ matrix.runner }}' |
| 27 | + strategy: |
| 28 | + fail-fast: false |
| 29 | + matrix: |
| 30 | + include: |
| 31 | + - arch: amd64 |
| 32 | + runner: ubuntu-22.04 |
| 33 | + - arch: arm64 |
| 34 | + runner: ubuntu-22.04-arm |
| 35 | + steps: |
| 36 | + - name: Free Disk Space for Docker |
| 37 | + uses: endersonmenezes/free-disk-space@e6ed9b02e683a3b55ed0252f1ee469ce3b39a885 |
| 38 | + with: |
| 39 | + remove_android: true |
| 40 | + remove_dotnet: true |
| 41 | + remove_haskell: true |
| 42 | + remove_tool_cache: true |
| 43 | + rm_cmd: rm |
| 44 | + remove_packages: >- |
| 45 | + azure-cli google-cloud-cli microsoft-edge-stable |
| 46 | + google-chrome-stable firefox postgresql* temurin-* *llvm* mysql* |
| 47 | + dotnet-sdk-* |
| 48 | + remove_packages_one_command: true |
| 49 | + - name: Login to github container registry |
| 50 | + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 |
| 51 | + with: |
| 52 | + registry: ghcr.io |
| 53 | + username: ${{github.actor}} |
| 54 | + password: ${{secrets.GITHUB_TOKEN}} |
| 55 | + - name: Checkout code |
| 56 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 |
| 57 | + with: |
| 58 | + fetch-depth: 0 |
| 59 | + - name: setup node |
| 60 | + uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f |
| 61 | + with: |
| 62 | + node-version-file: .tool-versions |
| 63 | + |
| 64 | + - name: make install |
| 65 | + run: | |
| 66 | + make install-node |
| 67 | +
|
| 68 | + - name: Build container |
| 69 | + run: | |
| 70 | + echo "Building image..." |
| 71 | + make build-image |
| 72 | +
|
| 73 | + echo "Creating combined trivy ignore file" |
| 74 | + # create combined trivy ignore file for use in trivy scan, combining common and specific ignore files if they exist |
| 75 | + combined="src/${BASE_FOLDER}/${CONTAINER_NAME}/.trivyignore_combined.yaml" |
| 76 | + common="src/common/.trivyignore.yaml" |
| 77 | + specific="src/${BASE_FOLDER}/${CONTAINER_NAME}/.trivyignore.yaml" |
| 78 | + echo "vulnerabilities:" > "$combined" |
| 79 | + if [ -f "$common" ]; then sed -n '2,$p' "$common" >> "$combined"; fi |
| 80 | + if [ -f "$specific" ]; then sed -n '2,$p' "$specific" >> "$combined"; fi |
| 81 | + echo "Combined trivy ignore file created at $combined" |
| 82 | +
|
| 83 | + env: |
| 84 | + ARCHITECTURE: '${{ matrix.arch }}' |
| 85 | + DOCKER_TAG: '${{ inputs.docker_tag }}' |
| 86 | + CONTAINER_NAME: '${{ inputs.container_name }}' |
| 87 | + BASE_VERSION: ${{ inputs.docker_tag}} |
| 88 | + IMAGE_TAG: ":${{ inputs.docker_tag }}-${{ matrix.arch }}" |
| 89 | + BASE_FOLDER: "${{ inputs.base_folder }}" |
| 90 | + - name: Check docker vulnerabilities - json output |
| 91 | + uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 |
| 92 | + with: |
| 93 | + scan-type: "image" |
| 94 | + image-ref: "ghcr.io/nhsdigital/eps-devcontainers/${{ inputs.container_name }}:${{ inputs.docker_tag }}-${{ matrix.arch }}" |
| 95 | + severity: "CRITICAL,HIGH" |
| 96 | + scanners: "vuln" |
| 97 | + vuln-type: "os,library" |
| 98 | + format: "json" |
| 99 | + output: "scan_results_docker.json" |
| 100 | + exit-code: "0" |
| 101 | + trivy-config: src/${{ inputs.base_folder }}/${{ inputs.container_name }}/trivy.yaml |
| 102 | + - uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f |
| 103 | + name: Upload scan results |
| 104 | + with: |
| 105 | + name: "scan_results_docker_${{ inputs.container_name }}_${{ matrix.arch }}.json" |
| 106 | + path: scan_results_docker.json |
| 107 | + - name: Check docker vulnerabilities - table output |
| 108 | + uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 |
| 109 | + with: |
| 110 | + scan-type: "image" |
| 111 | + image-ref: "ghcr.io/nhsdigital/eps-devcontainers/${{ inputs.container_name }}:${{ inputs.docker_tag }}-${{ matrix.arch }}" |
| 112 | + severity: "CRITICAL,HIGH" |
| 113 | + scanners: "vuln" |
| 114 | + vuln-type: "os,library" |
| 115 | + format: "table" |
| 116 | + output: "scan_results_docker.txt" |
| 117 | + exit-code: "1" |
| 118 | + trivy-config: src/${{ inputs.base_folder }}/${{ inputs.container_name }}/trivy.yaml |
| 119 | + |
| 120 | + - name: Show docker vulnerability output |
| 121 | + if: always() |
| 122 | + run: | |
| 123 | + echo "Scan output for ghcr.io/nhsdigital/eps-devcontainers/base:${DOCKER_TAG}-${ARCHITECTURE}" |
| 124 | + if [ -f scan_results_docker.txt ]; then |
| 125 | + cat scan_results_docker.txt |
| 126 | + fi |
| 127 | + env: |
| 128 | + ARCHITECTURE: '${{ matrix.arch }}' |
| 129 | + DOCKER_TAG: '${{ inputs.docker_tag }}' |
| 130 | + - name: Push tagged image |
| 131 | + run: | |
| 132 | + echo "Pushing image..." |
| 133 | + docker push "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}-${ARCHITECTURE}" |
| 134 | + env: |
| 135 | + DOCKER_TAG: ${{ inputs.docker_tag }} |
| 136 | + CONTAINER_NAME: '${{ inputs.container_name }}' |
| 137 | + ARCHITECTURE: '${{ matrix.arch }}' |
| 138 | + - name: Push latest image |
| 139 | + if: ${{ inputs.tag_latest }} |
| 140 | + run: | |
| 141 | + docker tag "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}-${ARCHITECTURE}" "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest-${ARCHITECTURE}" |
| 142 | + echo "Pushing image..." |
| 143 | + docker push "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest-${ARCHITECTURE}" |
| 144 | + env: |
| 145 | + DOCKER_TAG: ${{ inputs.docker_tag }} |
| 146 | + CONTAINER_NAME: '${{ inputs.container_name }}' |
| 147 | + ARCHITECTURE: '${{ matrix.arch }}' |
| 148 | + publish_combined_image: |
| 149 | + name: Publish combined image for ${{ inputs.container_name }} |
| 150 | + runs-on: ubuntu-22.04 |
| 151 | + needs: build_and_push_image |
| 152 | + permissions: |
| 153 | + contents: read |
| 154 | + packages: write |
| 155 | + attestations: write |
| 156 | + id-token: write |
| 157 | + steps: |
| 158 | + - name: Login to github container registry |
| 159 | + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 |
| 160 | + with: |
| 161 | + registry: ghcr.io |
| 162 | + username: ${{github.actor}} |
| 163 | + password: ${{secrets.GITHUB_TOKEN}} |
| 164 | + |
| 165 | + - name: Push multi-arch tagged image |
| 166 | + run: | |
| 167 | + docker buildx imagetools create -t "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}" \ |
| 168 | + "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}-amd64" \ |
| 169 | + "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}-arm64" |
| 170 | + echo "## PUSHED IMAGE : ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:${DOCKER_TAG}" >> "$GITHUB_STEP_SUMMARY" |
| 171 | + env: |
| 172 | + DOCKER_TAG: ${{ inputs.docker_tag }} |
| 173 | + CONTAINER_NAME: '${{ inputs.container_name }}' |
| 174 | + |
| 175 | + - name: Push multi-arch latest image |
| 176 | + if: ${{ inputs.tag_latest }} |
| 177 | + run: | |
| 178 | + docker buildx imagetools create -t "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest" \ |
| 179 | + "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest-amd64" \ |
| 180 | + "ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest-arm64" |
| 181 | + echo "## PUSHED COMBINED IMAGE : ghcr.io/nhsdigital/eps-devcontainers/${CONTAINER_NAME}:latest" >> "$GITHUB_STEP_SUMMARY" |
| 182 | + env: |
| 183 | + DOCKER_TAG: ${{ inputs.docker_tag }} |
| 184 | + CONTAINER_NAME: '${{ inputs.container_name }}' |
0 commit comments