|
| 1 | +defaults: |
| 2 | + run: |
| 3 | + shell: "bash" |
| 4 | + |
| 5 | +name: "CI" |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: ["master"] |
| 10 | + tags: ["v*"] |
| 11 | + pull_request: |
| 12 | + branches: ["master"] |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + name: "Verify that Docker image build well" |
| 17 | + |
| 18 | + runs-on: "ubuntu-latest" |
| 19 | + |
| 20 | + steps: |
| 21 | + - uses: "actions/checkout@v2.4.0" |
| 22 | + |
| 23 | + - name: "Cache Docker layers" |
| 24 | + uses: "actions/cache@v2.1.6" |
| 25 | + with: |
| 26 | + path: "/tmp/.buildx-cache" |
| 27 | + key: "${{ runner.os }}-buildx-${{ github.sha }}" |
| 28 | + restore-keys: |
| 29 | + ${{ runner.os }}-buildx- |
| 30 | + |
| 31 | + - name: "Login to Docker Hub" |
| 32 | + uses: "docker/login-action@v1.10.0" |
| 33 | + with: |
| 34 | + username: "${{ secrets.DOCKER_HUB_USERNAME }}" |
| 35 | + password: "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" |
| 36 | + |
| 37 | + - id: "buildx" |
| 38 | + name: "Setup Docker Buildx" |
| 39 | + uses: "docker/setup-buildx-action@v1.6.0" |
| 40 | + |
| 41 | + - id: "docker_build" |
| 42 | + name: "Build Docker image" |
| 43 | + uses: "docker/build-push-action@v2.7.0" |
| 44 | + with: |
| 45 | + builder: "${{ steps.buildx.outputs.name }}" |
| 46 | + context: "./" |
| 47 | + file: "./Dockerfile" |
| 48 | + push: false |
| 49 | + tags: "${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.event.repository.name }}:latest" |
| 50 | + cache-from: "type=local,src=/tmp/.buildx-cache" |
| 51 | + cache-to: "type=local,dest=/tmp/.buildx-cache" |
| 52 | + |
| 53 | + - name: "Docker image digest" |
| 54 | + run: "echo ${{ steps.docker_build.outputs.digest }}" |
| 55 | + |
| 56 | + deploy: |
| 57 | + needs: ["build"] |
| 58 | + name: "Push Docker image to the Docker Hub" |
| 59 | + if: "${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/') }}" |
| 60 | + |
| 61 | + runs-on: "ubuntu-latest" |
| 62 | + |
| 63 | + steps: |
| 64 | + - uses: "actions/checkout@v2.4.0" |
| 65 | + |
| 66 | + - name: "Cache Docker layers" |
| 67 | + uses: "actions/cache@v2.1.6" |
| 68 | + with: |
| 69 | + path: "/tmp/.buildx-cache" |
| 70 | + key: "${{ runner.os }}-buildx-${{ github.sha }}" |
| 71 | + restore-keys: |
| 72 | + ${{ runner.os }}-buildx- |
| 73 | + |
| 74 | + - name: "Login to Docker Hub" |
| 75 | + uses: "docker/login-action@v1.10.0" |
| 76 | + with: |
| 77 | + username: "${{ secrets.DOCKER_HUB_USERNAME }}" |
| 78 | + password: "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" |
| 79 | + |
| 80 | + - id: "buildx" |
| 81 | + name: "Setup Docker Buildx" |
| 82 | + uses: "docker/setup-buildx-action@v1.6.0" |
| 83 | + |
| 84 | + - id: "deploy_info" |
| 85 | + name: "Setup deploy info" |
| 86 | + run: | |
| 87 | + set -euo pipefail |
| 88 | +
|
| 89 | + base_tag_name="${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.event.repository.name }}" |
| 90 | + tag_name="${base_tag_name}:latest" |
| 91 | +
|
| 92 | + if [ "${GITHUB_REF::10}" = "refs/tags/" ]; then |
| 93 | + git_tag_name="${GITHUB_REF:10}" |
| 94 | + tag_name="${base_tag_name}:${git_tag_name}" |
| 95 | + fi |
| 96 | +
|
| 97 | + echo "::set-output name=tag_name::${tag_name}" |
| 98 | +
|
| 99 | + - id: "docker_build" |
| 100 | + name: "Build & push Docker image" |
| 101 | + uses: "docker/build-push-action@v2.7.0" |
| 102 | + with: |
| 103 | + builder: "${{ steps.buildx.outputs.name }}" |
| 104 | + context: "./" |
| 105 | + file: "./Dockerfile" |
| 106 | + push: true |
| 107 | + tags: "${{ steps.deploy_info.outputs.tag_name }}" |
| 108 | + cache-from: "type=local,src=/tmp/.buildx-cache" |
| 109 | + cache-to: "type=local,dest=/tmp/.buildx-cache" |
| 110 | + |
| 111 | + - name: "Docker image digest" |
| 112 | + run: "echo ${{ steps.docker_build.outputs.digest }}" |
0 commit comments