|
| 1 | +name: CD - Build and Push Docker Image |
| 2 | + |
| 3 | +# Déclenche le workflow après la réussite du workflow CI |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: ['CI - Build and Test'] |
| 7 | + types: |
| 8 | + - completed |
| 9 | + branches: |
| 10 | + - main |
| 11 | + # Permet également un déclenchement manuel |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +jobs: |
| 15 | + build-and-push: |
| 16 | + # Ne s'exécute que si le workflow CI a réussi |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + # Permissions nécessaires pour pousser vers GHCR |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + packages: write |
| 24 | + |
| 25 | + steps: |
| 26 | + # 1. Cloner le dépôt |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + |
| 30 | + # 2. Configurer Docker Buildx (pour builds multi-plateforme) |
| 31 | + - name: Set up Docker Buildx |
| 32 | + uses: docker/setup-buildx-action@v3 |
| 33 | + |
| 34 | + # 3. Se connecter à Docker Hub |
| 35 | + - name: Login to Docker Hub |
| 36 | + uses: docker/login-action@v3 |
| 37 | + with: |
| 38 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 39 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 40 | + |
| 41 | + # 4. Se connecter à GitHub Container Registry |
| 42 | + - name: Login to GitHub Container Registry |
| 43 | + uses: docker/login-action@v3 |
| 44 | + with: |
| 45 | + registry: ghcr.io |
| 46 | + username: ${{ github.actor }} |
| 47 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + |
| 49 | + # 5. Extraire les métadonnées (tags, labels) |
| 50 | + - name: Extract metadata |
| 51 | + id: meta |
| 52 | + uses: docker/metadata-action@v5 |
| 53 | + with: |
| 54 | + images: | |
| 55 | + johanve/sysinfo-api |
| 56 | + ghcr.io/${{ github.repository }} |
| 57 | + tags: | |
| 58 | + type=raw,value=latest |
| 59 | + type=sha,prefix={{branch}}- |
| 60 | +
|
| 61 | + # 6. Construire et pousser l'image vers Docker Hub et GHCR |
| 62 | + - name: Build and push Docker image |
| 63 | + uses: docker/build-push-action@v5 |
| 64 | + with: |
| 65 | + context: . |
| 66 | + file: ./Dockerfile |
| 67 | + push: true |
| 68 | + tags: ${{ steps.meta.outputs.tags }} |
| 69 | + labels: ${{ steps.meta.outputs.labels }} |
| 70 | + cache-from: type=gha |
| 71 | + cache-to: type=gha,mode=max |
| 72 | + |
| 73 | + # 7. Afficher l'empreinte de l'image |
| 74 | + - name: Image digest |
| 75 | + run: echo "Image pushed with digest ${{ steps.meta.outputs.digest }}" |
0 commit comments