|
| 1 | +name: Publish package to GitHub Packages |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + pull_request: |
| 7 | + |
| 8 | +env: |
| 9 | + IMAGE_NAME: action |
| 10 | + REGISTRY: ghcr.io |
| 11 | + |
| 12 | +jobs: |
| 13 | + test-image: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + |
| 17 | + - name: Check that the image builds |
| 18 | + run: docker build . --file Dockerfile |
| 19 | + validate-action: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + if: github.event_name == 'pull_request' |
| 22 | + steps: |
| 23 | + |
| 24 | + # This checks that .github/workflows/review-bot.yml is pointing towards the main branch |
| 25 | + # as, during development, we change this to use the code from the test branch and |
| 26 | + # we may forget to set it back to main |
| 27 | + - name: Validate that action points to main branch |
| 28 | + run: | |
| 29 | + BRANCH=$(yq '.jobs.set-auto-merge.steps[0].uses' $FILE_NAME | cut -d "@" -f2) |
| 30 | + # If the branch is not the main branch |
| 31 | + if [ "$BRANCH" != "$GITHUB_BASE_REF" ]; then |
| 32 | + echo "Action points to $BRANCH. It has to point to $GITHUB_BASE_REF instead!" |
| 33 | + exit 1 |
| 34 | + else |
| 35 | + echo "Action is correctly pointing to $GITHUB_BASE_REF" |
| 36 | + fi |
| 37 | + env: |
| 38 | + FILE_NAME: ".github/workflows/auto-merge-bot.yml" |
| 39 | + |
| 40 | + compare-versions: |
| 41 | + runs-on: ubuntu-latest |
| 42 | + outputs: |
| 43 | + version: ${{ steps.verification.outputs.VERSION }} |
| 44 | + exists: ${{ steps.checkTag.outputs.exists }} |
| 45 | + steps: |
| 46 | + |
| 47 | + - name: Extract package.json version |
| 48 | + id: package_version |
| 49 | + run: echo "VERSION=$(jq '.version' -r package.json)" >> $GITHUB_OUTPUT |
| 50 | + # Compare that the versions contain the same name |
| 51 | + - name: Compare versions |
| 52 | + id: verification |
| 53 | + uses: Bullrich/compare-version-on-action@main |
| 54 | + with: |
| 55 | + version: ${{ steps.package_version.outputs.VERSION }} |
| 56 | + # Verifies if there is a tag with that version number |
| 57 | + |
| 58 | + if: steps.verification.outputs.VERSION |
| 59 | + id: checkTag |
| 60 | + with: |
| 61 | + tag: v${{ steps.package_version.outputs.VERSION }} |
| 62 | + |
| 63 | + publish: |
| 64 | + if: github.event_name == 'push' && needs.compare-versions.outputs.exists == 'false' |
| 65 | + needs: [test-image, compare-versions] |
| 66 | + runs-on: ubuntu-latest |
| 67 | + permissions: |
| 68 | + contents: write |
| 69 | + packages: write |
| 70 | + steps: |
| 71 | + |
| 72 | + - name: Tag version and create release |
| 73 | + run: gh release create $VERSION --generate-notes |
| 74 | + env: |
| 75 | + VERSION: ${{ needs.compare-versions.outputs.version }} |
| 76 | + GH_TOKEN: ${{ github.token }} |
| 77 | + - name: Log in to the Container registry |
| 78 | + uses: docker/login-action@v3 |
| 79 | + with: |
| 80 | + registry: ${{ env.REGISTRY }} |
| 81 | + username: ${{ github.actor }} |
| 82 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + - name: Extract metadata (tags, labels) for Docker |
| 84 | + id: meta |
| 85 | + uses: docker/metadata-action@v5 |
| 86 | + with: |
| 87 | + images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ env.IMAGE_NAME }} |
| 88 | + tags: ${{ needs.compare-versions.outputs.version }} |
| 89 | + - uses: actions/checkout@v3 |
| 90 | + - name: Build and push Docker image |
| 91 | + uses: docker/build-push-action@v5 |
| 92 | + with: |
| 93 | + context: . |
| 94 | + push: true |
| 95 | + tags: ${{ steps.meta.outputs.tags }} |
| 96 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments