Universal Docker CI #2
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: Universal Docker CI | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| owner_name: | |
| description: "Name of the owner of the repo (e.g. SWEProject25)" | |
| required: true | |
| repo_name: | |
| description: "Name of the private repo to clone (e.g. devops)" | |
| required: true | |
| branch: | |
| description: "Branch to build from" | |
| required: false | |
| default: main | |
| jobs: | |
| build-test-push: | |
| runs-on: ubuntu-latest | |
| env: | |
| OWNER_NAME: ${{ github.event.inputs.owner_name }} | |
| REPO_NAME: ${{ github.event.inputs.repo_name }} | |
| BRANCH: ${{ github.event.inputs.branch }} | |
| GITHUB_PAT: ${{ secrets.PAT_GITHUB }} | |
| DOCKER_USER: ${{ secrets.DOCKER_USERNAME }} | |
| DOCKER_PASS: ${{ secrets.DOCKER_PASSWORD }} | |
| steps: | |
| - name: Mask sensitive values | |
| run: | | |
| echo "::add-mask::${OWNER_NAME}" | |
| echo "::add-mask::${REPO_NAME}" | |
| echo "::add-mask::${GITHUB_PAT}" | |
| echo "::add-mask::${DOCKER_USER}" | |
| echo "::add-mask::${DOCKER_PASS}" | |
| - name: Clone private repo (silent) | |
| run: | | |
| git clone -q https://${GITHUB_PAT}@github.com/${OWNER_NAME}/${REPO_NAME}.git app | |
| cd app | |
| git checkout -q ${BRANCH} | |
| - name: Get short commit SHA | |
| id: vars | |
| working-directory: ./app | |
| run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Build Docker image (silent) | |
| working-directory: ./app | |
| run: | | |
| TAG=${{ steps.vars.outputs.sha_short }} | |
| docker build -t ghcr.io/${OWNER_NAME,,}/${REPO_NAME,,}:${TAG} . > /dev/null 2>&1 | |
| docker tag ghcr.io/${OWNER_NAME,,}/${REPO_NAME,,}:${TAG} ${DOCKER_USER}/${REPO_NAME,,}:${TAG} | |
| - name: Test Docker image (10s timeout) | |
| working-directory: ./app | |
| run: | | |
| TAG=${{ steps.vars.outputs.sha_short }} | |
| docker run -d --name test_container ghcr.io/${OWNER_NAME,,}/${REPO_NAME,,}:${TAG} > /dev/null 2>&1 || exit 1 | |
| sleep 10 | |
| docker stop test_container > /dev/null 2>&1 || true | |
| docker rm test_container > /dev/null 2>&1 || true | |
| - name: Login to GitHub Container Registry | |
| run: | | |
| echo "${GITHUB_PAT}" | docker login ghcr.io -u "${OWNER_NAME}" --password-stdin > /dev/null 2>&1 | |
| - name: Push to GitHub Container Registry (silent) | |
| run: | | |
| TAG=${{ steps.vars.outputs.sha_short }} | |
| docker push ghcr.io/${OWNER_NAME,,}/${REPO_NAME,,}:${TAG} > /dev/null 2>&1 | |
| - name: Login to Docker Hub | |
| run: | | |
| echo "${DOCKER_PASS}" | docker login -u "${DOCKER_USER}" --password-stdin > /dev/null 2>&1 | |
| - name: Push to Docker Hub (silent) | |
| run: | | |
| TAG=${{ steps.vars.outputs.sha_short }} | |
| docker push ${DOCKER_USER}/${REPO_NAME,,}:${TAG} > /dev/null 2>&1 |