|
1 | | -name: Build release |
| 1 | +name: Create Release |
2 | 2 |
|
3 | | -on: |
| 3 | +on: |
4 | 4 | push: |
5 | | - branches: [ main ] |
6 | | - # Publish semver tags as releases. |
7 | | - tags: [ 'v*.*.*' ] |
8 | | - pull_request: |
9 | | - branches: [ main ] |
10 | 5 |
|
| 6 | + # Publish `master` as Docker `latest` image. |
| 7 | + branches: |
| 8 | + - main |
| 9 | + |
| 10 | + # Publish `v1.2.3` tags as releases. |
| 11 | + tags: |
| 12 | + - v* |
| 13 | + |
| 14 | + # Create a button to trigger the workflow manually. |
11 | 15 | workflow_dispatch: |
12 | | - branches: [ main ] |
13 | | - tags: [ 'v*.*.*' ] |
14 | 16 |
|
15 | 17 | env: |
16 | | - REGISTRY: ghcr.io |
17 | | - # github.repository as <account>/<repo> |
18 | | - IMAGE_NAME: ${{ github.repository }} |
| 18 | + # the target name of the image. |
| 19 | + IMAGE_NAME: deploy-from-github |
19 | 20 |
|
20 | 21 | jobs: |
21 | | - buildx: |
| 22 | + # Push image to GitHub Packages Container Registry. |
| 23 | + push: |
22 | 24 | runs-on: ubuntu-latest |
23 | | - steps: |
24 | | - - |
25 | | - name: Checkout |
26 | | - uses: actions/checkout@v3 |
27 | | - - |
28 | | - name: Set up Docker Buildx |
29 | | - id: buildx |
30 | | - uses: docker/setup-buildx-action@v2 |
31 | | - |
32 | | - # Login against a Docker registry except on PR |
33 | | - # https://github.com/docker/login-action |
34 | | - - name: Log into registry ${{ env.REGISTRY }} |
35 | | - if: github.event_name != 'pull_request' |
36 | | - uses: docker/login-action@v2 |
37 | | - with: |
38 | | - registry: ${{ env.REGISTRY }} |
39 | | - username: ${{ github.actor }} |
40 | | - password: ${{ secrets.GITHUB_TOKEN }} |
41 | | - |
42 | | - # Extract metadata (tags, labels) for Docker |
43 | | - # https://github.com/docker/metadata-action |
44 | | - - name: Extract Docker metadata |
45 | | - id: meta |
46 | | - uses: docker/metadata-action@v4 |
47 | | - with: |
48 | | - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
49 | | - |
50 | | - # Build and push Docker image with Buildx (don't push on PR) |
51 | | - # https://github.com/docker/build-push-action |
52 | | - - name: Build and push Docker image |
53 | | - id: build-and-push |
54 | | - uses: docker/build-push-action@v3 |
55 | | - with: |
56 | | - context: . |
57 | | - push: ${{ github.event_name != 'pull_request' }} |
58 | | - tags: ${{ steps.meta.outputs.tags }} |
59 | | - labels: ${{ steps.meta.outputs.labels }} |
| 25 | + permissions: |
| 26 | + packages: write |
| 27 | + contents: read |
60 | 28 |
|
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v3 |
| 31 | + |
| 32 | + - name: Build image |
| 33 | + run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}" |
| 34 | + |
| 35 | + - name: Log in to registry |
| 36 | + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin |
| 37 | + |
| 38 | + - name: Push image |
| 39 | + run: | |
| 40 | + IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME |
| 41 | +
|
| 42 | + # Change all uppercase to lowercase |
| 43 | + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') |
| 44 | + # Strip git ref prefix from version |
| 45 | + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') |
| 46 | + # Strip "v" prefix from tag name |
| 47 | + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') |
| 48 | + # Use Docker `latest` tag convention |
| 49 | + [ "$VERSION" == "main" ] && VERSION=latest |
| 50 | + echo IMAGE_ID=$IMAGE_ID |
| 51 | + echo VERSION=$VERSION |
| 52 | + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION |
| 53 | + docker push $IMAGE_ID:$VERSION |
0 commit comments