|
| 1 | +name: Build and push Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '*' |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + runs-on: ubuntu-22.04 |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v3 |
| 20 | + |
| 21 | + - name: Get full image name |
| 22 | + id: base_image_name |
| 23 | + env: |
| 24 | + BASE_IMAGE_NAME: ${{ github.repository }} |
| 25 | + run: | |
| 26 | + if [[ $GITHUB_REF == refs/tags/* ]]; then |
| 27 | + echo "image_name=${REGISTRY}/${BASE_IMAGE_NAME}" >> $GITHUB_OUTPUT |
| 28 | + else |
| 29 | + echo "image_name=${REGISTRY}/${BASE_IMAGE_NAME}-stage" >> $GITHUB_OUTPUT |
| 30 | + fi |
| 31 | + - name: Login to GitHub Container Registry |
| 32 | + uses: docker/login-action@v2 |
| 33 | + with: |
| 34 | + registry: ${{ env.REGISTRY }} |
| 35 | + username: ${{ github.actor }} |
| 36 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + |
| 38 | + - name: Extract metadata (tags, labels) for Docker |
| 39 | + id: meta |
| 40 | + uses: docker/metadata-action@v4 |
| 41 | + with: |
| 42 | + images: ${{ steps.base_image_name.outputs.image_name }} |
| 43 | + tags: | |
| 44 | + type=raw,value=latest,enable={{is_default_branch}} |
| 45 | + type=raw,value={{branch}}-{{sha}}-{{date 'X'}},enable=${{ startsWith(github.ref, 'refs/heads') }} |
| 46 | + type=semver,pattern={{version}},prefix=v |
| 47 | + type=semver,pattern={{major}}.{{minor}},prefix=v |
| 48 | + - name: Build and push image |
| 49 | + uses: docker/build-push-action@v3 |
| 50 | + with: |
| 51 | + context: . |
| 52 | + tags: ${{ steps.meta.outputs.tags }} |
| 53 | + labels: ${{ steps.meta.outputs.labels }} |
| 54 | + push: ${{ startsWith(github.ref, 'refs/tags/v') || endsWith(github.ref, '/stage') }} |
0 commit comments