|
| 1 | +name: Vib Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + tags: |
| 7 | + - '*' |
| 8 | + schedule: |
| 9 | + - cron: '21 */6 * * *' |
| 10 | + pull_request: |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +env: |
| 14 | + CUSTOM_IMAGE_NAME: custom |
| 15 | + BUILDX_NO_DEFAULT_ATTESTATIONS: 1 |
| 16 | + |
| 17 | +jobs: |
| 18 | + check_update: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + outputs: |
| 22 | + has_updates: ${{ steps.set_output.outputs.has_updates }} |
| 23 | + base_image: ${{ steps.read_base_recipe.outputs.base_image }} |
| 24 | + |
| 25 | + permissions: |
| 26 | + contents: write # Allow actions to create a digest |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: sudo apt-get install -y jq skopeo libfyaml-utils |
| 34 | + |
| 35 | + - name: Read base image name from recipe |
| 36 | + id: read_base_recipe |
| 37 | + run: | |
| 38 | + BASE_IMAGE="$(fy-filter -f recipe.yml /stages/-1/base)" |
| 39 | + echo The base image is $BASE_IMAGE |
| 40 | + if [ -z $BASE_IMAGE ]; then exit 1; fi |
| 41 | + echo "base_image=$BASE_IMAGE" >> "$GITHUB_OUTPUT" |
| 42 | + echo "BASE_IMAGE=$BASE_IMAGE" >> "$GITHUB_ENV" |
| 43 | +
|
| 44 | + - name: Get last successful run |
| 45 | + if: ${{ github.ref_type == 'branch' }} |
| 46 | + env: |
| 47 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + continue-on-error: true |
| 49 | + run: | |
| 50 | + gh run list -b "${{ github.ref_name }}" -w "${{ github.workflow }}" -s "success" -L 1 --json databaseId > last_run.json |
| 51 | + echo "LAST_RUN_ID=$(jq -r '.[0].databaseId' last_run.json)" >> "$GITHUB_ENV" |
| 52 | +
|
| 53 | + - name: Download the previous digest |
| 54 | + uses: actions/download-artifact@v4 |
| 55 | + continue-on-error: true |
| 56 | + with: |
| 57 | + name: digest |
| 58 | + github-token: ${{ github.token }} |
| 59 | + run-id: ${{ env.LAST_RUN_ID }} |
| 60 | + |
| 61 | + - name: Check if there was an update to the base image |
| 62 | + run: | |
| 63 | + touch digest.txt |
| 64 | + mv digest.txt last_digest.txt |
| 65 | + skopeo inspect --raw docker://${{ env.BASE_IMAGE }} | sha256sum > digest.txt |
| 66 | + echo Old digest is: $(cat last_digest.txt) |
| 67 | + echo New digest is: $(cat digest.txt) |
| 68 | + echo "HAS_UPDATES=$(cmp -s digest.txt last_digest.txt; echo $?)" >> "$GITHUB_ENV" |
| 69 | +
|
| 70 | + - name: Upload current digest |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: digest |
| 74 | + path: digest.txt |
| 75 | + |
| 76 | + - name: Set output |
| 77 | + id: set_output |
| 78 | + run: | |
| 79 | + if [ ${{ github.event_name == 'schedule'}} == false ] |
| 80 | + then |
| 81 | + echo action was manually run, updating either way |
| 82 | + echo "has_updates=true" >> "$GITHUB_OUTPUT" |
| 83 | + elif [ ${{ env.HAS_UPDATES }} == 1 ] |
| 84 | + then |
| 85 | + echo base image was updated since the last build |
| 86 | + echo "has_updates=true" >> "$GITHUB_OUTPUT" |
| 87 | + else |
| 88 | + echo no updates to the base image since the last build |
| 89 | + echo "has_updates=false" >> "$GITHUB_OUTPUT" |
| 90 | + fi |
| 91 | +
|
| 92 | + build: |
| 93 | + runs-on: ubuntu-latest |
| 94 | + needs: check_update |
| 95 | + if: ${{ needs.check_update.outputs.has_updates == 'true' }} |
| 96 | + |
| 97 | + permissions: |
| 98 | + packages: write # Allow pushing images to GHCR |
| 99 | + attestations: write # To create and write attestations |
| 100 | + id-token: write # Additional permissions for the persistence of the attestations |
| 101 | + |
| 102 | + steps: |
| 103 | + - uses: actions/checkout@v4 |
| 104 | + |
| 105 | + - uses: vanilla-os/vib-gh-action@v0.8.1 |
| 106 | + with: |
| 107 | + recipe: 'recipe.yml' |
| 108 | + plugins: 'Vanilla-OS/vib-fsguard:v1.5.3' |
| 109 | + |
| 110 | + - uses: actions/upload-artifact@v4 |
| 111 | + with: |
| 112 | + name: Containerfile |
| 113 | + path: Containerfile |
| 114 | + |
| 115 | + - name: Generate image name |
| 116 | + run: | |
| 117 | + REPO_OWNER_LOWERCASE="$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" |
| 118 | + echo "REPO_OWNER_LOWERCASE=$REPO_OWNER_LOWERCASE">> "$GITHUB_ENV" |
| 119 | + echo "IMAGE_URL=ghcr.io/$REPO_OWNER_LOWERCASE/${{ env.CUSTOM_IMAGE_NAME }}">> "$GITHUB_ENV" |
| 120 | +
|
| 121 | + - name: Set image info |
| 122 | + run: | |
| 123 | + echo -n "${{ needs.check_update.outputs.base_image }}" > ./includes.container/image-info/base-image-name |
| 124 | + echo -n "${{ env.REPO_OWNER_LOWERCASE }}/${{ env.CUSTOM_IMAGE_NAME }}" > ./includes.container/image-info/image-name |
| 125 | +
|
| 126 | + - name: Docker meta |
| 127 | + id: docker_meta |
| 128 | + uses: docker/metadata-action@v5 |
| 129 | + with: |
| 130 | + images: | |
| 131 | + ${{ env. IMAGE_URL }} |
| 132 | + tags: | |
| 133 | + type=semver,pattern={{version}} |
| 134 | + type=semver,pattern={{major}}.{{minor}} |
| 135 | + type=semver,pattern={{raw}} |
| 136 | + type=semver,pattern=v{{major}} |
| 137 | + type=ref,event=branch |
| 138 | +
|
| 139 | + - name: Set up Docker Buildx |
| 140 | + uses: docker/setup-buildx-action@v3 |
| 141 | + |
| 142 | + - name: Login to GitHub Package Registry |
| 143 | + uses: docker/login-action@v3 |
| 144 | + if: ${{ github.event_name != 'pull_request' }} |
| 145 | + with: |
| 146 | + registry: ghcr.io |
| 147 | + username: ${{ github.repository_owner }} |
| 148 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 149 | + |
| 150 | + - name: Build and Push the Docker image |
| 151 | + id: push |
| 152 | + uses: docker/build-push-action@v6 |
| 153 | + with: |
| 154 | + context: . |
| 155 | + file: Containerfile |
| 156 | + push: ${{ github.event_name != 'pull_request' }} |
| 157 | + tags: ${{ steps.docker_meta.outputs.tags }} |
| 158 | + labels: ${{ steps.docker_meta.outputs.labels }} |
| 159 | + cache-from: type=gha |
| 160 | + cache-to: type=gha,mode=max |
| 161 | + platforms: linux/amd64 |
| 162 | + provenance: false |
| 163 | + |
| 164 | + - name: Attest pushed image |
| 165 | + uses: actions/attest-build-provenance@v1 |
| 166 | + id: attest |
| 167 | + if: ${{ github.event_name != 'pull_request' }} |
| 168 | + with: |
| 169 | + subject-name: ${{ env.IMAGE_URL }} |
| 170 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 171 | + push-to-registry: false |
0 commit comments