|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + working_directory: |
| 7 | + type: string |
| 8 | + description: What directory should the image be built from |
| 9 | + required: true |
| 10 | + image_name: |
| 11 | + type: string |
| 12 | + description: Name of Docker image |
| 13 | + required: true |
| 14 | + image_tag: |
| 15 | + type: string |
| 16 | + description: Tag for Docker image |
| 17 | + required: true |
| 18 | + push_image: |
| 19 | + type: boolean |
| 20 | + description: Should the image be pushed to the Github Container Registry |
| 21 | + required: false |
| 22 | + default: false |
| 23 | + secrets: |
| 24 | + GH_TOKEN: |
| 25 | + description: Github access token |
| 26 | + required: false |
| 27 | + SLACK_WEBHOOK_URL: |
| 28 | + description: Slack webhook URL to send messages to |
| 29 | + required: false |
| 30 | + outputs: |
| 31 | + image_name: |
| 32 | + description: Fully qualified image name |
| 33 | + value: ${{ jobs.build-image.outputs.image_name}} |
| 34 | + image_ref: |
| 35 | + description: Docker image reference |
| 36 | + value: ${{ jobs.build-image.outputs.image_ref }} |
| 37 | + |
| 38 | + workflow_dispatch: |
| 39 | + inputs: |
| 40 | + working_directory: |
| 41 | + type: string |
| 42 | + description: What directory should the image be built from |
| 43 | + required: true |
| 44 | + image_name: |
| 45 | + type: string |
| 46 | + description: Name of Docker image |
| 47 | + required: true |
| 48 | + image_tag: |
| 49 | + type: string |
| 50 | + description: Tag for Docker image |
| 51 | + required: true |
| 52 | + push_image: |
| 53 | + type: boolean |
| 54 | + description: Should the image be pushed to the Github Container Registry |
| 55 | + required: false |
| 56 | + default: false |
| 57 | + |
| 58 | +jobs: |
| 59 | + build-image: |
| 60 | + runs-on: ubuntu-22.04 |
| 61 | + name: Build and push image |
| 62 | + timeout-minutes: 30 |
| 63 | + |
| 64 | + outputs: |
| 65 | + image_name: ${{ steps.env_var.outputs.image_name }} |
| 66 | + image_ref: ${{ steps.env_var.outputs.image_ref }} |
| 67 | + |
| 68 | + steps: |
| 69 | + - name: Checkout Repository |
| 70 | + uses: actions/checkout@v2 |
| 71 | + |
| 72 | + - name: Set up Docker Buildx |
| 73 | + uses: docker/setup-buildx-action@v1.5.1 |
| 74 | + |
| 75 | + - name: Cache Docker layers |
| 76 | + uses: actions/cache@v2.1.6 |
| 77 | + with: |
| 78 | + path: /tmp/.buildx-cache |
| 79 | + key: ohw-docker-buildx-${{ inputs.image_name }}-${{ github.sha }} |
| 80 | + restore-keys: | |
| 81 | + ohw-docker-buildx-${{ inputs.image_name }} |
| 82 | +
|
| 83 | + - name: Set Job Environment Variables |
| 84 | + id: env_var |
| 85 | + run: | |
| 86 | + SHA7="${GITHUB_SHA::7}" |
| 87 | + DOCKER_TAG=$SHA7 |
| 88 | + IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/${{ inputs.image_name }}" |
| 89 | + echo "DOCKER_TAG=${{ inputs.image_tag }}" >> $GITHUB_ENV |
| 90 | + echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV |
| 91 | + echo "::set-output name=image_name::${IMAGE_NAME})" |
| 92 | + echo "::set-output name=image_ref::${DOCKER_TAG})" |
| 93 | +
|
| 94 | + - name: Build Docker Image |
| 95 | + uses: docker/build-push-action@v2.6.1 |
| 96 | + with: |
| 97 | + tags: | |
| 98 | + ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} |
| 99 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 100 | + cache-to: type=local,dest=/tmp/.buildx-cache-new |
| 101 | + push: false |
| 102 | + load: true |
| 103 | + context: ${{ inputs.working_directory }} |
| 104 | + |
| 105 | + - name: Docker image sizes |
| 106 | + run: | |
| 107 | + docker images | grep ${{ env.IMAGE_NAME }} |
| 108 | +
|
| 109 | + echo "### Image sizes" >> $GITHUB_STEP_SUMMARY |
| 110 | + docker images | grep ${{ env.IMAGE_NAME }} >> $GITHUB_STEP_SUMMARY |
| 111 | +
|
| 112 | + - name: Export Full Conda Environment |
| 113 | + run: | |
| 114 | + docker run ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} pixi list > conda-packages.txt |
| 115 | +
|
| 116 | + echo "### Conda Environment" >> $GITHUB_STEP_SUMMARY |
| 117 | + cat conda-packages.txt >> $GITHUB_STEP_SUMMARY |
| 118 | +
|
| 119 | + - name: Archive Conda Package List |
| 120 | + uses: actions/upload-artifact@v1 |
| 121 | + with: |
| 122 | + name: conda-packages |
| 123 | + path: conda-packages.txt |
| 124 | + |
| 125 | + - name: "Log into GitHub Container Registery" |
| 126 | + uses: docker/login-action@v1.9.0 |
| 127 | + if: ${{ inputs.push_image}} |
| 128 | + with: |
| 129 | + registry: ghcr.io |
| 130 | + username: ${{ github.repository_owner }} |
| 131 | + password: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} |
| 132 | + |
| 133 | + - name: Push Docker Image to GitHub Container Registry |
| 134 | + if: ${{ inputs.push_image }} |
| 135 | + run: docker push ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }} |
| 136 | + |
| 137 | + - name: Notify on newly built image |
| 138 | + if: ${{ inputs.push_image }} |
| 139 | + uses: slackapi/slack-github-action@v1.18.0 |
| 140 | + env: |
| 141 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 142 | + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK |
| 143 | + with: |
| 144 | + payload: | |
| 145 | + { |
| 146 | + "text": "Built image ${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}. Maybe let 2i2c know?", |
| 147 | + "blocks": [ |
| 148 | + { |
| 149 | + "type": "section", |
| 150 | + "text": { |
| 151 | + "type": "mrkdwn", |
| 152 | + "text": "Built image `${{ env.IMAGE_NAME }}:${{ env.DOCKER_TAG }}`. Maybe let 2i2c know?" |
| 153 | + } |
| 154 | + } |
| 155 | + ] |
| 156 | + } |
| 157 | +
|
| 158 | + - name: Move Docker Cache |
| 159 | + run: | |
| 160 | + rm -rf /tmp/.buildx-cache |
| 161 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
0 commit comments