|
| 1 | +name: "Ansible V3 K8S" |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + paths: ["IaC/3-v3/ansible/**"] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + action: |
| 10 | + description: "실행할 작업" |
| 11 | + required: true |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - syntax-check |
| 15 | + - deploy |
| 16 | + ref: |
| 17 | + description: "체크아웃할 커밋 SHA (deploy 시 필수)" |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: ansible-v3-k8s-dev |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +permissions: |
| 26 | + id-token: write |
| 27 | + contents: read |
| 28 | + pull-requests: write |
| 29 | + |
| 30 | +env: |
| 31 | + AWS_REGION: "ap-northeast-2" |
| 32 | + ANSIBLE_DIR: "IaC/3-v3/ansible" |
| 33 | + |
| 34 | +jobs: |
| 35 | + # ────────────────────────────────────────────── |
| 36 | + # Syntax Check: PR 또는 수동 트리거 |
| 37 | + # ────────────────────────────────────────────── |
| 38 | + syntax-check: |
| 39 | + name: "Ansible Syntax Check" |
| 40 | + if: >- |
| 41 | + github.event_name == 'pull_request' || |
| 42 | + (github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'syntax-check') |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + fetch-depth: 0 |
| 48 | + |
| 49 | + - name: Checkout ref |
| 50 | + if: inputs.ref != '' |
| 51 | + run: git checkout ${{ inputs.ref }} |
| 52 | + |
| 53 | + - name: Install Ansible |
| 54 | + run: pip install ansible boto3 |
| 55 | + |
| 56 | + - name: Install amazon.aws collection |
| 57 | + run: ansible-galaxy collection install amazon.aws |
| 58 | + |
| 59 | + - name: Ansible Syntax Check |
| 60 | + run: ansible-playbook playbooks/site.yml --syntax-check |
| 61 | + working-directory: ${{ env.ANSIBLE_DIR }} |
| 62 | + |
| 63 | + - name: Ansible Lint (optional) |
| 64 | + run: | |
| 65 | + pip install ansible-lint |
| 66 | + ansible-lint playbooks/site.yml |
| 67 | + working-directory: ${{ env.ANSIBLE_DIR }} |
| 68 | + continue-on-error: true |
| 69 | + |
| 70 | + - name: Comment Result on PR |
| 71 | + if: github.event_name == 'pull_request' |
| 72 | + uses: actions/github-script@v7 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const body = `### Ansible Syntax Check Result ✅ |
| 76 | + | Step | Status | |
| 77 | + |------|--------| |
| 78 | + | Syntax Check | ${'${{ steps.syntax.outcome || 'success' }}' === 'failure' ? '❌' : '✅'} | |
| 79 | + | Lint | ${'${{ steps.lint.outcome || 'success' }}' === 'failure' ? '⚠️' : '✅'} | |
| 80 | +
|
| 81 | + *Pushed by: @${{ github.actor }}*`; |
| 82 | +
|
| 83 | + const { data: comments } = await github.rest.issues.listComments({ |
| 84 | + owner: context.repo.owner, repo: context.repo.repo, |
| 85 | + issue_number: context.issue.number |
| 86 | + }); |
| 87 | + const bot = comments.find(c => c.user.type === 'Bot' && c.body.includes('Ansible Syntax Check Result')); |
| 88 | + const params = { owner: context.repo.owner, repo: context.repo.repo, body }; |
| 89 | + if (bot) { await github.rest.issues.updateComment({ ...params, comment_id: bot.id }); } |
| 90 | + else { await github.rest.issues.createComment({ ...params, issue_number: context.issue.number }); } |
| 91 | +
|
| 92 | + # ────────────────────────────────────────────── |
| 93 | + # Deploy: 수동 트리거만 (workflow_dispatch + action=deploy + ref 필수) |
| 94 | + # ────────────────────────────────────────────── |
| 95 | + deploy: |
| 96 | + name: "Ansible Deploy" |
| 97 | + if: >- |
| 98 | + github.event_name == 'workflow_dispatch' && |
| 99 | + github.event.inputs.action == 'deploy' && |
| 100 | + github.event.inputs.ref != '' |
| 101 | + runs-on: ubuntu-latest |
| 102 | + steps: |
| 103 | + - uses: actions/checkout@v4 |
| 104 | + with: |
| 105 | + fetch-depth: 0 |
| 106 | + |
| 107 | + - name: Checkout ref |
| 108 | + run: git checkout ${{ inputs.ref }} |
| 109 | + |
| 110 | + - uses: aws-actions/configure-aws-credentials@v4 |
| 111 | + with: |
| 112 | + role-to-assume: ${{ secrets.ANSIBLE_AWS_ROLE_ARN }} |
| 113 | + aws-region: ${{ env.AWS_REGION }} |
| 114 | + |
| 115 | + - name: Install Ansible + dependencies |
| 116 | + run: pip install ansible boto3 |
| 117 | + |
| 118 | + - name: Install amazon.aws collection |
| 119 | + run: ansible-galaxy collection install amazon.aws |
| 120 | + |
| 121 | + - name: Install SSM plugin |
| 122 | + run: | |
| 123 | + curl -so session-manager-plugin.deb \ |
| 124 | + "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" |
| 125 | + sudo dpkg -i session-manager-plugin.deb |
| 126 | +
|
| 127 | + - name: Run Ansible Playbook |
| 128 | + run: | |
| 129 | + ansible-playbook \ |
| 130 | + -i inventory/aws_ec2.yaml \ |
| 131 | + playbooks/site.yml \ |
| 132 | + -e "ansible_aws_ssm_profile=" \ |
| 133 | + -v |
| 134 | + working-directory: ${{ env.ANSIBLE_DIR }} |
0 commit comments