|
| 1 | +name: Build Container Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + |
| 13 | +env: |
| 14 | + REGISTRY: ghcr.io |
| 15 | + IMAGE_NAME: ${{ github.repository }} |
| 16 | + |
| 17 | +jobs: |
| 18 | + build: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + packages: write |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Install buildah |
| 29 | + run: | |
| 30 | + sudo apt-get update |
| 31 | + sudo apt-get install -y buildah |
| 32 | +
|
| 33 | + - name: Log in to GitHub Container Registry |
| 34 | + if: github.event_name != 'pull_request' |
| 35 | + run: | |
| 36 | + echo "${{ secrets.GITHUB_TOKEN }}" | buildah login -u ${{ github.actor }} --password-stdin ${{ env.REGISTRY }} |
| 37 | +
|
| 38 | + - name: Extract metadata for Docker |
| 39 | + id: meta |
| 40 | + run: | |
| 41 | + # Lowercase the image name |
| 42 | + IMAGE_NAME_LOWER=$(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 43 | + echo "image_name=$IMAGE_NAME_LOWER" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + # Generate tags based on event type |
| 46 | + TAGS="" |
| 47 | + if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 48 | + # Main branch: tag as latest and commit SHA |
| 49 | + TAGS="$IMAGE_NAME_LOWER:latest,$IMAGE_NAME_LOWER:${{ github.sha }}" |
| 50 | + elif [[ "${{ github.ref_type }}" == "tag" ]]; then |
| 51 | + # Tag event: use the tag name and latest |
| 52 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 53 | + TAGS="$IMAGE_NAME_LOWER:$TAG_NAME,$IMAGE_NAME_LOWER:latest" |
| 54 | + elif [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 55 | + # PR: just use PR number for testing |
| 56 | + TAGS="$IMAGE_NAME_LOWER:pr-${{ github.event.pull_request.number }}" |
| 57 | + fi |
| 58 | + echo "tags=$TAGS" >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + - name: Build container image with buildah |
| 61 | + id: build |
| 62 | + run: | |
| 63 | + IFS=',' read -ra TAG_ARRAY <<< "${{ steps.meta.outputs.tags }}" |
| 64 | +
|
| 65 | + # Build the image with the first tag |
| 66 | + buildah bud -f Dockerfile -t "${TAG_ARRAY[0]}" . |
| 67 | +
|
| 68 | + # Tag with additional tags |
| 69 | + for tag in "${TAG_ARRAY[@]:1}"; do |
| 70 | + buildah tag "${TAG_ARRAY[0]}" "$tag" |
| 71 | + done |
| 72 | +
|
| 73 | + echo "Built and tagged image with: ${{ steps.meta.outputs.tags }}" |
| 74 | +
|
| 75 | + - name: Push container image |
| 76 | + if: github.event_name != 'pull_request' |
| 77 | + run: | |
| 78 | + IFS=',' read -ra TAG_ARRAY <<< "${{ steps.meta.outputs.tags }}" |
| 79 | +
|
| 80 | + # Push all tags |
| 81 | + for tag in "${TAG_ARRAY[@]}"; do |
| 82 | + echo "Pushing $tag" |
| 83 | + buildah push "$tag" |
| 84 | + done |
| 85 | +
|
| 86 | + - name: Image digest |
| 87 | + if: github.event_name != 'pull_request' |
| 88 | + run: | |
| 89 | + IFS=',' read -ra TAG_ARRAY <<< "${{ steps.meta.outputs.tags }}" |
| 90 | + buildah inspect "${TAG_ARRAY[0]}" | grep -i digest || true |
0 commit comments