Skip to content

Commit 52a382d

Browse files
authored
feat: Action to copy .devcontainer
This Github Action is manually triggered to add a custom .devcontainer to a working repository.
1 parent 1b354e0 commit 52a382d

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Copy Devcontainer Settings
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
target_repo:
7+
description: 'The name of the target repository (e.g., your-repo)'
8+
required: true
9+
type: string
10+
checkout_branch:
11+
description: 'The branch to checkout in the target repository (e.g., main, develop)'
12+
default: 'main'
13+
type: string
14+
push_branch:
15+
description: 'The branch to push changes to in the target repository (e.g., suggested-update)'
16+
default: 'suggested-update'
17+
type: string
18+
19+
jobs:
20+
copy_devcontainer_settings:
21+
runs-on: ubuntu-latest
22+
permissions:
23+
contents: write
24+
pull-requests: write
25+
26+
steps:
27+
- name: Checkout Target Repo
28+
uses: actions/checkout@v4
29+
with:
30+
repository: ${{ github.repository_owner }}/${{ github.event.inputs.target_repo }}
31+
token: ${{ secrets.PAT }}
32+
ref: ${{ github.event.inputs.checkout_branch }}
33+
34+
- name: Checkout Settings Repo (Current Repo)
35+
uses: actions/checkout@v4
36+
with:
37+
path: settings_repo
38+
39+
- name: Copy Devcontainer Files (Overwrite existing)
40+
run: |
41+
mkdir -p .devcontainer
42+
cp -rf settings_repo/.devcontainer/* ./.devcontainer/
43+
rm -rf settings_repo
44+
45+
- name: Configure Git
46+
run: |
47+
git config --global user.name "github-actions[bot]"
48+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
49+
50+
# - name: Checkout or Create Push Branch # Improved Branch Handling
51+
# run: |
52+
# git fetch origin ${{ github.event.inputs.push_branch }} || git checkout --orphan ${{ github.event.inputs.push_branch }}
53+
# echo "new_branch=${{ github.event.inputs.push_branch }}" >> $GITHUB_ENV
54+
55+
- name: Check if Push Branch Exists (Remote Check)
56+
id: check_branch
57+
run: |
58+
if git ls-remote --heads origin ${{ github.event.inputs.push_branch }} | wc -l; then
59+
timestamp=$(date +%s)
60+
echo "${{ github.event.inputs.push_branch }}-${timestamp}"
61+
new_branch_name="${{ github.event.inputs.push_branch }}-${timestamp}"
62+
echo "new_branch=${new_branch_name}" >> $GITHUB_ENV
63+
else
64+
echo "new_branch=${{ github.event.inputs.push_branch }}" >> $GITHUB_ENV
65+
fi
66+
67+
- name: Commit and Push Changes to Target Repo
68+
run: |
69+
git checkout -b ${{ env.new_branch }}
70+
git add .
71+
commit_title="chore(devcontainer): Update devcontainer settings"
72+
commit_body="From: ${{ github.server_url }}/${{ github.repository }}"
73+
git commit -m "${commit_title}" -m "${commit_body}"
74+
git push -u origin ${{ env.new_branch }}
75+
76+
- name: Create Pull Request
77+
uses: actions/github-script@v7
78+
with:
79+
github-token: ${{ secrets.PAT }}
80+
script: |
81+
const headBranch = '${{ env.new_branch }}';
82+
const baseBranch = '${{ github.event.inputs.checkout_branch }}';
83+
const targetRepo = '${{ github.event.inputs.target_repo }}';
84+
85+
return github.rest.pulls.create({
86+
owner: context.repo.owner, // Use the owner from the current context
87+
repo: targetRepo,
88+
title: 'Update devcontainer settings',
89+
head: headBranch,
90+
base: baseBranch,
91+
body: `Automated update of devcontainer settings from ${{ github.server_url }}/${{ github.repository }}`
92+
});

0 commit comments

Comments
 (0)