|
| 1 | +name: Build & Deploy Docker |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + dockerfile: |
| 7 | + description: "Path to Dockerfile" |
| 8 | + default: "Dockerfile" |
| 9 | + required: false |
| 10 | + type: string |
| 11 | + image_name: |
| 12 | + description: "Full image name (e.g. org/my-api)" |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + image_tag: |
| 16 | + description: "Optional tag override (defaults to pushed Git tag)" |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + remote_host: |
| 20 | + description: "SSH host (user@host)" |
| 21 | + required: true |
| 22 | + type: string |
| 23 | + remote_path: |
| 24 | + description: "Remote path where compose files live" |
| 25 | + required: true |
| 26 | + type: string |
| 27 | + runner_group: |
| 28 | + description: "Runner group or label" |
| 29 | + required: false |
| 30 | + default: "ubuntu-latest" |
| 31 | + type: string |
| 32 | + secrets: |
| 33 | + dockerhub_username: |
| 34 | + required: true |
| 35 | + dockerhub_password: |
| 36 | + required: true |
| 37 | + ssh_private_key: |
| 38 | + required: true |
| 39 | + outputs: |
| 40 | + tag: |
| 41 | + description: "Tag effectively built/deployed" |
| 42 | + value: ${{ jobs.get-tag.outputs.tag }} |
| 43 | + |
| 44 | +permissions: |
| 45 | + id-token: write |
| 46 | + contents: read |
| 47 | + |
| 48 | +jobs: |
| 49 | + get-tag: |
| 50 | + runs-on: ${{ inputs.runner_group }} |
| 51 | + outputs: |
| 52 | + tag: ${{ steps.out.outputs.tag }} |
| 53 | + steps: |
| 54 | + - name: Checkout |
| 55 | + uses: actions/checkout@v4 |
| 56 | + with: |
| 57 | + fetch-depth: 0 |
| 58 | + |
| 59 | + - name: Compute tag |
| 60 | + id: out |
| 61 | + run: | |
| 62 | + TAG="${{ inputs.image_tag }}" |
| 63 | + if [ -z "$TAG" ]; then |
| 64 | + TAG="${GITHUB_REF##*/}" # refs/tags/v1.2.3 → v1.2.3 |
| 65 | + fi |
| 66 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + build: |
| 69 | + needs: get-tag |
| 70 | + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/[email protected] |
| 71 | + with: |
| 72 | + dockerfile: ${{ inputs.dockerfile }} |
| 73 | + image-name: ${{ inputs.image_name }} |
| 74 | + image-tag: ${{ needs.get-tag.outputs.tag }} |
| 75 | + hadolint: false |
| 76 | + security-scan: false |
| 77 | + push: true |
| 78 | + secrets: |
| 79 | + username: ${{ secrets.dockerhub_username }} |
| 80 | + password: ${{ secrets.dockerhub_password }} |
| 81 | + |
| 82 | + deploy: |
| 83 | + needs: [build, get-tag] |
| 84 | + runs-on: ${{ inputs.runner_group }} |
| 85 | + |
| 86 | + steps: |
| 87 | + - name: Checkout |
| 88 | + uses: actions/checkout@v4 |
| 89 | + with: |
| 90 | + fetch-depth: 0 |
| 91 | + |
| 92 | + - name: Install SSH key |
| 93 | + uses: webfactory/[email protected] |
| 94 | + with: |
| 95 | + ssh-private-key: ${{ secrets.ssh_private_key }} |
| 96 | + |
| 97 | + - name: Add remote host to known_hosts |
| 98 | + run: ssh-keyscan -H "${{ inputs.remote_host#*@ }}" >> ~/.ssh/known_hosts |
| 99 | + |
| 100 | + - name: Prepare .env for Compose |
| 101 | + run: | |
| 102 | + cat <<EOF > .env |
| 103 | +IMAGE_NAME=${{ inputs.image_name }} |
| 104 | +IMAGE_TAG=${{ needs.get-tag.outputs.tag }} |
| 105 | +DOCKERHUB_USERNAME=${{ secrets.dockerhub_username }} |
| 106 | +DOCKERHUB_PASSWORD=${{ secrets.dockerhub_password }} |
| 107 | +EOF |
| 108 | + |
| 109 | + - name: Copy compose files |
| 110 | + run: | |
| 111 | + scp docker-compose.yml .env "${{ inputs.remote_host }}":"${{ inputs.remote_path }}/" |
| 112 | +
|
| 113 | + - name: Pull & restart containers |
| 114 | + run: | |
| 115 | + ssh "${{ inputs.remote_host }}" bash -s <<'REMOTE' |
| 116 | + cd "${{ inputs.remote_path }}" |
| 117 | + set -e |
| 118 | + export \$(grep -v '^#' .env | xargs) |
| 119 | + echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin |
| 120 | + docker compose pull |
| 121 | + docker compose up -d |
| 122 | +REMOTE |
0 commit comments