diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index e9687b2..0fd0813 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -1,20 +1,14 @@ name: Push to registry on: - create: + push: + tags: + - 'v*' jobs: docker: - if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: - - name: Install jq if not present - run: | - if ! command -v jq &> /dev/null - then - sudo apt-get update - sudo apt-get install -y jq - fi - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx @@ -25,27 +19,35 @@ jobs: registry: http://registry.ellite.dev/ username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} + - name: Get the version + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/} - name: Build and push uses: docker/build-push-action@v5 with: push: true - tags: registry.ellite.dev/raibu-web:${{ github.ref_name }} - - name: Update latest tag - run: | - LATEST_TAG=$(curl -s https://api.github.com/repos/${{ github.repository }}/tags | jq -r '.[] | select(type=="object") | .name' | sort -V | tail -n 1) - CURRENT_TAG=${{ github.ref_name }} - SEMVER_REGEX="^v([0-9]+)\.([0-9]+)\.([0-9]+)$" - if [[ ! $CURRENT_TAG =~ $SEMVER_REGEX ]]; then - echo "Error: Tag $CURRENT_TAG does not follow semantic versioning" - exit 1 - fi - if [[ "$(printf '%s\n' "$LATEST_TAG" "$CURRENT_TAG" | sort -V | tail -n 1)" == "$CURRENT_TAG" ]]; then - docker tag registry.ellite.dev/raibu-web:${{ github.ref_name }} registry.ellite.dev/raibu-web:latest - docker push registry.ellite.dev/raibu-web:latest - fi + tags: registry.ellite.dev/raibu-web:${{ steps.get_version.outputs.VERSION }} + - name: Check if tag is the latest + id: check_latest + uses: actions/github-script@v3 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const tags = await github.repos.listTags({ + owner: context.repo.owner, + repo: context.repo.repo, + }); + const versions = tags.data.map(tag => tag.name).sort((a, b) => semver.rcompare(a, b)); + return versions[0] === '${{ steps.get_version.outputs.VERSION }}'; + - name: Build and push latest + if: steps.check_latest.outputs.result == 'true' + uses: docker/build-push-action@v5 + with: + push: true + tags: registry.ellite.dev/raibu-web:latest - name: Test image run: | - docker pull registry.ellite.dev/raibu-web:${{ github.ref_name }} + docker pull registry.ellite.dev/raibu-web:${{ steps.get_version.outputs.VERSION }} if [ $? -eq 0 ]; then echo "Image exists" else @@ -53,7 +55,6 @@ jobs: exit 1 fi tarball: - if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: - name: Checkout @@ -69,4 +70,4 @@ jobs: uses: actions/upload-artifact@v2 with: name: raibu-web - path: raibu-web.tar.gz + path: raibu-web.tar.gz \ No newline at end of file