Add test to docker workflow #4
Workflow file for this run
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: Build Docker image and push to ghcr.io | |
| on: | |
| push: | |
| tags: | |
| - "v*.*.*" | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| build_and_deploy: | |
| runs-on: ubuntu-latest | |
| env: | |
| IMG_NAME: ghcr.io/utrechtuniversity/caddy-reverse-proxy | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build new docker image | |
| id: build-image | |
| run: docker build -t ${{ env.IMG_NAME }}:latest . | |
| - name: Test new docker image | |
| id: test-image | |
| run: | | |
| BASIC_USER="foo" | |
| BASIC_PW="$(docker run caddy hash-password -p bar)" | |
| LISTEN_PORT=8080 | |
| docker run --rm --name test-build -d ${{ env.IMG_NAME }}:latest . && \ | |
| docker run test-build wget -S --spider http://127.0.0.1:$LISTEN_PORT/ | grep -q "401 Unauthorized" && \ | |
| docker run test-build wget -q -S --spider http://foo:bar@127.0.0.1:$LISTEN_PORT/ && \ | |
| docker kill test-build | |
| - name: Tag the image with all semantic versions | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| run: | | |
| VERSION=${GITHUB_REF#refs/tags/} | |
| VERSION=${VERSION:1} | |
| MAJOR=${VERSION%%.*} | |
| MINOR=${VERSION%.*} | |
| echo "Full version: $VERSION" # debug | |
| echo "Major version: $MAJOR" # debug | |
| echo "Minor version: $MINOR" # debug | |
| docker tag ${{ env.IMG_NAME }} "${{ env.IMG_NAME }}:$VERSION" | |
| docker tag ${{ env.IMG_NAME }} "${{ env.IMG_NAME }}:$MAJOR" | |
| docker tag ${{ env.IMG_NAME }} "${{ env.IMG_NAME }}:$MINOR" | |
| docker image list # debug | |
| - name: Push image | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| run: docker push --all-tags ${{ env.IMG_NAME }} |