docker #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: docker | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version_tag: | |
| type: string | |
| description: Container version tag. If not set, it will be created automatically based on the released version and commit hash. | |
| required: false | |
| default: '' | |
| release: | |
| types: [published] | |
| env: | |
| IMAGE_NAME: "scworkflow" # must be lowercase | |
| CONTEXT: "./" | |
| NAMESPACE: "nciccbr" | |
| USE_INPUT_TAG: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version_tag }} | |
| permissions: read-all | |
| jobs: | |
| build-docker: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Prepare build-time variables | |
| id: vars | |
| run: | | |
| echo "DATE=$(date +"%Y-%m-%d")" >> "$GITHUB_OUTPUT" | |
| if [ '${{ github.event_name }}' == 'release' ]; then | |
| # include latest tag since this is a release | |
| VERSION=${{ github.ref_name }} | |
| DOCKER_TAGS=${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}:${VERSION},${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}:latest | |
| elif [ '${{ env.USE_INPUT_TAG }}' == 'true' ]; then | |
| # use manual tag | |
| VERSION=${{ github.event.inputs.version_tag }} | |
| DOCKER_TAGS=${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}:${VERSION} | |
| else | |
| # set tag from last release and current commit | |
| HASH=$(git rev-parse --short HEAD) | |
| VERSION="$(grep 'Version:' $CONTEXT/DESCRIPTION | sed 's/Version: /v/')_${HASH}" | |
| DOCKER_TAGS=${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}:${VERSION} | |
| fi | |
| echo "VERSION_TAG=$(echo $VERSION)" >> "$GITHUB_OUTPUT" | |
| echo "DOCKER_TAGS=$(echo $DOCKER_TAGS)" >> "$GITHUB_OUTPUT" | |
| - name: debug | |
| run: | | |
| echo "Github tag: ${{ github.ref_name }}" | |
| echo "Github event_name: ${{ github.event_name }}" | |
| echo "Container version tag: ${{ steps.vars.outputs.VERSION_TAG }}" | |
| - name: Login to DockerHub | |
| if: ${{ github.event_name != 'pull_request' }} | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push | |
| uses: docker/build-push-action@v4 | |
| with: | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.vars.outputs.DOCKER_TAGS }} | |
| context: ${{ env.CONTEXT }} | |
| file: ${{ env.CONTEXT }}/Dockerfile | |
| build-args: | | |
| BUILD_DATE=${{ steps.vars.outputs.DATE }} | |
| BUILD_TAG=${{ steps.vars.outputs.VERSION_TAG }} | |
| REPONAME=${{ env.IMAGE_NAME }} |