|
| 1 | +name: Docker build and push |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - '*' |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - 'master' |
| 12 | + - 'releases/v*' |
| 13 | + - 'feature/*' |
| 14 | + |
| 15 | +env: |
| 16 | + # TEST_TARGET: Name of the testing target in the Dockerfile |
| 17 | + TEST_TARGET: testing |
| 18 | + |
| 19 | + # DO_TEST - true to build and run unit tests, false to skip the tests |
| 20 | + DO_TEST: false |
| 21 | + |
| 22 | + # DO_PUSH - true to push to the HPE_DEPLOY_REPO, false to not push |
| 23 | + DO_PUSH: true |
| 24 | + |
| 25 | +jobs: |
| 26 | + build: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v2 |
| 31 | + |
| 32 | + - name: Lowercase repository name for docker build |
| 33 | + id: lowercase-repository-name |
| 34 | + run: | |
| 35 | + echo "HPE_BUILD_REPO=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 36 | + echo "HPE_DEPLOY_REPO=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 37 | +
|
| 38 | + - name: set TAG & TESTTAG for main/master |
| 39 | + if: ${{ github.event.repository.default_branch == 'main' || github.event.repository.default_branch == 'master' }} |
| 40 | + run: | |
| 41 | + echo "TAG=${{ github.sha }}" >> ${GITHUB_ENV} |
| 42 | + echo "LATESTTAG=latest" >> ${GITHUB_ENV} |
| 43 | + echo "TESTTAG=test-${{ github.sha }}" >> ${GITHUB_ENV} |
| 44 | +
|
| 45 | + - name: set TAG & TESTTAG for all other branches |
| 46 | + if: ${{ github.event.repository.default_branch != 'main' && github.event.repository.default_branch != 'master' }} |
| 47 | + run: | |
| 48 | + echo "TAG=dev-${{ github.sha }}" >> ${GITHUB_ENV} |
| 49 | + echo "LATESTTAG=dev-latest" >> ${GITHUB_ENV} |
| 50 | + echo "TESTTAG=test-${{ github.sha }}" >> ${GITHUB_ENV} |
| 51 | +
|
| 52 | + - name: Docker login |
| 53 | + uses: docker/login-action@v1 |
| 54 | + with: |
| 55 | + registry: ghcr.io |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Build the test Docker image |
| 60 | + if: ${{ env.DO_TEST == 'true' }} |
| 61 | + id: docker_build_test_target |
| 62 | + uses: docker/build-push-action@v2 |
| 63 | + with: |
| 64 | + push: false |
| 65 | + target: ${{ env.TEST_TARGET }} |
| 66 | + tags: ${{ env.HPE_BUILD_REPO }}:${{ env.TESTTAG }} |
| 67 | + |
| 68 | + - name: Run the Docker image unit tests |
| 69 | + if: ${{ env.DO_TEST == 'true' }} |
| 70 | + run: docker run ${HPE_BUILD_REPO}:${TESTTAG} |
| 71 | + |
| 72 | + - name: Build the final Docker image |
| 73 | + id: docker_build |
| 74 | + uses: docker/build-push-action@v2 |
| 75 | + with: |
| 76 | + push: false |
| 77 | + tags: ${{ env.HPE_BUILD_REPO }}:${{ env.TAG }} |
| 78 | + |
| 79 | + - name: Peek at the docker images |
| 80 | + run: docker images |
| 81 | + |
| 82 | + - name: Tag the build |
| 83 | + run: docker tag docker.io/${HPE_BUILD_REPO}:${TAG} ghcr.io/${HPE_DEPLOY_REPO}:${TAG} |
| 84 | + |
| 85 | + - name: Tag the build as latest |
| 86 | + run: docker tag ${HPE_BUILD_REPO}:${TAG} ghcr.io/${HPE_DEPLOY_REPO}:${LATESTTAG} |
| 87 | + |
| 88 | + - name: Push the tagged build |
| 89 | + if: ${{ env.DO_PUSH == 'true' }} |
| 90 | + run: docker push ghcr.io/${HPE_DEPLOY_REPO}:${TAG} |
| 91 | + |
| 92 | + - name: Push the build tagged as latest |
| 93 | + if: ${{ env.DO_PUSH == 'true' }} |
| 94 | + run: docker push ghcr.io/${HPE_DEPLOY_REPO}:${LATESTTAG} |
0 commit comments