|
| 1 | +name: Build and Push Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + aws_ecr: |
| 7 | + description: "Push to AWS ECR" |
| 8 | + default: true |
| 9 | + required: false |
| 10 | + type: boolean |
| 11 | + docker_hub: |
| 12 | + description: "Push to Docker Hub" |
| 13 | + default: true |
| 14 | + required: false |
| 15 | + type: boolean |
| 16 | + provenance: |
| 17 | + description: "Generate provenance attestation for the build" |
| 18 | + default: true |
| 19 | + required: false |
| 20 | + type: boolean |
| 21 | + image_name: |
| 22 | + description: "The name of the image to deploy (default: repo name)" |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + platform: |
| 26 | + description: "The image's platform (default: linux/amd64)" |
| 27 | + default: "linux/amd64" |
| 28 | + required: false |
| 29 | + type: string |
| 30 | + secrets: |
| 31 | + AWS_ACCOUNT_ID: |
| 32 | + description: "The AWS account ID used to determine the ECR registry" |
| 33 | + required: true |
| 34 | + AWS_REGION: |
| 35 | + description: "The AWS region used to determine the ECR registry" |
| 36 | + required: true |
| 37 | + AWS_ECR_ACCESS_KEY_ID: |
| 38 | + description: "The access key ID used to log into AWS ECR" |
| 39 | + required: true |
| 40 | + AWS_ECR_SECRET_ACCESS_KEY: |
| 41 | + description: "The secret access key ID used to log into AWS ECR" |
| 42 | + required: true |
| 43 | + DOCKERHUB_USERNAME: |
| 44 | + description: "The username used to log into Docker Hub" |
| 45 | + required: true |
| 46 | + DOCKERHUB_PASSWORD: |
| 47 | + description: "The password used to log into Docker Hub" |
| 48 | + required: true |
| 49 | + DOCKER_BUILD_ARGS: |
| 50 | + description: "Docker build arguments" |
| 51 | + required: false |
| 52 | + |
| 53 | +permissions: |
| 54 | + contents: write |
| 55 | + |
| 56 | +jobs: |
| 57 | + build-and-push: |
| 58 | + runs-on: ubuntu-22.04 |
| 59 | + |
| 60 | + env: |
| 61 | + ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com |
| 62 | + |
| 63 | + steps: |
| 64 | + - name: Checkout code |
| 65 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 |
| 66 | + with: |
| 67 | + # Never deploy from non-main branches |
| 68 | + ref: main |
| 69 | + |
| 70 | + - name: Check if Dockerfile is present |
| 71 | + id: dockerfile-exists |
| 72 | + run: | |
| 73 | + dockerfile_exists=$(test -f Dockerfile && echo 'true' || echo 'false') |
| 74 | + if [ "${dockerfile_exists}" == "false" ]; then |
| 75 | + echo "::warning:: Skip deploy due to missing Dockerfile" |
| 76 | + fi |
| 77 | + echo "result=${dockerfile_exists}" >> $GITHUB_OUTPUT |
| 78 | +
|
| 79 | + - name: Set up Docker |
| 80 | + uses: docker/setup-docker-action@v4 |
| 81 | + with: |
| 82 | + daemon-config: | |
| 83 | + { |
| 84 | + "features": { |
| 85 | + "containerd-snapshotter": true |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + - name: Set up QEMU |
| 90 | + uses: docker/setup-qemu-action@v3 |
| 91 | + |
| 92 | + - name: Set up Docker Buildx |
| 93 | + if: steps.dockerfile-exists.outputs.result == 'true' |
| 94 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 |
| 95 | + |
| 96 | + - name: Login to DockerHub |
| 97 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef |
| 98 | + if: ${{steps.dockerfile-exists.outputs.result == 'true' && inputs.docker_hub}} |
| 99 | + with: |
| 100 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 101 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 102 | + |
| 103 | + - name: Login to ECR |
| 104 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef |
| 105 | + if: ${{steps.dockerfile-exists.outputs.result == 'true' && inputs.aws_ecr}} |
| 106 | + with: |
| 107 | + registry: ${{ env.ECR_REGISTRY }} |
| 108 | + username: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }} |
| 109 | + password: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }} |
| 110 | + |
| 111 | + - name: Build Docker image |
| 112 | + if: ${{steps.dockerfile-exists.outputs.result == 'true' && (inputs.docker_hub || inputs.aws_ecr)}} |
| 113 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 |
| 114 | + with: |
| 115 | + context: . |
| 116 | + file: ./Dockerfile |
| 117 | + load: true |
| 118 | + cache-from: type=gha |
| 119 | + cache-to: type=gha,mode=max |
| 120 | + build-args: ${{ secrets.DOCKER_BUILD_ARGS }} |
| 121 | + provenance: false |
| 122 | + platforms: ${{ inputs.platform }} |
| 123 | + |
| 124 | + - name: Push to Docker Hub |
| 125 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 |
| 126 | + if: ${{steps.dockerfile-exists.outputs.result == 'true' && inputs.docker_hub}} |
| 127 | + with: |
| 128 | + context: . |
| 129 | + file: ./Dockerfile |
| 130 | + push: true |
| 131 | + tags: | |
| 132 | + sencudra/${{ inputs.image_name || github.event.repository.name }}:latest |
| 133 | + sencudra/${{ inputs.image_name || github.event.repository.name }}:${{ github.sha }} |
| 134 | + cache-from: type=gha |
| 135 | + cache-to: type=gha,mode=max |
| 136 | + build-args: ${{ secrets.DOCKER_BUILD_ARGS }} |
| 137 | + provenance: false |
| 138 | + platforms: ${{ inputs.platform }} |
| 139 | + |
| 140 | + - name: Push to AWS ECR |
| 141 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 |
| 142 | + if: ${{steps.dockerfile-exists.outputs.result == 'true' && inputs.aws_ecr}} |
| 143 | + with: |
| 144 | + context: . |
| 145 | + file: ./Dockerfile |
| 146 | + push: true |
| 147 | + tags: | |
| 148 | + ${{ env.ECR_REGISTRY }}/${{ inputs.image_name || github.event.repository.name }}:production |
| 149 | + ${{ env.ECR_REGISTRY }}/${{ inputs.image_name || github.event.repository.name }}:${{ github.sha }} |
| 150 | + cache-from: type=gha |
| 151 | + cache-to: type=gha,mode=max |
| 152 | + build-args: ${{ secrets.DOCKER_BUILD_ARGS }} |
| 153 | + provenance: false |
| 154 | + platforms: ${{ inputs.platform }} |
0 commit comments