|
| 1 | +name: container-images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + tags: [ "v*" ] |
| 7 | + |
| 8 | +jobs: |
| 9 | + build-container-image: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: read |
| 13 | + packages: write |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + - uses: docker/setup-qemu-action@v3 |
| 19 | + - uses: docker/setup-buildx-action@v3 |
| 20 | + |
| 21 | + - name: Set ALL_TAGS |
| 22 | + env: |
| 23 | + REPOSITORY: '${{ github.repository }}' |
| 24 | + run: | |
| 25 | + # tag main if main branch |
| 26 | + if [[ "${{ github.ref_name }}" == "main" ]]; then |
| 27 | + image_tags=("main") |
| 28 | + # tag with tag name if tag |
| 29 | + elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then |
| 30 | + image_tags=("${{ github.ref_name }}") |
| 31 | + # tag with latest if tag is a new major, minor or patch version |
| 32 | + if [[ "${{ github.ref_name}}" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then |
| 33 | + image_tags+=("latest") |
| 34 | + fi |
| 35 | + fi |
| 36 | +
|
| 37 | + lc_repo=${REPOSITORY,,} |
| 38 | +
|
| 39 | + image_paths=() |
| 40 | + for tag in ${image_tags[@]}; do |
| 41 | + image_paths+=("ghcr.io/$lc_repo:$tag") |
| 42 | + done |
| 43 | +
|
| 44 | + # join with ',' and then skip first character |
| 45 | + ALL_TAGS=$(printf ',%s' "${image_paths[@]}") |
| 46 | + echo "ALL_TAGS=${ALL_TAGS:1}" >>$GITHUB_ENV |
| 47 | +
|
| 48 | + - name: Login to ghcr.io |
| 49 | + uses: docker/login-action@v3 |
| 50 | + with: |
| 51 | + registry: ghcr.io |
| 52 | + username: ${{ github.actor }} |
| 53 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + |
| 55 | + - name: Build and push default image |
| 56 | + uses: docker/build-push-action@v5 |
| 57 | + with: |
| 58 | + context: . # Because GH actions are for kids and put protection on everything; https://stackoverflow.com/a/71159809/11276254 |
| 59 | + platforms: linux/amd64,linux/arm64 |
| 60 | + push: true |
| 61 | + tags: ${{ env.ALL_TAGS }} |
0 commit comments