|
| 1 | +# Copyright (C) 2021 Intel Corporation |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions |
| 5 | + |
| 6 | +# This workflow combines a pull-request flow that only builds the container, |
| 7 | +# and a push flow that builds and pushes the container to the package registry. |
| 8 | +# |
| 9 | +# The push flow is triggered when the user pushes a commit to their personal, |
| 10 | +# forked repository; in this case the container image is uploaded to their |
| 11 | +# personal registry. The push flow is also triggered when a pull-request is |
| 12 | +# merged into the main branch; in this case the container image is uploaded |
| 13 | +# to the registry belonging to the runtime repository. |
| 14 | +# |
| 15 | +# This workflow design allows users to iterate and test container |
| 16 | +# changes in their forked repository, while ensuring that only merged |
| 17 | +# pull requests overwrite production images used by build workflows. |
| 18 | + |
| 19 | +name: build container images |
| 20 | + |
| 21 | +# https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/ |
| 22 | +permissions: |
| 23 | + # Grant read permissions to repository in case it is not a forked public |
| 24 | + # repository, but a private repository that was created manually. |
| 25 | + contents: read |
| 26 | + |
| 27 | + # If trigger-specific permissions were supported, write permissions to the |
| 28 | + # container registry would only be needed for push events. |
| 29 | + packages: write |
| 30 | + |
| 31 | +on: |
| 32 | + push: |
| 33 | + branches: |
| 34 | + - main |
| 35 | + paths: |
| 36 | + - '.github/workflows/klocwork-container.yml' |
| 37 | + - 'container/ubuntu-20.04-klocwork-build-tools/**' |
| 38 | + - 'container/ubuntu-20.04-klocwork-desktop-tools/**' |
| 39 | + |
| 40 | + pull_request: |
| 41 | + paths: |
| 42 | + - '.github/workflows/klocwork-container.yml' |
| 43 | + - 'container/ubuntu-20.04-klocwork-build-tools/**' |
| 44 | + - 'container/ubuntu-20.04-klocwork-desktop-tools/**' |
| 45 | + |
| 46 | + workflow_dispatch: |
| 47 | + |
| 48 | +jobs: |
| 49 | + build: |
| 50 | + runs-on: |
| 51 | + - self-hosted |
| 52 | + - linux |
| 53 | + - x64 |
| 54 | + - container |
| 55 | + |
| 56 | + # https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment |
| 57 | + environment: klocwork-container |
| 58 | + |
| 59 | + # This rebuilds all container images whenever any single Dockerfile is |
| 60 | + # changed. Therefore, when iterating on a container change, consider |
| 61 | + # temporarily commenting all but the container of interest below. |
| 62 | + strategy: |
| 63 | + matrix: |
| 64 | + container: |
| 65 | + - ubuntu-20.04-klocwork-build-tools |
| 66 | + - ubuntu-20.04-klocwork-desktop-tools |
| 67 | + |
| 68 | + env: |
| 69 | + image: ghcr.io/${{ github.repository }}/${{ matrix.container }} |
| 70 | + |
| 71 | + KLOCWORK_BUILD_TOOLS_URL: ${{ secrets.KLOCWORK_BUILD_TOOLS_URL }} |
| 72 | + KLOCWORK_DESKTOP_TOOLS_URL: ${{ secrets.KLOCWORK_DESKTOP_TOOLS_URL }} |
| 73 | + KLOCWORK_LICENSE_SERVER: ${{ secrets.KLOCWORK_LICENSE_SERVER }} |
| 74 | + |
| 75 | + continue-on-error: true |
| 76 | + |
| 77 | + steps: |
| 78 | + - name: checkout code |
| 79 | + uses: actions/checkout@v3 |
| 80 | + |
| 81 | + # https://docs.docker.com/engine/reference/commandline/tag/#extended-description |
| 82 | + # > A tag name must be valid ASCII and may contain lowercase and |
| 83 | + # > uppercase letters, digits, underscores, periods and dashes. |
| 84 | + # > A tag name may not start with a period or a dash and may |
| 85 | + # > contain a maximum of 128 characters. |
| 86 | + - name: set container tag from git reference |
| 87 | + id: tag |
| 88 | + run: | |
| 89 | + ref=$(echo "$ref" | sed 's#^refs/\(\|heads/\|tags/\)##') |
| 90 | + echo "tag=${ref//[^a-zA-Z0-9_.]/-}" >> "$GITHUB_OUTPUT" |
| 91 | + env: |
| 92 | + ref: ${{ github.ref }} |
| 93 | + |
| 94 | + # Override uid of default user in container to align with GitHub-hosted runners |
| 95 | + # https://github.com/actions/checkout/issues/47 |
| 96 | + - name: build image |
| 97 | + run: | |
| 98 | + docker build \ |
| 99 | + --build-arg uid=1001 \ |
| 100 | + --build-arg klocwork_build_tools_url="$KLOCWORK_BUILD_TOOLS_URL" \ |
| 101 | + --build-arg klocwork_desktop_tools_url="$KLOCWORK_DESKTOP_TOOLS_URL" \ |
| 102 | + --build-arg klocwork_license_server="$KLOCWORK_LICENSE_SERVER" \ |
| 103 | + -t "$image:$tag" -f container/${{ matrix.container }}/Dockerfile . |
| 104 | + env: |
| 105 | + tag: ${{ steps.tag.outputs.tag }} |
| 106 | + |
| 107 | + - name: login to registry |
| 108 | + if: github.event_name == 'push' |
| 109 | + run: echo "$token" | docker login ghcr.io -u "$user" --password-stdin |
| 110 | + env: |
| 111 | + user: ${{ github.repository_owner }} |
| 112 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + |
| 114 | + - name: push image to registry |
| 115 | + if: github.event_name == 'push' |
| 116 | + run: docker image push "$image:$tag" |
| 117 | + env: |
| 118 | + tag: ${{ steps.tag.outputs.tag }} |
| 119 | + |
| 120 | + - name: logout from registry |
| 121 | + if: always() && github.event_name == 'push' |
| 122 | + run: docker logout ghcr.io |
0 commit comments