Build and Push Docker image to GitLab Registry #22
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: Build and Push Docker image to GitLab Registry | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| packages: read | |
| env: | |
| IMAGE_NAME: inocs-sum-odp-impact-api | |
| REGISTRY: ${{ secrets.GITLAB_REGISTRY }} | |
| PROJECT_PATH: ${{ vars.GITLAB_PROJECT_PATH }} | |
| jobs: | |
| test: | |
| name: Run Tests | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.13" | |
| - name: Install pipenv | |
| run: pip install pipenv | |
| - name: Install dependencies | |
| run: pipenv install --dev | |
| - name: Run tests | |
| run: pipenv run pytest | |
| version: | |
| name: Extract Version | |
| runs-on: ubuntu-latest | |
| needs: test | |
| outputs: | |
| version: ${{ steps.get_version.outputs.version }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Extract version from __version__.py | |
| id: get_version | |
| run: | | |
| VERSION=$(python3 -c "exec(open('src/sum_impact_assessment/__version__.py').read()); print(__version__)") | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Extracted version: $VERSION" | |
| build: | |
| name: Build and Push Docker Image | |
| runs-on: ubuntu-latest | |
| needs: [test, version] | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitLab Container Registry | |
| run: echo "${{ secrets.GITLAB_PASSWORD }}" | docker login ${{ env.REGISTRY }} -u ${{ secrets.GITLAB_USER }} --password-stdin | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| provenance: false | |
| sbom: false | |
| tags: | | |
| ${{ env.REGISTRY }}/${{ env.PROJECT_PATH }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }} | |
| ${{ env.REGISTRY }}/${{ env.PROJECT_PATH }}/${{ env.IMAGE_NAME }}:latest | |
| cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_PATH }}/${{ env.IMAGE_NAME }}:latest | |
| cache-to: type=inline | |
| - name: Image Summary | |
| run: | | |
| echo "### Docker Image Built Successfully! :rocket:" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**Version:** \`${{ needs.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY | |
| echo "**Registry:** \`${{ env.REGISTRY }}\`" >> $GITHUB_STEP_SUMMARY | |
| echo "**Image Tags:**" >> $GITHUB_STEP_SUMMARY | |
| echo "- \`${{ env.REGISTRY }}/${{ env.PROJECT_PATH }}/${{ env.IMAGE_NAME }}:${{ needs.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY | |
| echo "- \`${{ env.REGISTRY }}/${{ env.PROJECT_PATH }}/${{ env.IMAGE_NAME }}:latest\`" >> $GITHUB_STEP_SUMMARY | |
| - name: Logout | |
| run: docker logout ${{ env.REGISTRY }} |