|
| 1 | +# Combine workflow for pull-request, push-to-main and release events. |
| 2 | + |
| 3 | +name: Build and release execution environment |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + registry: |
| 9 | + description: The registry to which the image will be pushed. |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + secrets: |
| 13 | + registry_username: |
| 14 | + required: true |
| 15 | + registry_password: |
| 16 | + required: true |
| 17 | + registry_redhat_username: |
| 18 | + required: false |
| 19 | + registry_redhat_password: |
| 20 | + required: false |
| 21 | + |
| 22 | +jobs: |
| 23 | + prepare: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout repo |
| 27 | + uses: actions/checkout@v2 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + debug: |
| 32 | + if: github.event_name == 'pull_request_target' |
| 33 | + needs: prepare |
| 34 | + runs-on: ubuntu-latest |
| 35 | + environment: test |
| 36 | + steps: |
| 37 | + - name: Checkout repo |
| 38 | + uses: actions/checkout@v2 |
| 39 | + with: |
| 40 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 41 | + fetch-depth: 0 |
| 42 | + ref: ${{ github.event.pull_request.head.ref != '' && github.event.pull_request.head.ref || 'main' }} |
| 43 | + |
| 44 | + - name: Print working directory |
| 45 | + run: pwd |
| 46 | + |
| 47 | + - name: List files in the directory |
| 48 | + run: ls -lahR |
| 49 | + |
| 50 | + - name: List environment variables |
| 51 | + run: printenv | sort |
| 52 | + |
| 53 | + - name: Show git branch and commit |
| 54 | + run: | |
| 55 | + echo "Current Branch:" |
| 56 | + git branch |
| 57 | + echo "Current Commit:" |
| 58 | + git rev-parse HEAD |
| 59 | +
|
| 60 | + - name: Fetch all branches |
| 61 | + run: git fetch --all |
| 62 | + |
| 63 | + - name: List all remote branches |
| 64 | + run: git branch -r |
| 65 | + |
| 66 | + - name: Show detailed git diff |
| 67 | + run: git diff origin/main |
| 68 | + |
| 69 | + build-ee: |
| 70 | + needs: prepare |
| 71 | + outputs: |
| 72 | + push_success: ${{ steps.push_to_ghcr.outputs.push_success }} |
| 73 | + runs-on: ubuntu-latest |
| 74 | + environment: ${{ github.event_name }} |
| 75 | + steps: |
| 76 | + - name: Checkout repo |
| 77 | + uses: actions/checkout@v2 |
| 78 | + with: |
| 79 | + fetch-depth: 0 |
| 80 | + |
| 81 | + - name: Fetch the base and head refs only if a push and release |
| 82 | + if: github.event_name == 'push' || github.event_name == 'release' |
| 83 | + run: | |
| 84 | + git fetch origin ${{ github.base_ref }} |
| 85 | + git fetch origin ${{ github.head_ref }} |
| 86 | +
|
| 87 | + - name: Install python requirements (ansible-builder) |
| 88 | + run: pip install ansible-builder |
| 89 | + |
| 90 | + - name: Define common environment variables |
| 91 | + run: | |
| 92 | + echo "EE=`yq -r '.options.tags[0]' 'execution-environment.yml'`" >> $GITHUB_ENV |
| 93 | + echo "EE_file=execution-environment.yml" >> $GITHUB_ENV |
| 94 | + echo "IMAGE_REGISTRY=ghcr.io" >> $GITHUB_ENV |
| 95 | +
|
| 96 | + - name: Define environment variables for PR |
| 97 | + if: github.event_name == 'pull_request_target' |
| 98 | + run: | |
| 99 | + SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7) |
| 100 | + echo "IMAGE_TAG=pr-${{ github.event.number }}-$SHORT_SHA" >> $GITHUB_ENV |
| 101 | +
|
| 102 | + - name: Define environment variables for push and release |
| 103 | + if: github.event_name == 'push' || github.event_name == 'release' |
| 104 | + run: | |
| 105 | + echo "IMAGE_TAG=latest" >> $GITHUB_ENV |
| 106 | +
|
| 107 | + - name: Print the environment variables |
| 108 | + run: | |
| 109 | + echo $GITHUB_ENV |
| 110 | +
|
| 111 | + - name: Login to ghcr |
| 112 | + uses: redhat-actions/podman-login@v1 |
| 113 | + with: |
| 114 | + registry: ${{ env.IMAGE_REGISTRY }} |
| 115 | + username: ${{ github.actor }} |
| 116 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 117 | + |
| 118 | + - name: (devel) Build image and create artifact |
| 119 | + run: | |
| 120 | + echo "Would build: ${{ env.EE }}" |
| 121 | +
|
| 122 | + - name: Build image and create artifact |
| 123 | + run: | |
| 124 | + ansible-builder build -v 3 \ |
| 125 | + --build-arg AH_TOKEN=${{ secrets.AH_TOKEN }} \ |
| 126 | + --context=../${{ env.EE }} \ |
| 127 | + --tag=${{ env.EE }}:${{ env.IMAGE_TAG }} \ |
| 128 | + --tag=${{ env.EE }}:${{ github.sha }} |
| 129 | +
|
| 130 | + # Create artifact file |
| 131 | + COMMANDS_FILE="commands-${{ env.EE }}.txt" |
| 132 | + echo "" >> $COMMANDS_FILE |
| 133 | + echo "EE: ${{ env.EE }}" >> $COMMANDS_FILE |
| 134 | + echo "" >> $COMMANDS_FILE |
| 135 | + echo "\`\`\`" > $COMMANDS_FILE |
| 136 | + echo "podman pull ${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ env.EE }}:${{ env.IMAGE_TAG }}" >> $COMMANDS_FILE |
| 137 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 138 | + echo "<details>" >> $COMMANDS_FILE |
| 139 | + echo "<summary><b>More info...</b></summary>" >> $COMMANDS_FILE |
| 140 | + echo "" >> $COMMANDS_FILE |
| 141 | + echo "#### Installed collections" >> $COMMANDS_FILE |
| 142 | + echo "" >> $COMMANDS_FILE |
| 143 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 144 | + podman run -it ${{ env.EE }}:${{ env.IMAGE_TAG }} ansible-galaxy collection list >> $COMMANDS_FILE |
| 145 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 146 | + echo "" >> $COMMANDS_FILE |
| 147 | + echo "#### EE Testing" >> $COMMANDS_FILE |
| 148 | + echo "" >> $COMMANDS_FILE |
| 149 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 150 | + podman run -it ${{ env.EE }}:${{ env.IMAGE_TAG }} ansible-navigator --version >> $COMMANDS_FILE |
| 151 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 152 | + echo "" >> $COMMANDS_FILE |
| 153 | + echo "#### Ansible version" >> $COMMANDS_FILE |
| 154 | + echo "" >> $COMMANDS_FILE |
| 155 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 156 | + podman run -it ${{ env.EE }}:${{ env.IMAGE_TAG }} ansible --version >> $COMMANDS_FILE |
| 157 | + echo "\`\`\`" >> $COMMANDS_FILE |
| 158 | + echo "</details>" >> $COMMANDS_FILE |
| 159 | +
|
| 160 | + - name: Upload build artifact |
| 161 | + uses: actions/upload-artifact@v4 |
| 162 | + with: |
| 163 | + name: commands-${{ env.EE }} |
| 164 | + path: ./commands-${{ env.EE }}.txt |
| 165 | + |
| 166 | + - name: Push To GHCR |
| 167 | + id: push_to_ghcr |
| 168 | + uses: redhat-actions/push-to-registry@v2 |
| 169 | + with: |
| 170 | + image: ${{ env.EE }} |
| 171 | + tags: ${{ env.IMAGE_TAG }} |
| 172 | + registry: ${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }} |
| 173 | + |
| 174 | + - name: Set push success flag |
| 175 | + if: success() |
| 176 | + run: echo "push_success=true" >> $GITHUB_ENV |
| 177 | + |
| 178 | + - name: Print summary |
| 179 | + run: | |
| 180 | + echo "## Usage" >> $GITHUB_STEP_SUMMARY |
| 181 | + echo "Image pushed to repository: ${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ env.EE }}:${{ env.IMAGE_TAG }}" >> $GITHUB_STEP_SUMMARY |
| 182 | + echo "> \`podman pull ${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ env.EE }}:${{ env.IMAGE_TAG }}\`" >> $GITHUB_STEP_SUMMARY |
| 183 | +
|
| 184 | + post-comment: |
| 185 | + if: github.event_name == 'pull_request_target' |
| 186 | + needs: build-ee |
| 187 | + runs-on: ubuntu-latest |
| 188 | + steps: |
| 189 | + - name: Checkout |
| 190 | + uses: actions/checkout@v2 |
| 191 | + |
| 192 | + - name: Download Artifacts |
| 193 | + uses: actions/download-artifact@v4 |
| 194 | + |
| 195 | + - name: Post Comment |
| 196 | + uses: actions/github-script@v7 |
| 197 | + with: |
| 198 | + script: | |
| 199 | + const fs = require('fs'); |
| 200 | + const path = require('path'); |
| 201 | + let commentBody = '### **EE Images Built** \n\n'; |
| 202 | +
|
| 203 | + const artifactsDirectory = './'; // Base directory where artifacts are downloaded |
| 204 | + fs.readdirSync(artifactsDirectory, { withFileTypes: true }).forEach(dirent => { |
| 205 | + if (dirent.isDirectory() && dirent.name.startsWith('commands-')) { |
| 206 | + const artifactDirPath = path.join(artifactsDirectory, dirent.name); |
| 207 | + fs.readdirSync(artifactDirPath).forEach(file => { |
| 208 | + const filePath = path.join(artifactDirPath, file); |
| 209 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 210 | + commentBody += content + '\n'; |
| 211 | + }); |
| 212 | + } |
| 213 | + }); |
| 214 | +
|
| 215 | + const prNumber = context.issue.number; |
| 216 | + const repo = context.repo; |
| 217 | + github.rest.issues.createComment({ |
| 218 | + ...repo, |
| 219 | + issue_number: prNumber, |
| 220 | + body: commentBody.trim() |
| 221 | + }); |
0 commit comments