Langkah 2: Buat contoh revisi aplikasi - AWS CodeDeploy

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Langkah 2: Buat contoh revisi aplikasi

Pada langkah ini, Anda membuat contoh revisi aplikasi untuk diterapkan ke instans lokal Anda.

Karena sulit untuk mengetahui perangkat lunak dan fitur mana yang sudah terinstal—atau diizinkan untuk diinstal oleh kebijakan organisasi Anda—pada instans lokal Anda, contoh revisi aplikasi yang kami tawarkan di sini hanya menggunakan skrip batch (untuk Windows Server) atau skrip shell (untuk Server Ubuntu dan RHEL) untuk menulis file teks ke lokasi di instans lokal Anda. Satu file ditulis untuk masing-masing dari beberapa peristiwa siklus hidup CodeDeploy penerapan, termasuk Instal,, AfterInstall, ApplicationStartdan. ValidateService Selama peristiwa siklus hidup BeforeInstallpenerapan, skrip akan berjalan untuk menghapus file lama yang ditulis selama penerapan sampel ini sebelumnya dan membuat lokasi pada instance lokal untuk menulis file baru.

catatan

Contoh revisi aplikasi ini mungkin gagal diterapkan jika salah satu dari berikut ini benar:

  • Pengguna yang memulai CodeDeploy agen pada instans lokal tidak memiliki izin untuk mengeksekusi skrip.

  • Pengguna tidak memiliki izin untuk membuat atau menghapus folder di lokasi yang tercantum dalam skrip.

  • Pengguna tidak memiliki izin untuk membuat file teks di lokasi yang tercantum dalam skrip.

catatan

Jika Anda mengonfigurasi instance Windows Server dan ingin menyebarkan sampel yang berbeda, Anda mungkin ingin menggunakannya Langkah 2: Konfigurasikan konten sumber Anda untuk menyebarkan ke instans Windows Server Amazon EC2 di Tutorial: Menyebarkan “halo, dunia!” aplikasi dengan CodeDeploy (Windows Server) tutorial.

Jika Anda mengonfigurasi instance RHEL dan ingin menerapkan sampel yang berbeda, Anda mungkin ingin menggunakan contoh yang ada Langkah 2: Konfigurasikan konten sumber Anda untuk digunakan ke instans Amazon EC2 Amazon Linux atau Red Hat Enterprise Linux Amazon EC2 di Tutorial: Terapkan WordPress ke instans Amazon EC2 (Amazon Linux atau Red Hat Enterprise Linux dan Linux, macOS, atau Unix) tutorial.

Saat ini, tidak ada contoh alternatif untuk Ubuntu Server.

  1. Pada mesin pengembangan Anda, buat subdirektori (subfolder) bernama CodeDeployDemo-OnPrem yang akan menyimpan file revisi aplikasi sampel, dan kemudian beralih ke subfolder. Untuk contoh ini, kami berasumsi Anda akan menggunakan c:\temp folder sebagai folder root untuk Windows Server atau /tmp folder sebagai folder root untuk Ubuntu Server dan RHEL. Jika Anda menggunakan folder yang berbeda, pastikan untuk menggantinya dengan folder kami di seluruh tutorial ini:

    Untuk Windows:

    mkdir c:\temp\CodeDeployDemo-OnPrem cd c:\temp\CodeDeployDemo-OnPrem

    Untuk Linux, macOS, atau Unix:

    mkdir /tmp/CodeDeployDemo-OnPrem cd /tmp/CodeDeployDemo-OnPrem
  2. Di root CodeDeployDemo-OnPrem subfolder, gunakan editor teks untuk membuat dua file bernama appspec.yml daninstall.txt:

    appspec.ymluntuk Windows Server:

    version: 0.0 os: windows files: - source: .\install.txt destination: c:\temp\CodeDeployExample hooks: BeforeInstall: - location: .\scripts\before-install.bat timeout: 900 AfterInstall: - location: .\scripts\after-install.bat timeout: 900 ApplicationStart: - location: .\scripts\application-start.bat timeout: 900 ValidateService: - location: .\scripts\validate-service.bat timeout: 900

    appspec.ymluntuk Server Ubuntu dan RHEL:

    version: 0.0 os: linux files: - source: ./install.txt destination: /tmp/CodeDeployExample hooks: BeforeInstall: - location: ./scripts/before-install.sh timeout: 900 AfterInstall: - location: ./scripts/after-install.sh timeout: 900 ApplicationStart: - location: ./scripts/application-start.sh timeout: 900 ValidateService: - location: ./scripts/validate-service.sh timeout: 900

    Untuk informasi selengkapnya tentang AppSpec file, lihat Tambahkan file spesifikasi aplikasi ke revisi untuk CodeDeploy danCodeDeploy AppSpec referensi file.

    install.txt:

    The Install deployment lifecycle event successfully completed.
  3. Di bawah root CodeDeployDemo-OnPrem subfolder, buat scripts subfolder, lalu beralih ke sana:

    Untuk Windows:

    mkdir c:\temp\CodeDeployDemo-OnPrem\scripts cd c:\temp\CodeDeployDemo-OnPrem\scripts

    Untuk Linux, macOS, atau Unix:

    mkdir -p /tmp/CodeDeployDemo-OnPrem/scripts cd /tmp/CodeDeployDemo-OnPrem/scripts
  4. Di root scripts subfolder, gunakan editor teks untuk membuat empat file bernamabefore-install.bat,,, dan validate-service.bat untuk Windows Server after-install.batapplication-start.bat, atau,, before-install.sh after-install.shapplication-start.sh, dan validate-service.sh untuk Ubuntu Server dan RHEL:

    Untuk Windows Server:

    before-install.bat:

    set FOLDER=%HOMEDRIVE%\temp\CodeDeployExample if exist %FOLDER% ( rd /s /q "%FOLDER%" ) mkdir %FOLDER%

    after-install.bat:

    cd %HOMEDRIVE%\temp\CodeDeployExample echo The AfterInstall deployment lifecycle event successfully completed. > after-install.txt

    application-start.bat:

    cd %HOMEDRIVE%\temp\CodeDeployExample echo The ApplicationStart deployment lifecycle event successfully completed. > application-start.txt

    validate-service.bat:

    cd %HOMEDRIVE%\temp\CodeDeployExample echo The ValidateService deployment lifecycle event successfully completed. > validate-service.txt

    Untuk Server Ubuntu dan RHEL:

    before-install.sh:

    #!/bin/bash export FOLDER=/tmp/CodeDeployExample if [ -d $FOLDER ] then rm -rf $FOLDER fi mkdir -p $FOLDER

    after-install.sh:

    #!/bin/bash cd /tmp/CodeDeployExample echo "The AfterInstall deployment lifecycle event successfully completed." > after-install.txt

    application-start.sh:

    #!/bin/bash cd /tmp/CodeDeployExample echo "The ApplicationStart deployment lifecycle event successfully completed." > application-start.txt

    validate-service.sh:

    #!/bin/bash cd /tmp/CodeDeployExample echo "The ValidateService deployment lifecycle event successfully completed." > validate-service.txt unset FOLDER
  5. Untuk Ubuntu Server dan RHEL saja, pastikan keempat skrip shell memiliki izin eksekusi:

    chmod +x ./scripts/*