|
| 1 | +name: Push Docker images to DockerHub and ECR |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, master] |
| 6 | + |
| 7 | +jobs: |
| 8 | + multiple-registries: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + env: |
| 12 | + ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # 2.3.4 |
| 17 | + |
| 18 | + - name: Set up Docker Buildx |
| 19 | + uses: docker/setup-buildx-action@154c24e1f33dbb5865a021c99f1318cfebf27b32 # 1.1.1 |
| 20 | + |
| 21 | + - name: Cache Docker layers |
| 22 | + uses: actions/cache@0781355a23dac32fd3bac414512f4b903437991a # 2.1.3 |
| 23 | + with: |
| 24 | + path: /tmp/.buildx-cache |
| 25 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 26 | + restore-keys: | |
| 27 | + ${{ runner.os }}-buildx- |
| 28 | +
|
| 29 | + - name: Login to DockerHub |
| 30 | + uses: docker/login-action@f3364599c6aa293cdc2b8391b1b56d0c30e45c8a # 1.8.0 |
| 31 | + with: |
| 32 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 33 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 34 | + |
| 35 | + - name: Login to ECR |
| 36 | + uses: docker/login-action@f3364599c6aa293cdc2b8391b1b56d0c30e45c8a # 1.8.0 |
| 37 | + with: |
| 38 | + registry: ${{ env.ECR_REGISTRY }} |
| 39 | + username: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }} |
| 40 | + password: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }} |
| 41 | + |
| 42 | + - name: Build and push Docker image |
| 43 | + uses: docker/build-push-action@0db984c1826869dcd0740ff26ff75ff543238fd9 # 2.2.2 |
| 44 | + with: |
| 45 | + context: . |
| 46 | + file: ./Dockerfile |
| 47 | + push: true |
| 48 | + tags: | |
| 49 | + ${{ github.event.repository.full_name }}:latest |
| 50 | + ${{ github.event.repository.full_name }}:${{ github.sha }} |
| 51 | + ${{ env.ECR_REGISTRY }}/${{ github.event.repository.name }}:production |
| 52 | + ${{ env.ECR_REGISTRY }}/${{ github.event.repository.name }}:${{ github.sha }} |
| 53 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 54 | + cache-to: type=local,dest=/tmp/.buildx-cache |
0 commit comments