fix: Update version prefix to develop-v0.1 in Docker workflow #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Main - build image and push | |
| # This workflow builds and pushes a Docker image to GitHub Container Registry. | |
| # It is triggered on push and pull_request events for the "main" branch, as well as manually via workflow_dispatch. | |
| # The image is tagged with a snapshot version and optionally with "latest". | |
| # The repository owner is converted to lowercase for consistent tagging. | |
| # The workflow uses the GITHUB_TOKEN secret for authentication with GitHub Container Registry. | |
| # Uncomment the Docker Hub section if you want to push to Docker Hub as well. | |
| # The Docker Hub credentials should be stored in the repository secrets as DOCKER_USERNAME and DOCKER_PASSWORD. | |
| # The workflow also includes a cleanup job to remove old Docker images from the registry. | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| workflow_dispatch: # allows manual triggering of the workflow | |
| env: | |
| VERSION_PREFIX: snapshot-v0.1. | |
| jobs: | |
| publish_image: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Pull latest changes | |
| run: git pull origin main --rebase | |
| - name: Set version string | |
| run: echo "VERSION=${{ env.VERSION_PREFIX }}${{ github.run_number }}" >> $GITHUB_ENV | |
| - name: Write version to file | |
| run: echo "__version__ = '${{ env.VERSION }}'" > src/version.py | |
| - name: Commit version file and push changes | |
| if: github.event_name == 'push' | |
| uses: devops-infra/action-commit-push@master | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| commit_prefix: "[AUTO] " | |
| commit_message: "Update version to ${{ env.VERSION }}" | |
| - name: Convert repository owner to lowercase | |
| run: echo "owner=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
| - name: Build image | |
| run: docker build -t ghcr.io/${{ env.owner }}/eos_connect:snapshot . | |
| # Only upload the image artifact for pull_request events | |
| - name: Upload Docker image as artifact | |
| if: github.event_name == 'pull_request' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: eos_connect_image-${{ env.VERSION }} | |
| path: eos_connect_${{ env.VERSION }}.tar.gz | |
| # Only run tagging and pushing tasks for "push" events | |
| - name: Log in to GitHub Container Registry | |
| if: github.event_name == 'push' | |
| run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
| - name: Tag image with snapshot version | |
| if: github.event_name == 'push' | |
| run: docker tag ghcr.io/${{ env.owner }}/eos_connect:snapshot ghcr.io/${{ env.owner }}/eos_connect:${{ env.VERSION }} | |
| - name: Tag image with latest | |
| if: github.event_name == 'push' | |
| run: docker tag ghcr.io/${{ env.owner }}/eos_connect:snapshot ghcr.io/${{ env.owner }}/eos_connect:latest | |
| - name: Push Docker image to GitHub Container Registry | |
| if: github.event_name == 'push' | |
| run: | | |
| docker push ghcr.io/${{ env.owner }}/eos_connect:snapshot | |
| docker push ghcr.io/${{ env.owner }}/eos_connect:${{ env.VERSION }} | |
| docker push ghcr.io/${{ env.owner }}/eos_connect:latest | |
| # - name: Log in to Docker Hub | |
| # run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | |
| # - name: Push image to Docker Hub | |
| # run: docker push ${{ secrets.DOCKER_USERNAME }}/eos_connect:nightly | |
| cleanup_old_develops: | |
| needs: publish_image | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Cleanup old Docker images | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| echo "Cleaning up old Docker images..." | |
| ghcr_images=$(gh api -H "Authorization: token $GH_TOKEN" /user/packages/container/eos_connect/versions | jq -r '.[] | select(.metadata.container.tags[] | startswith("${{ env.VERSION_PREFIX }}")) | .id' | tail -n +4) | |
| for image_id in $ghcr_images; do | |
| gh api -X DELETE -H "Authorization: token $GH_TOKEN" /user/packages/container/eos_connect/versions/$image_id | |
| done |