|
| 1 | +name: Build container image |
| 2 | +# On pushes to the main branches, build and push the newly created docker |
| 3 | +# images to the GitHub Container Repository where people can pull them from. |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [main, master, release, development] |
| 8 | + tags: [v*.*.*] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + packages: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + name: build and push container image |
| 17 | + runs-on: ubuntu-latest |
| 18 | + env: |
| 19 | + PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
| 20 | + REPO: ${{ github.event.repository.name }} |
| 21 | + USER: ${{ github.actor }} |
| 22 | + TARGET: production |
| 23 | + SERVER: ghcr.io |
| 24 | + steps: |
| 25 | + - name: Make lower case image environment variable |
| 26 | + run: | |
| 27 | + # images need to be lower case |
| 28 | + full="$SERVER/$USER/$REPO" |
| 29 | + echo "IMAGE=${full,,}" >> $GITHUB_ENV |
| 30 | + - name: Checkout repository |
| 31 | + uses: actions/checkout@v6.0.1 |
| 32 | + with: { fetch-depth: 0 } |
| 33 | + - name: Log in to GitHub Container Repository |
| 34 | + run: docker login "$SERVER" --username "$USER" --password "$PASSWORD" |
| 35 | + - name: Get image tags |
| 36 | + id: vars |
| 37 | + run: | |
| 38 | + if [[ "${GITHUB_REF}" == refs/tags/* ]]; then |
| 39 | + FULL_TAG="${GITHUB_REF#refs/tags/}" # Remove refs/tags/ |
| 40 | + echo "full_tag=$FULL_TAG" >> $GITHUB_OUTPUT |
| 41 | + # Remove leading 'v' if present |
| 42 | + VERSION="${FULL_TAG#v}" |
| 43 | + # Remove pre-release or build metadata (anything after '-') |
| 44 | + VERSION="${VERSION%%-*}" |
| 45 | + # Split version like "1.2.3" into "1.2.3", "1.2", "1" |
| 46 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
| 47 | + echo "tags=$FULL_TAG,$MAJOR.$MINOR,$MAJOR" >> $GITHUB_OUTPUT |
| 48 | + else |
| 49 | + echo "full_tag=latest" >> $GITHUB_OUTPUT |
| 50 | + echo "tags=latest" >> $GITHUB_OUTPUT |
| 51 | + fi |
| 52 | + - name: Build docker image |
| 53 | + run: | |
| 54 | + tag="$IMAGE:${{ steps.vars.outputs.full_tag }}" |
| 55 | + docker build --target "$TARGET" --tag "$tag" . |
| 56 | + - name: Basic container runtime test |
| 57 | + run: docker run --rm "$IMAGE:${{ steps.vars.outputs.full_tag }}" -- --help |
| 58 | + - name: Tag docker image with multiple tags |
| 59 | + run: | |
| 60 | + for tag in $(echo "${{ steps.vars.outputs.tags }}" | tr ',' ' '); do |
| 61 | + image="$IMAGE:${{ steps.vars.outputs.full_tag }}" |
| 62 | + alias="$IMAGE:$tag" |
| 63 | + docker tag "$image" "$alias" |
| 64 | + done |
| 65 | + - name: Push created image to registry |
| 66 | + run: | |
| 67 | + for tag in $(echo "${{ steps.vars.outputs.tags }}" | tr ',' ' '); do |
| 68 | + docker push "$IMAGE:$tag" |
| 69 | + done |
0 commit comments