1616
1717env :
1818 # TODO: Change variable to your image's name.
19+ REGISTRY : ghcr.io
1920 IMAGE_NAME : servermanager
2021
2122jobs :
@@ -42,36 +43,43 @@ jobs:
4243 # Ensure test job passes before pushing image.
4344 needs : test
4445
46+ name : Build and publish image
4547 runs-on : ubuntu-latest
46- if : github.event_name == 'push'
47-
48+ permissions :
49+ contents : read
50+ packages : write
4851 steps :
49- - uses : actions/checkout@v2
50-
51- - name : Build image
52- run : docker build . --file Dockerfile --tag $IMAGE_NAME
53-
54- - name : Log into registry
55- run : echo "${{ secrets.CR_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
56-
57- - name : Push image
58- run : |
59- IMAGE_ID=ghcr.io/${{ github.actor }}/$IMAGE_NAME
60-
61- # Change all uppercase to lowercase
62- IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
63-
64- # Strip git ref prefix from version
65- VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
66-
67- # Strip "v" prefix from tag name
68- [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
69-
70- # Use Docker `latest` tag convention
71- [ "$VERSION" == "master" ] && VERSION=latest
72-
73- echo IMAGE_ID=$IMAGE_ID
74- echo VERSION=$VERSION
75-
76- docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
77- docker push $IMAGE_ID:$VERSION
52+ - name : Checkout repository
53+ uses : actions/checkout@v4
54+
55+ - name : Set up Docker Buildx
56+ uses : docker/setup-buildx-action@v3
57+
58+ - name : Extract Docker metadata
59+ id : meta
60+ uses : docker/metadata-action@v5
61+ with :
62+ images : ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
63+ tags : |
64+ type=ref,event=branch
65+ type=ref,event=tag
66+ type=ref,event=pr
67+ type=sha,format=long
68+ type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
69+
70+ - name : Log in to GitHub Container Registry
71+ if : github.event_name == 'push'
72+ uses : docker/login-action@v3
73+ with :
74+ registry : ${{ env.REGISTRY }}
75+ username : ${{ github.actor }}
76+ password : ${{ secrets.GITHUB_TOKEN }}
77+
78+ - name : Build and push
79+ uses : docker/build-push-action@v6
80+ with :
81+ context : .
82+ file : Dockerfile
83+ push : ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/heads/') && github.ref == 'refs/heads/master' }}
84+ tags : ${{ steps.meta.outputs.tags }}
85+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments