|
7 | 7 |
|
8 | 8 | env:
|
9 | 9 | IMAGE_NAME: action
|
| 10 | + REGISTRY: ghcr.io |
10 | 11 |
|
11 | 12 | jobs:
|
12 | 13 | test-image:
|
13 | 14 | runs-on: ubuntu-latest
|
14 | 15 | steps:
|
15 |
| - - uses: actions/checkout@v3.3.0 |
| 16 | + - uses: actions/checkout@v4.1.1 |
16 | 17 | - name: Check that the image builds
|
17 | 18 | run: docker build . --file Dockerfile
|
18 | 19 |
|
19 |
| - test-versions: |
| 20 | + compare-versions: |
20 | 21 | runs-on: ubuntu-latest
|
| 22 | + outputs: |
| 23 | + version: ${{ steps.verification.outputs.VERSION }} |
| 24 | + exists: ${{ steps.checkTag.outputs.exists }} |
21 | 25 | steps:
|
22 |
| - - uses: actions/checkout@v3.3.0 |
| 26 | + - uses: actions/checkout@v4.1.1 |
23 | 27 | - name: Extract package.json version
|
24 | 28 | id: package_version
|
25 | 29 | run: echo "VERSION=$(jq '.version' -r package.json)" >> $GITHUB_OUTPUT
|
26 |
| - - name: Extract action.yml version |
27 |
| - uses: mikefarah/yq@master |
28 |
| - id: action_image |
29 |
| - with: |
30 |
| - cmd: yq '.runs.image' 'action.yml' |
31 |
| - - name: Parse action.yml version |
32 |
| - id: action_version |
33 |
| - run: | |
34 |
| - echo "IMAGE_VERSION=$(echo $IMAGE_URL | cut -d: -f3)" >> $GITHUB_OUTPUT |
35 |
| - env: |
36 |
| - IMAGE_URL: ${{ steps.action_image.outputs.result }} |
| 30 | + # Compare that the versions contain the same name |
37 | 31 | - name: Compare versions
|
38 |
| - run: | |
39 |
| - echo "Verifying that $IMAGE_VERSION from action.yml is the same as $PACKAGE_VERSION from package.json" |
40 |
| - [[ $IMAGE_VERSION == $PACKAGE_VERSION ]] |
41 |
| - env: |
42 |
| - IMAGE_VERSION: ${{ steps.action_version.outputs.IMAGE_VERSION }} |
43 |
| - PACKAGE_VERSION: ${{ steps.package_version.outputs.VERSION }} |
44 |
| - |
45 |
| - tag: |
46 |
| - if: github.event_name == 'push' |
47 |
| - needs: [test-image, test-versions] |
48 |
| - runs-on: ubuntu-latest |
49 |
| - permissions: |
50 |
| - contents: write |
51 |
| - outputs: |
52 |
| - tagcreated: ${{ steps.autotag.outputs.tagcreated }} |
53 |
| - tagname: ${{ steps.autotag.outputs.tagname }} |
54 |
| - steps: |
55 |
| - - uses: actions/checkout@v3.3.0 |
56 |
| - with: |
57 |
| - fetch-depth: 0 |
58 |
| - - uses: butlerlogic/action-autotag@stable |
59 |
| - id: autotag |
| 32 | + id: verification |
| 33 | + uses: Bullrich/compare-version-on-action@main |
60 | 34 | with:
|
61 |
| - head_branch: master |
62 |
| - tag_prefix: "v" |
63 |
| - env: |
64 |
| - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
65 |
| - - name: Changelog |
66 |
| - uses: Bullrich/generate-release-changelog@2.0.2 |
67 |
| - id: Changelog |
68 |
| - env: |
69 |
| - REPO: ${{ github.repository }} |
70 |
| - - name: Create Release |
71 |
| - if: steps.autotag.outputs.tagname != '' |
72 |
| - uses: actions/create-release@latest |
73 |
| - env: |
74 |
| - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
75 |
| - with: |
76 |
| - tag_name: ${{ steps.autotag.outputs.tagname }} |
77 |
| - release_name: Release ${{ steps.autotag.outputs.tagname }} |
78 |
| - body: | |
79 |
| - ${{ steps.Changelog.outputs.changelog }} |
| 35 | + version: ${{ steps.package_version.outputs.VERSION }} |
| 36 | + # Verifies if there is a tag with that version number |
| 37 | + - uses: mukunku/tag-exists-action@v1.4.0 |
| 38 | + if: steps.verification.outputs.VERSION |
| 39 | + id: checkTag |
| 40 | + with: |
| 41 | + tag: v${{ steps.package_version.outputs.VERSION }} |
80 | 42 |
|
81 | 43 | publish:
|
| 44 | + if: github.event_name == 'push' && needs.compare-versions.outputs.exists == 'false' |
| 45 | + needs: [test-image, compare-versions] |
82 | 46 | runs-on: ubuntu-latest
|
83 | 47 | permissions:
|
| 48 | + contents: write |
84 | 49 | packages: write
|
85 |
| - needs: [tag] |
86 |
| - if: needs.tag.outputs.tagname != '' |
87 | 50 | steps:
|
88 |
| - - uses: actions/checkout@v3 |
89 |
| - - name: Build image |
90 |
| - run: docker build . --file Dockerfile --tag $IMAGE_NAME |
91 |
| - - name: Log into registry |
92 |
| - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin |
93 |
| - - name: Push image |
94 |
| - run: | |
95 |
| - IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME |
96 |
| -
|
97 |
| - # Change all uppercase to lowercase |
98 |
| - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') |
99 |
| -
|
100 |
| - # Strip git ref prefix from version |
101 |
| - VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') |
102 |
| -
|
103 |
| - # Strip "v" prefix from tag name |
104 |
| - [[ ! -z $TAG ]] && VERSION=$(echo $TAG | sed -e 's/^v//') |
105 |
| -
|
106 |
| - # Use Docker `latest` tag convention |
107 |
| - [ "$VERSION" == "main" ] && VERSION=latest |
108 |
| -
|
109 |
| - echo IMAGE_ID=$IMAGE_ID |
110 |
| - echo VERSION=$VERSION |
111 |
| -
|
112 |
| - docker tag $IMAGE_NAME $IMAGE_ID:$VERSION |
113 |
| - docker push $IMAGE_ID:$VERSION |
| 51 | + - uses: actions/checkout@v4.1.1 |
| 52 | + - name: Tag version and create release |
| 53 | + run: gh release create $VERSION --generate-notes |
114 | 54 | env:
|
115 |
| - TAG: ${{ needs.tag.outputs.tagname }} |
| 55 | + VERSION: v${{ needs.compare-versions.outputs.version }} |
| 56 | + GH_TOKEN: ${{ github.token }} |
| 57 | + - name: Log in to the Container registry |
| 58 | + uses: docker/login-action@v3 |
| 59 | + with: |
| 60 | + registry: ${{ env.REGISTRY }} |
| 61 | + username: ${{ github.actor }} |
| 62 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + - name: Extract metadata (tags, labels) for Docker |
| 64 | + id: meta |
| 65 | + uses: docker/metadata-action@v5 |
| 66 | + with: |
| 67 | + images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ env.IMAGE_NAME }} |
| 68 | + tags: ${{ needs.compare-versions.outputs.version }} |
| 69 | + - uses: actions/checkout@v3 |
| 70 | + - name: Build and push Docker image |
| 71 | + uses: docker/build-push-action@v5 |
| 72 | + with: |
| 73 | + context: . |
| 74 | + push: true |
| 75 | + tags: ${{ steps.meta.outputs.tags }} |
| 76 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments