|
| 1 | +name: Build CI Container |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: read |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + paths: |
| 11 | + - .github/workflows/build-ci-container-code-format.yml |
| 12 | + - '.github/workflows/containers/github-action-ci-code-format/**' |
| 13 | + - llvm/utils/git/code-format-helper.py |
| 14 | + - llvm/utils/git/requirements_formatting.txt |
| 15 | + - llvm/utils/git/requirements_formatting.txt.in |
| 16 | + pull_request: |
| 17 | + paths: |
| 18 | + - .github/workflows/build-ci-container-code-format.yml |
| 19 | + - '.github/workflows/containers/github-action-ci-code-format/**' |
| 20 | + - llvm/utils/git/code-format-helper.py |
| 21 | + - llvm/utils/git/requirements_formatting.txt |
| 22 | + - llvm/utils/git/requirements_formatting.txt.in |
| 23 | + |
| 24 | +jobs: |
| 25 | + build-ci-container-code-format: |
| 26 | + if: github.repository_owner == 'llvm' |
| 27 | + runs-on: depot-ubuntu-24.04-16 |
| 28 | + steps: |
| 29 | + - name: Checkout LLVM |
| 30 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 31 | + with: |
| 32 | + sparse-checkout: .github/workflows/containers/github-action-ci-code-format/ |
| 33 | + - name: Write Variables |
| 34 | + id: vars |
| 35 | + run: | |
| 36 | + tag=$(git rev-parse --short=12 HEAD) |
| 37 | + container_name="ghcr.io/$GITHUB_REPOSITORY_OWNER/amd64/ci-ubuntu-24.04-code-format" |
| 38 | + echo "container-name=$container_name" >> $GITHUB_OUTPUT |
| 39 | + echo "container-name-tag=$container_name:$tag" >> $GITHUB_OUTPUT |
| 40 | + echo "container-filename=$(echo $container_name:$tag | sed -e 's/\//-/g' -e 's/:/-/g').tar" >> $GITHUB_OUTPUT |
| 41 | + - name: Build container |
| 42 | + run: | |
| 43 | + podman build --target ci-container-code-format \ |
| 44 | + -f .github/workflows/containers/github-action-ci-code-format/Dockerfile \ |
| 45 | + -t ${{ steps.vars.outputs.container-name-tag }} . |
| 46 | +
|
| 47 | + # Save the container so we have it in case the push fails. This also |
| 48 | + # allows us to separate the push step into a different job so we can |
| 49 | + # maintain minimal permissions while building the container. |
| 50 | + - name: Save container image |
| 51 | + run: | |
| 52 | + podman save ${{ steps.vars.outputs.container-name-tag }} > ${{ steps.vars.outputs.container-filename }} |
| 53 | +
|
| 54 | + - name: Upload container image |
| 55 | + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 |
| 56 | + with: |
| 57 | + name: container-amd64 |
| 58 | + path: "*.tar" |
| 59 | + retention-days: 14 |
| 60 | + |
| 61 | + - name: Test Container |
| 62 | + run: | |
| 63 | + for image in ${{ steps.vars.outputs.container-name-tag }}; do |
| 64 | + # Use --pull=never to ensure we are testing the just built image. |
| 65 | + podman run --pull=never --rm -it $image /usr/bin/bash -x -c 'cd $HOME && clang-format --version | grep version' |
| 66 | + done |
| 67 | +
|
| 68 | + push-ci-container: |
| 69 | + if: github.event_name == 'push' |
| 70 | + needs: |
| 71 | + - build-ci-container-code-format |
| 72 | + permissions: |
| 73 | + packages: write |
| 74 | + runs-on: ubuntu-24.04 |
| 75 | + env: |
| 76 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + steps: |
| 78 | + - name: Download container |
| 79 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0 |
| 80 | + |
| 81 | + - name: Push Container |
| 82 | + run: | |
| 83 | + function push_container { |
| 84 | + image_name=$1 |
| 85 | + latest_name=$(echo $image_name | sed 's/:[a-f0-9]\+$/:latest/g') |
| 86 | + podman tag $image_name $latest_name |
| 87 | + echo "Pushing $image_name ..." |
| 88 | + podman push $image_name |
| 89 | + echo "Pushing $latest_name ..." |
| 90 | + podman push $latest_name |
| 91 | + } |
| 92 | +
|
| 93 | + podman login -u ${{ github.actor }} -p $GITHUB_TOKEN ghcr.io |
| 94 | + for f in $(find . -iname *.tar); do |
| 95 | + image_name=$(podman load -q -i $f | sed 's/Loaded image: //g') |
| 96 | + push_container $image_name |
| 97 | +
|
| 98 | + if echo $image_name | grep '/amd64/'; then |
| 99 | + # For amd64, create an alias with the arch component removed. |
| 100 | + # This matches the convention used on dockerhub. |
| 101 | + default_image_name=$(echo $(dirname $(dirname $image_name))/$(basename $image_name)) |
| 102 | + podman tag $image_name $default_image_name |
| 103 | + push_container $default_image_name |
| 104 | + fi |
| 105 | + done |
0 commit comments