|
| 1 | +--- |
| 2 | +name: Build container image |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + schedule: |
| 8 | + - cron: '05 10 * * *' # 10:05am UTC everyday |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + paths-ignore: |
| 13 | + - '**/README.md' |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +env: |
| 17 | + IMAGE_REGISTRY: "ghcr.io/${{ github.repository_owner }}" # do not edit |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.ref || github.run_id }}-${{ inputs.brand_name}}-${{ inputs.stream_name }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + build_push: |
| 25 | + name: Build and push image |
| 26 | + runs-on: ubuntu-26.04 |
| 27 | + |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + packages: write |
| 31 | + id-token: write |
| 32 | + |
| 33 | + steps: |
| 34 | + # These stage versions are pinned by https://github.com/renovatebot/renovate |
| 35 | + - name: Checkout |
| 36 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 |
| 37 | + |
| 38 | + - name: Maximize build space |
| 39 | + uses: ublue-os/remove-unwanted-software@695eb75bc387dbcd9685a8e72d23439d8686cba6 |
| 40 | + |
| 41 | + - name: Install just |
| 42 | + uses: extractions/setup-just@53165ef7e734c5c07cb06b3c8e7b647c5aa16db3 #v4.0.0 |
| 43 | + |
| 44 | + - name: Check Just Syntax |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + just check |
| 48 | +
|
| 49 | + - name: Image Name |
| 50 | + id: image-name |
| 51 | + run: | |
| 52 | + IMAGE_NAME=$(just image_name) |
| 53 | +
|
| 54 | + echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV |
| 55 | +
|
| 56 | + - name: Default Tag |
| 57 | + id: gen-default-tag |
| 58 | + run: | |
| 59 | + DEFAULT_TAG=$(just generate-default-tag) |
| 60 | +
|
| 61 | + echo "DEFAULT_TAG=${DEFAULT_TAG}" >> $GITHUB_ENV |
| 62 | +
|
| 63 | + - name: Prepare environment |
| 64 | + run: | |
| 65 | + # Lowercase the image uri https://github.com/macbre/push-to-ghcr/issues/12 |
| 66 | + echo "IMAGE_REGISTRY=${IMAGE_REGISTRY,,}" >> ${GITHUB_ENV} |
| 67 | + echo "IMAGE_NAME=${IMAGE_NAME,,}" >> ${GITHUB_ENV} |
| 68 | +
|
| 69 | + # This actually builds the container image |
| 70 | + - name: Build Image |
| 71 | + id: build-image |
| 72 | + run: | |
| 73 | + just build \ |
| 74 | + ${IMAGE_NAME} \ |
| 75 | + ${DEFAULT_TAG} |
| 76 | +
|
| 77 | + # This is optional, this provides smaller delta updates |
| 78 | + - name: Rechunk with rpm-ostree |
| 79 | + id: rechunk |
| 80 | + run: | |
| 81 | + just ostree-rechunk \ |
| 82 | + ${IMAGE_NAME} \ |
| 83 | + ${DEFAULT_TAG} |
| 84 | +
|
| 85 | + # If you are feeling adventurous, use the new distro agnostic rechunker |
| 86 | + # https://github.com/coreos/chunkah |
| 87 | + # You can delete the Rechunk with rpm-ostree portion then if you use this |
| 88 | + #- name: Rechunk with Chunkah |
| 89 | + # id: rechunk |
| 90 | + # run: | |
| 91 | + # just rechunk \ |
| 92 | + # ${IMAGE_NAME} \ |
| 93 | + # ${DEFAULT_TAG} |
| 94 | + |
| 95 | + - name: Generate Build Tags |
| 96 | + id: gen-build-tags |
| 97 | + run: | |
| 98 | + alias_tags="$(just generate-build-tags \ |
| 99 | + ${IMAGE_NAME} \ |
| 100 | + ${DEFAULT_TAG})" |
| 101 | +
|
| 102 | + echo "Tags for this Action..." |
| 103 | + echo "${alias_tags}" |
| 104 | + echo "alias_tags=${alias_tags}" >> $GITHUB_OUTPUT |
| 105 | +
|
| 106 | + - name: Tag Image |
| 107 | + id: tag-images |
| 108 | + env: |
| 109 | + ALIAS_TAGS: ${{ steps.gen-build-tags.outputs.alias_tags }} |
| 110 | + run: | |
| 111 | + just tag-images \ |
| 112 | + "${IMAGE_NAME}" \ |
| 113 | + "${DEFAULT_TAG}" \ |
| 114 | + "${ALIAS_TAGS}" |
| 115 | +
|
| 116 | + # These `if` statements are so that pull requests for your custom images do not make it publish any packages under your name without you knowing |
| 117 | + # They also check if the runner is on the default branch so that things like the merge queue (if you enable it), are going to work |
| 118 | + - name: Login to GitHub Container Registry |
| 119 | + uses: docker/login-action@abd2ef45e78c5afb21d64d4ca52ee8550d9572c7 # v4.5.1 |
| 120 | + if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 121 | + with: |
| 122 | + registry: ghcr.io |
| 123 | + username: ${{ github.actor }} |
| 124 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + |
| 126 | + - name: Push To GHCR |
| 127 | + if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 128 | + id: push-image |
| 129 | + env: |
| 130 | + ALIAS_TAGS: ${{ steps.gen-build-tags.outputs.alias_tags }} |
| 131 | + run: | |
| 132 | + set -euox pipefail |
| 133 | + for tag in ${ALIAS_TAGS}; do |
| 134 | + podman push --digestfile=/tmp/digestfile ${IMAGE_NAME}:${tag} ${IMAGE_REGISTRY}/${IMAGE_NAME}:${tag} |
| 135 | + done |
| 136 | +
|
| 137 | + digest=$(< /tmp/digestfile) |
| 138 | +
|
| 139 | + echo "digest=${digest}" >> $GITHUB_OUTPUT |
| 140 | +
|
| 141 | + # This section is optional and only needs to be enabled if you plan on distributing |
| 142 | + # your project for others to consume. You will need to create a public and private key |
| 143 | + # using Cosign and save the private key as a repository secret in Github for this workflow |
| 144 | + # to consume. For more details, review the image signing section of the README. |
| 145 | + - name: Install Cosign |
| 146 | + uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2 |
| 147 | + if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 148 | + with: |
| 149 | + # be careful when upgrading major versions |
| 150 | + cosign-release: 'v3.1.2' |
| 151 | + |
| 152 | + - name: Sign container image |
| 153 | + id: sign-image |
| 154 | + if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 155 | + env: |
| 156 | + COSIGN_EXPERIMENTAL: false |
| 157 | + COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }} |
| 158 | + DIGEST: ${{ steps.push-image.outputs.digest }} |
| 159 | + run: | |
| 160 | + # https://github.com/containers/container-libs/issues/388 |
| 161 | + # https://github.com/coreos/rpm-ostree/issues/5509 |
| 162 | + # use old bundle format, so we can use newer cosign 3.X.X instead of 2.X.X |
| 163 | + cosign sign -y --new-bundle-format=false --use-signing-config=false --key env://COSIGN_PRIVATE_KEY ${IMAGE_REGISTRY}/${IMAGE_NAME}@${DIGEST} |
| 164 | +
|
0 commit comments